0% found this document useful (0 votes)
41 views2 pages

Mongodbqueries PDF

This document provides a reference card for MongoDB queries and updates. It lists various query operators and what they match, such as equality, comparison, element, logical, and regular expression queries. It also lists update modifiers for incrementing, setting, unsetting, pushing, adding to sets, popping, pulling, and pulling all from arrays. The document was created and distributed by MongoDB to serve as a quick reference guide.

Uploaded by

Rahul Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Mongodbqueries PDF

This document provides a reference card for MongoDB queries and updates. It lists various query operators and what they match, such as equality, comparison, element, logical, and regular expression queries. It also lists update modifiers for incrementing, setting, unsetting, pushing, adding to sets, popping, pulling, and pulling all from arrays. The document was created and distributed by MongoDB to serve as a quick reference guide.

Uploaded by

Rahul Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MongoDB Reference Card

Queries and Updates


Queries and What They Match
{a: 10} {a: 10, b: "hello"} {a: {$gt: 10}} {a: {$in: [10, "hello"]}} {a: {$all: [10, "hello"]}}

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.

MongoDB Reference Cards // Queries and Updates

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.

You might also like