-
Notifications
You must be signed in to change notification settings - Fork 5
Queryable Array
The 'queryable array' is an extended array object that contains a bunch of Linq like functions to help filter through your result set. It is returned in callbacks on loads of multiple documents and in both dynamic queries and index queries.
The queryable array supports chained functions, much like Linq but the syntax is a little different. In C# we can define lambda expressions to concisely reduce our result set, in JavaScript the code is a little more verbose but the idea is the same.
We need to pass a function to each queryable function, and return a boolean operation.
documents.where(function(x) { return x.age > 5; });
This statement will return all documents where the age is greater than 5. The equivalent in C# would be:
documents.Where(x => x.age > 5);
As you can see the code is very similar and the meaning is clear from both, but in JavaScript we have to write a little more code. Alternatively if you are using coffeescript, queryable functions are far more readable.
documents.where((x) -> x.age > 5)
- all(function) - return true if all documents satisfy the condition provided, false otherwise
- any(function) - returns true if any documents satisfy the condition provided, false otherwise
- count(function) - returns the number of documents that satisfy the condition provided
- elementAt(index) - returns the document at the specified index
- exists(function) - returns true if a document exists that satisfies the condition provided
- first(function?) - returns the first document that satisfies the condition provided. returns first document if no function is provided
- last(function?) - returns the last document that satisfies the condition provided. returns last document if no function is provided
- load(key) - load the document with the specified key
- random(function) - returns a random document that satisfies the condition provided
- select(property) - returns a new queryable array containing the values of the property specified
- select(function) - applies the function provided to the documents and returns an array containing the transformed results
- single(function) - returns a single document that satisfies the condition provided. or null. throws on 1+ documents
- where(function) - returns all documents that satisfy the condition provided