MongoDB query syntax (greater than, less than, greater than or equal to, less than or equal to, etc.)

Source: Internet
Author: User
Tags findone mongodb query

1). Greater than, less than, greater than or equal to, less than or equal to

$GT: Greater Than
$LT: Less than
$gte: greater than or equal to
$lte: Less than or equal to

Example:

Db.collection.find ({"field": {$gt: Value}}); Greater Than:field > value
Db.collection.find ({"field": {$lt: Value}}); Less Than:field < value
Db.collection.find ({"field": {$gte: Value}}); Greater than or equal To:field >= value
Db.collection.find ({"field": {$lte: Value}}); Less than or equal To:field <= value

If query J is greater than 3, less than 4:

Db.things.find ({j: {$lt: 3}});
Db.things.find ({j: {$gte: 4}});

can also be combined within a single statement:

Db.collection.find ({"field": {$gt: value1, $lt: value2}}); value1 < Field < value

2) Not equal to $ne

Example:

Db.things.find ({x: {$ne: 3}});



3) in and not in ($in $nin)

Grammar:
Db.collection.find ({"field": {$in: Array}});

Example:

Db.things.find ({j:{$in: [2,4,6]}});
Db.things.find ({j:{$nin: [2,4,6]}});


4) modulo Operation $mod

As in the following operation:
Db.things.find ("this.a% 10 = = 1")

Available $mod instead of:

Db.things.find ({A: {$mod: [10, 1]}})


5) $all

$all is similar to $in, but he needs to match all the values in the condition:

If you have an object:

{A: [1, 2, 3]}

The following conditions can be matched:

Db.things.find ({A: {$all: [2, 3]}});

But the following conditions are not:

Db.things.find ({A: {$all: [2, 3, 4]}});

6) $size

$size is the number of elements in the matching array, if there is an object: {a:["foo"}, he has only one element:

The following statement can match: Db.things.find ({A: {$size: 1}});

The official web says it can't be used to match a range of elements, and if you're looking for $size<5, they recommend creating a field to hold the number of elements.

You cannot use $size to find a range of sizes (for example:arrays with more than 1 element). If you need-to-a range, create an extra size field that is increment when you add elements.

7) $exists

$exists used to determine whether an element exists:

Such as:

Db.things.find ({A: {$exists: true}}); If element A is present, it returns
Db.things.find ({A: {$exists: false}}); If element A does not exist, it returns

8) $type

$type match the type of an element based on Bson type, as if it were matched by type ID, but I didn't find the Bson type and ID table.

Db.things.find ({A: {$type: 2}}); Matches if A is a string
Db.things.find ({A: {$type: 16}}); Matches if a is an int
9) Regular Expressions

MONGO supports regular expressions, such as:

Db.customers.find ({name:/acme.*corp/i}); The meaning of the latter I is case-sensitive

10) Querying values within the data

The following query is a record of red in query colors, and if the colors element is a data, the database will iterate over the elements of this array to query. Db.things.find ({colors: "red"});

) $elemMatch

If an object has an element that is an array, then $elemmatch can match the elements within the array:

> T.find ({x: {$elemMatch: {a:1, B: {$gt: 1}}})
{"_id": ObjectId ("4b5783300334000000000aa9"),
"X": [{"A": 1, "B": 3}, 7, {"B": [+]}, {"A": 11}]
} $elemMatch: {a:1, B: {$gt: 1}} All conditions must match.

Note that the above statement is not the same as below.

> T.find ({"X.A": 1, "x.b": {$gt: 1}})
$elemMatch is the match {"A": 1, "B": 3}, and the following is the match {"B": "A", {"a": 11}


12) Querying the values of embedded objects

Db.postings.find ({"Author.name": "Joe"});

Note that usage is author.name, just use one point. More detailed can see this link: dot notation

As an example:

> Db.blog.save ({title: "My first Post", Author: {Name: "Jane", Id:1}})

If we want to query authors name is Jane, we can do this:

> Db.blog.findOne ({"Author.name": "Jane"})

If not, then you need to use the following sentence to match:

Db.blog.findOne ({"Author": {"name": "Jane", "id": 1}})

The following sentence:

Db.blog.findOne ({"Author": {"name": "Jane"}})

is not matched, because MongoDB for sub-objects, he is exact match.

13) The meta-operator $not take the inverse

Such as:

Db.customers.find ({name: {$not:/acme.*corp/i}});d B.things.find ({A: {$not: {$mod: [10, 1]}}); MongoDB also has a lot of functions to use, such as sorting, statistics, etc., please refer to the original text.

MongoDB currently does not have or (or) operators, can only be replaced by a workaround, you may refer to the following links:

Http://www.mongodb.org/display/DOCS/OR+operations+in+query+expressions

MongoDB query syntax (greater than, less than, greater than or equal to, less than or equal to, and so on)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.