Skip to content
Mauricio David edited this page Jan 31, 2020 · 16 revisions

This documentation is valid only for v4.x version.


LiteDB project has a simple console application (LiteDB.Shell.exe) that can be used to work with your databases. It's very useful to see, update and test your data.

In v4, LiteDB back shell command support into LiteDB.dll, so now shell commands are part of LiteDB (not only an external tool).

Reference

Shell console commands

The commands here works only in console application LiteDB.Shell.exe:

  • help [full] - Display basic or full commands help
  • open <filename|connectionString> - Open new database. If not exists, create a new one. Can be passed a connection string with all options (like password or timeout)
  • close - Close current database
  • pretty - Show JSON in pretty format
  • ed - Open notepad.exe with last command to edit
  • run <filename> - Read filename and add each line as a new command
  • spool [on|off] - Turn on/off spool of all commands
  • timer - Show a timer on command prompt
  • -- - Comments (ignore rest of line)
  • /<command>/ - Supports multiline command
  • upgrade /<filename|connectionString>/ - Upgrade datafile from LiteDB v2
  • version - Show LiteDB assembly version
  • quit - Exit shell application

Collections commands

Syntax: db.<collection>.<command>

  • insert <jsonDoc> - Find a document using query filter syntax

    • db.customer.insert { _id:1, Name: "John Doe", Age: 38 }
    • db.customer.insert { Name: "John Doe" } - Auto create Id as ObjectId
  • bulk <filename.json> - Insert all documents inside a JSON file. JSON file must be an JSON array of documents

    • db.customer.bulk my-documents.json
  • update <jsonDoc> - Update a existing document using _id

    • db.customer.update { _id:1, Name: "Joana Doe", Age: 29 }
  • update <field|path>=<value|expression>, [..] [where <filter>] - Update multiple documents fields using filter query

    • db.customer.update Name = "John" where _id = 1
    • db.customer.update Age = $.Age + 1 where _id > 0
    • db.customer.update Addresses[*].Full = Addresses[*].Street + ", " + Addresses[*].Number
  • delete <filter> - Delete documents using filter syntax

    • db.customer.delete _id = 1
    • db.customer.delete Name like "Jo"
  • ensureIndex <field|name> [unique] [using <expr>] - Create a new index on collection in field. Support json options See Indexes

    • db.customers.ensureIndex Name unique - Create a unique index
    • db.customers.ensureIndex NameLower using LOWER($.Name)
  • indexes - List all indexes on collections

    • db.customers.indexes
  • dropIndex <field|name> - Drop an index

    • db.customers.dropIndex Name
  • drop - Drop a collection and all documents inside. Return false if not exists

    • db.customer.drop
  • rename - Rename a collection. Return true if success or false if an error occurs

    • db.customer.rename my_customers
  • count <filter> - Count documents with defined filter. See <filter> syntax below

    • db.customer.count Name = "John Doe"
    • db.customer.count Age between [20, 40]
  • min <field> - Get lowest value in field index

    • db.customer.min _id
    • db.customer.min Age
  • max <field> -

    • db.customer.max _id
    • db.customer.max Age
  • find [filter][skip N][limit M][includes p0, p1, ..] - Find documents using query filter syntax. See <filter> syntax below

    • db.customer.find
    • db.customer.find limit 10
    • db.customer.find Name = "John Doe"
    • db.customer.find Age between [20, 40]
    • db.customer.find Name like "John" and Age > 30
    • db.customer.find skip 10 limit 10 includes $.Orders[*]
  • select [path|expr] as Name1, [path|expr] [where <filter>][skip N][limit M][include p0, p1, ..] - Find and transform documents

    • db.customer.select $ where Age > 30
    • db.customer.select LOWER($.Addresses[*].Street) where Age > 30
    • db.customer.select $.Name as Name, ARRAY($.Addresses[@.City = 'NY']) as addresses where Age > 30
  • <filter> = <field> [=|>|>=|<|<=|!=|like|contains|in|between] <jsonValue>

  • <filter> = <filter> [and|or] <filter>

  • <jsonDoc> and <jsonValue> are JSON extended format. See Data Structure.

FileStorage

Syntax: fs.<command>

  • upload <fileId> <filename> - Upload a new local file to database. If fileId exists, override data content

    • fs.upload my-file picture.jpg
    • fs.upload $/photos/pic-001 C:\Temp\mypictures\picture-1.jpg
  • download <fileId> <filename> - Download existing database file to a local file.

    • fs.download my-file copy-picture.jpg
    • fs.download $/photos/pic-001 C:\Temp\mypictures\copy-picture-1.jpg
  • update <fileId> <jsonDoc> - Update file metadata

    • fs.update my-file { user: "John", machine: "srv001" }
  • delete <fileId> - Delete a file

    • fs.delete my-file
  • find [fileId] - List all files inside database or starting with fileId parameter

    • fs.find
    • fs.find $/photos/

Database utils

Syntax: db.<command>

  • userversion [N] - Get/Set user database file version

  • shrink [password] - Reduce database removing empty pages and change password (optional). If password was not provided, new datafile will be not encrypted.