-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expressions
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
orfalse
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:
collection.EnsureIndex("Name", true, "LOWER($.Name)")
collection.EnsureIndex(x => x.Name, true, "LOWER($.Name)")
- Query documents inside a collection based on expression
Query.EQ("SUBSTRING($.Name, 0, 1)", "T")
- Update shell command
db.mycol.update $.Name = LOWER($.Name) where _id = 1
- Create new document result in SELECT shell command
db.mycol.select $._id, ARRAY(UPPER($.Books[*].Title)) AS titles where $.Name startswith "John"
-
$
- 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 are always works in IEnumerable<BsonValue>
as input/output parameters.
-
LOWER($.Name)
- ReturnsIEnumerable
with a single element -
LOWER($.Books[*].Title)
- ReturnsIEnumerable
with all values in lower case
String function will work only if your <values>
are string. Any other data type will skip results
-
LOWER(<values>)
: SameToLower()
fromString
. Returns a string -
UPPER(<values>)
: SameToUpper()
fromString
. Returns a string -
SUBSTRING(<values>, <index>, <length>)
: SameSubstring()
fromString
. Returns a string -
LPAD(<values>, <totalWidth>, <paddingChar>)
: SamePadLeft()
fromString
. Returns a string -
RPAD(<values>, <totalWidth>, <paddingChar>)
: SamePadRight()
fromString
. Returns a string -
FORMAT(<values>, <format>)
: SameFormat()
fromString
. Works if any data type (useRawValue
). Returns a string
Operators are function to implement same math syntax.
-
ADD(<left>, <right>)
: Same as<left> + <right>
- Returns a number result with both sides are number. If any side are string, concat values and return as string. -
MINUS(<left>, <right>)
: Same as<left> - <right>
- Returns a number -
MULTIPLY(<left>, <right>)
: Same as<left> * <right>
- Returns a number -
DIVIDE(<left>, <right>)
: Same as<left> / <right>
- Returns a number -
EQ(<left>, <right>)
: Same as<left> = <right>
. Returns a boolean -
NEQ(<left>, <right>)
: Same as<left> != <right>
. Returns a boolean -
GT(<left>, <right>)
: Same as<left> != <right>
. Returns a boolean -
GTE(<left>, <right>)
: Same as<left> >= <right>
. Returns a boolean -
LT(<left>, <right>)
: Same as<left> < <right>
. Returns a boolean -
LTE(<left>, <right>)
: Same as<left> <= <right>
. Returns a boolean -
AND(<left>, <right>)
: Same as<left> && <right>
. Left and right must be a boolean and returns a boolean -
OR(<left>, <right>)
: Same as<left> || <right>
. Left and right must be a boolean and returns a boolean
-
SUM(<values>)
- Sum all number values and returns a number -
COUNT(<values>)
- Count all values and returns a integer
-
ARRAY(<values>)
- Convert all values into a single array -
IS_DATE(<values>)
- Return true for each value that are date -
IS_NUMBER(<values>)
- Return true for each value that are a number -
IS_STRING(<values>)
- Return true for each value that are string
All function are static methods from BsonExpression
.
Data Modeling
- Data Structure
- BsonDocument
- Object Mapping
- Relationships with Document References
- Collections
- FileStorage
Index
Query
Database
Version 4 changes
Shell