Discussion on mongodb query query

  • 2020-05-24 06:24:52
  • OfStack

One of the biggest features of Mongodb is that it supports dynamic queries, just like traditional relational database queries, but its queries are more flexible.

1. Query Expression Objects: query expression object

The query expression document is also an BSON structure document. For example, we can query all records in the collection with the following query statement:
db.users.find({})
Here, the expression object is an empty document that matches all the records in the query. Then look at:


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

Here, we will query for all documents with an "last_name" property value of "Smith".

2. Query options

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


// Return in addition to age All fields except fields
> db.user.find({},{age:0});
// return tags=tennis In addition to comments Of all the columns
db.posts.find( { tags : 'tennis' }, { comments : 0 } );
// return userid=16 the name field
> db.user.find({userid:16},{name:1});
{ "_id" : 16, "name" : "user16" }
// return x=john All of the z field
db.things.find( { x : "john" }, { z : 1 } );

Note: the _id field is always returned, even if it is not explicitly specified

3. Query conditions

1) < , < =, > , > =


//  Is greater than : field > value 
db.collection.find({ "field" : { $gt: value } } );  
 
// Less than: field < value 
db.collection.find({ "field" : { $lt: value } } );
  
// Greater than or equal to : field >= value 
db.collection.find({ "field" : { $gte: value } } ); 
 
// Less than or equal to: field<=value 
db.collection.find({ "field" : { $lte: value } } ); 
 

2) $all

The $all operation is similar to the $in operation, except that the $all operation requires that all the values in the array be included in the returned record, 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

The $exists operation 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" : 20 } 
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 } 
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 } 
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 } 
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 } 

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

4) $mod

The $mod operation allows us to simply extract the module without using the where clause, such as:


//where clause  
> db.user.find("this._id%10==1").limit(5); 
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 } 
{ "_id" : 11, "name" : "user11", "userid" : 11, "age" : 20 } 
{ "_id" : 21, "name" : "user21", "userid" : 21, "age" : 20 } 
{ "_id" : 31, "name" : "user31", "userid" : 31, "age" : 20 } 
{ "_id" : 41, "name" : "user41", "userid" : 41, "age" : 20 } 

//$mod operation  
> db.user.find({_id:{$mod:[10,1]}}).limit(5); 
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 } 
{ "_id" : 11, "name" : "user11", "userid" : 11, "age" : 20 } 
{ "_id" : 21, "name" : "user21", "userid" : 21, "age" : 20 } 
{ "_id" : 31, "name" : "user31", "userid" : 31, "age" : 20 } 
{ "_id" : 41, "name" : "user41", "userid" : 41, "age" : 20 } 

5) $ne

$ne means not equal, not equal to, needless to say, look at the example:


> db.user.find().limit(5); 
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 } 
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 } 
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 } 
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 } 
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 } 

> db.user.find({_id:{$ne:0}}).limit(5); 
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 } 
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 } 
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 } 
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 } 
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }

6) $in

The $in operation is similar to IN in a traditional relational database, see example:


// The database has records for all arrays  
> db.user.find({_id:{$in:[2,3,4,5,6]}}).limit(5); 
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 } 
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 } 
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 } 
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 } 
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 } 

// Because it's not in the database _id=1111 The record of  
> db.user.find({_id:{$in:[2,3,4,5,1111]}}).limit(5); 
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 } 
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 } 
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 } 
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }

7) $nin

$nin is the opposite of $in. For example:


// deducted _id=1/2/3/4 The record of  
> db.user.find({_id:{$nin:[1,2,3,4]}}).limit(5);     
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 } 
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 } 
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 } 
{ "_id" : 7, "name" : "user7", "userid" : 7, "age" : 20 } 
{ "_id" : 8, "name" : "user8", "userid" : 8, "age" : 20 } 

$$or nor, 8)

$nor is the opposite of $or. It's hard to explain.


> db.user.find({$nor:[{_id:2},{name:'user3'},{userid:4}]}).limit(5);   
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 } 
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 } 
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 } 
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 } 
{ "_id" : 7, "name" : "user7", "userid" : 7, "age" : 20 }

> db.user.find({$or:[{_id:2},{name:'user3'},{userid:4}]}).limit(5); 
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 } 
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 } 
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 } 

That's all for this article, I hope you enjoy it.


Related articles: