Mongodbqueries PDF
Mongodbqueries PDF
Docs where a is 10, or an array containing the value 10. Docs where a is 10 and b is "hello". Docs where a is greater than 10. Also available: $lt (<), $gte (>=), $lte (<=), and $ne (!=). Docs where a is either 10 or "hello". Docs where a is an array containing both 10 and "hello". Docs where a is an embedded document with b equal to 10. Docs where a is an array that contains an element with both b equal to 1 and c equal to 2. Docs where a is 1 or b is 2. Docs where a begins with the letter m. Docs where a mod 10 is 1. Docs where a is a string (see bsonspec.org for more).
{"a.b": 10}
{a: {$elemMatch: {b: 1, c: 2}}} {$or: [{a: 1}, {b: 2}]} {a: /^m/} {a: {$mod: [10, 1]}} {a: {$type: 2}}
The following queries cannot use indexes as of MongoDB v2.0. These query forms should normally be accompanied by at least one other query term which does use an index:
{a: {$nin: [10, "hello"]}} {a: {$size: 3}} {a: {$exists: true}} {a: /foo.*bar/} {a: {$not: {$type: 2}}}
Docs where a is anything but 10 or "hello". Docs where a is an array with exactly 3 elements. Docs containing an a field. Docs where a matches the regular expression foo.*bar. Docs where a is not a string. $not negates any of the other query operators.
Update Modifiers
{$inc: {a: 2}} {$set: {a: 5}} {$unset: {a: 1}} {$push: {a: 1}} {$push: {a: {$each: [1, 2]}}} {$addToSet: {a: 1}}
Increment a by 2. Set a to the value 5. Delete the a key. Append the value 1 to the array a. Append both 1 and 2 to the array a. Append the value 1 to the array a (if the value doesnt already exist). Append both 1 and 2 to the array a (if they dont already exist). Remove the last element from the array a. Remove the first element from the array a. Remove all occurrences of 5 from the array a. Remove all occurrences of 5 or 6 from the array a.
{$addToSet: {a: {$each: [1, 2]}}} {$pop: {a: 1}} {$pop: {a: -1}} {$pull: {a: 5}} {$pullAll: {a: [5, 6]}}
Created and distributed by 10gen, Inc. For more information or to download MongoDB, visit mongodb.org or 10gen.com.