A brief talk on query _mongodb in MongoDB

Source: Internet
Author: User
Tags comments mongodb

One of the biggest features of MongoDB is that it supports dynamic queries, just like traditional relational database queries, but its queries come in more flexible mode.

One, query Expression Objects: Querying an Expression object

The query expression document is also a Bson structure document, for example, we can use the following query statement to query all the records in the collection:
Db.users.find ({})
Here, the expression object is an empty document that matches all the records at the time of the query. Look again:

Copy Code code as follows:

Db.users.find ({' last_name ': ' Smith '})

Here, we will query for all document records with the "Last_Name" property value "Smith".

Second, query options

In addition to query expression surprises, MongoDB also supports some additional parameter options. For example, we might just want to return some specific field values:

Copy Code code as follows:

Returns all fields except the Age field
> Db.user.find ({},{age:0});
Returns all columns except the comments of Tags=tennis
Db.posts.find ({tags: ' tennis '}, {comments:0});
Returns the Name field of Userid=16
> Db.user.find ({userid:16},{name:1});
{"_id": "Name": "User16"}
Returns all z fields for X=john
Db.things.find ({x: "John"}, {z:1});

Note: The _id field will always be returned, even if it is not explicitly specified

Third, the inquiry condition

1), <=, >=

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

2) $all

$all operations are similar to $in operations, but the $all operation requires that the values in the array be included in the returned records, such as:

> Use test; 
Switched to DB Test 

> Db.things.insert ({a:[1,2,3]}); 

> Db.things.find ();             
{"_id": ObjectId ("4de73360059e7f4bdf907cfe"), "a": [1, 2, 3]} 

> Db.things.find ({a:{$all: [2,3]}}); 
{"_id": ObjectId ("4de73360059e7f4bdf907cfe"), "a": [1, 2, 3]} 

> Db.things.find ({a:{$all: [1,2,3]}}); 
{"_id": ObjectId ("4de73360059e7f4bdf907cfe"), "a": [1, 2, 3]} 

> Db.things.find ({a:{$all: [1]}});    
{"_id": ObjectId ("4de73360059e7f4bdf907cfe"), "a": [1, 2, 3]} 

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

3) $exists

$exists action checks whether a field exists, such as:

> for (var i=0;i<1000;i++) Db.user.save ({_id:i,name: ' user ' +i,userid:i,age:20}); 
 
Contains userid 
> Db.user.find ({userid:{$exists: true}}). limit (5); 
{"_id": 0, "name": "User0", "userid": 0, "age": 
{"_id": 1, "name": "User1", "userid": 1, "Age": 
{"_id": 2, "name": "User2", "userid": 2, "age": 
{"_id": 3, "name": "User3", "userid": 3, "age": 
{"_id": 4, "name": "User4", "userid": 4, "Age": 

Does not contain sex field 
> Db.user.find ({sex:{$exists: false}}). limit (5);   

4) $mod

$mod operation allows us to simply take a modulo operation without needing to use a WHERE clause, such as:

WHERE clause 
> Db.user.find ("this._id%10==1"). Limit (5); 
{"_id": 1, "name": "User1", "userid": 1, "Age": 
{"_id": One, "name": "User11", "userid": One, "Age": 
{"_id": "Name": "User21", "userid": ", Age" 
: {"_id": "Name": "User31", "userid": "," Age ": 
" {"_id": "Name": "User41", "userid": "A", "Age": 

$mod Action 
> Db.user.find ({_id:{$mod: [10,1]}}). limit (5); 
{"_id": 1, "name": "User1", "userid": 1, "Age": 
{"_id": One, "name": "User11", "userid": One, "Age": 
{"_id": "Name": "User21", "userid": ", Age" 
: {"_id": "Name": "User31", "userid": "," Age ": 
" {"_id": "A", "name": "User41", "userid": A, "Age": 20} 

5) $ne

$ne mean not equal is not equal to, needless to say, see examples:

> Db.user.find (). Limit (5); 
{"_id": 0, "name": "User0", "userid": 0, "age": 
{"_id": 1, "name": "User1", "userid": 1, "Age": 
{"_id": 2, "name": "User2", "userid": 2, "age": 
{"_id": 3, "name": "User3", "userid": 3, "age": 
{"_id": 4, "name": "User4", "userid": 4, "Age": 

> Db.user.find ({_id:{$ne: 0}}). limit (5); 
{"_id": 1, "name": "User1", "userid": 1, "Age": 
{"_id": 2, "name": "User2", "userid": 2, "age": 
{"_id": 3, "name": "User3", "userid": 3, "age": 
{"_id": 4, "name": "User4", "userid": 4, "Age": 
{"_id": 5, "name": "User5", "userid": 5, "Age": 20}

6) $in

$in operation is similar to in, see examples in traditional relational databases:

The database has an array corresponding to the records 
> Db.user.find ({_id:{$in: [2,3,4,5,6]}}). limit (5); 
{"_id": 2, "name": "User2", "userid": 2, "age": 
{"_id": 3, "name": "User3", "userid": 3, "age": 
{"_id": 4, "name": "User4", "userid": 4, "Age": 
{"_id": 5, "name": "User5", "userid": 5, "age": 
{"_id": 6, "name": "User6", "userid": 6, "Age": 

Because there are no _id=1111 records 
> Db.user.find ({_id:{$in: [2,3,4,5,1111]}}) in the database. Limit (5); 
{"_id": 2, "name": "User2", "userid": 2, "age": 
{"_id": 3, "name": "User3", "userid": 3, "age": 
{"_id": 4, "name": "User4", "userid": 4, "Age": 
{"_id": 5, "name": "User5", "userid": 5, "Age": 20}

7) $nin

$nin In contrast to $in operations, see examples:

Deduct _ID=1/2/3/4 Records 
> Db.user.find ({_id:{$nin: [1,2,3,4]}}). limit (5);     
{"_id": 0, "name": "User0", "userid": 0, "age": 
{"_id": 5, "name": "User5", "userid": 5, "age": 
{"_id": 6, "name": "User6", "userid": 6, "Age": 
{"_id": 7, "name": "User7", "userid": 7, "Age": 
{"_id": 8, "name": "User8", "userid": 8, "Age": 20} 

8) $nor, $or

$nor contrary to $or, it is difficult to explain, see examples:

> Db.user.find ({$nor: [{_id:2},{name: ' User3 '},{userid:4}]}). Limit (5);   
{"_id": 0, "name": "User0", "userid": 0, "age": 
{"_id": 1, "name": "User1", "userid": 1, "Age": 
{"_id": 5, "name": "User5", "userid": 5, "age": 
{"_id": 6, "name": "User6", "userid": 6, "Age": 
{"_id": 7, "name": "User7", "userid": 7, "Age":

> Db.user.find ({$or: [{_id:2},{name: ' User3 '},{userid:4}]}). Limit (5); 
{"_id": 2, "name": "User2", "userid": 2, "age": 
{"_id": 3, "name": "User3", "userid": 3, "age": 
{"_id": 4, "name": "User4", "userid": 4, "Age": 20} 

The above mentioned is the entire content of this article, I hope you can enjoy.

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.