Skip to content
Mauricio David edited this page Aug 27, 2017 · 20 revisions

Expressions are path or formulas to access and modify your document data. Based on JSON path article (http://goessner.net/articles/JsonPath/), LiteDB support a near syntax to navigate in a single document. Path always returns an IEnumerable<BsonValue> in any case.

BsonExpression are the class that parse a string expression (or path) and compile into a LINQ Expression to be fast evaluate by LiteDB. Parser uses

  • Path starts with $: $.Address.Street
  • Int values starts with [0-9]*: 123
  • Double values starts with [0-9].[0-9]: 123.45
  • Strings are represented with a single ': 'Hello World'
  • Null are just null
  • Bool are represented using true or false keyword.
  • Functions are represented with FUNCTION_NAME(par1, par2, ...): LOWER($.Name)

Examples:

  • $.Price
  • $.Price + 100
  • SUM($.Items[*].Price)

Expressions can be used in your database to:

  • Create an index based on an expression: db.EnsureIndex(x => x.Name, "LOWER($.Name)")
  • Query documents inside a collection based on expression: Query.EQ("SUBSTRING($.Name, 0, 1)", "T")
  • Update field only data
  • Select command in shell: db.mycol.select $.Name, ARRAY(UPPER($.Books[*].Title)) AS titles where $.Name startswith "John"

Path

  • $ - Root
  • $.Name - Name field
  • $.Name.First - First field from a Name subdocument
  • $.Books - Returns book array value
  • $.Books[0] - Return first book inside books array
  • $.Books[*] - Return all books inside books array
  • $.Books[*].Title Return all titles from all books
  • $.Books[-1] - Return last book inside books array

Path also support expression to filter child node

  • $.Books[@.Title = 'John Doe'] - Return all books where title is John Doe
  • $.Books[@.Price > 100].Title - Return all titles where book price are greater than 100

Inside array, @ represent current sub document. It's possible use functions inside expressions too:

  • $.Books[SUBSTRING(LOWER(@.Title), 0, 1) = 'j'] - Return all books with title starts with T or t.

Functions