grammar:
db.collection.find(query, projection)
- Parameter Description
- query : optional, use query operators to specify query conditions
- projection : optional, use the projection operator to specify the returned key. All the key values in the document will be returned during the query. You only need to omit this parameter (default is omitted).
format:
> db.demo.find().pretty()
Operator query:
- Greater than: (greater than>) $ gt
- Greater than or equal to: (gt equal> =) $ gte
- Less than: (less than <) $ lt
- Less than or equal to: (lt equal <=) $ lte
- Match: (equal =) $ eq
- Mismatch: (not equal! =) $ Ne
- Contains: (like) / val1 /
- Beginning: (like) / ^ val1 /
- End: (like) / val1 $ /
MongoDB and RDBMS Where statement comparison
If you are familiar with regular SQL data, the following table can better understand MongoDB's conditional query:
operating | format | example | Similar statements in RDBMS |
---|---|---|---|
more than the | {<key>:{$gt:<val>}} |
db.demo.find({"age":{$gt:20}}).pretty() |
where age > 20 |
greater than or equal to | {<key>:{$gte:<val>}} |
db.demo.find({"age":{$gte:20}}).pretty() |
where age >= 20 |
Less than | {<key>:{$lt:<val>}} |
db.demo.find({"age":{$lt:20}}).pretty() |
where age < 20 |
less than or equal to | {<key>:{$lte:<val>}} |
db.demo.find({"age":{$lte:20}}).pretty() |
where age <= 20 |
equal | {<key>:<val> } |
db.demo.find({"name":"yourName"}).pretty() |
where name = 'yourName' |
not equal to | {<key>:{$ne:<val>}} |
db.demo.find({"age":{$ne:20}}).pretty() |
where age != 20 |
and query:
> db.demo.find({key1:val1, key2:val2}).pretty()
or query:
> db.demo.find({$or: [{key1: val1}, {key2:val2}]}).pretty()
and, or query:
> db.demo.find({"key1": "val1", $or: [{"key2": "val2"},{"key3": "val3"}]}).pretty()
$ type query
Types of | digital | Remarks |
---|---|---|
Double | 1 | |
String | 2 | |
Object | 3 | |
Array | 4 | |
Binary data | 5 | |
Undefined | 6 | Obsolete. |
Object id | 7 | |
Boolean | 8 | |
Date | 9 | |
Null | 10 | |
Regular Expression | 11 | |
JavaScript | 13 | |
Symbol | 14 | |
JavaScript (with scope) | 15 | |
32-bit integer | 16 | |
Timestamp | 17 | |
64-bit integer | 18 | |
Min key | 255 | Query with -1 . |
Max key | 127 |
Query the data whose name is String
db.demo.find({"name" : {$type : 2}})
或
db.demo.find({"name" : {$type : 'string'}})
> db.demo.find()
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "yourName", "age" : 20, "hobby" : [ "drawing", "run" ] }
>
> db.demo.find({"name":{$type:2}})
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "yourName", "age" : 20, "hobby" : [ "drawing", "run" ] }
>
> db.demo.find({"name":{$type:'string'}})
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "yourName", "age" : 20, "hobby" : [ "drawing", "run" ] }
>
> db.demo.find({"name":{$type:3}})
>
limit (limit number of queries)
> db.COLLECTION_NAME.find().limit(NUMBER)
skip (number of skipped data)
> db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
sort
> db.COLLECTION_NAME.find().sort({KEY:1})
> db.demo.find()
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "hisName", "age" : 20, "hobby" : [ "drawing", "run" ] }
{ "_id" : ObjectId("5e79a878a3edd4aa4ff0c2c4"), "name" : "yourName", "age" : "30", "hobby" : [ "drawing", "run" ] }
>
> db.demo.find().sort({"age":1})
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "hisName", "age" : 20, "hobby" : [ "drawing", "run" ] }
{ "_id" : ObjectId("5e79a878a3edd4aa4ff0c2c4"), "name" : "yourName", "age" : "30", "hobby" : [ "drawing", "run" ] }
>
> db.demo.find().sort({"age":-1})
{ "_id" : ObjectId("5e79a878a3edd4aa4ff0c2c4"), "name" : "yourName", "age" : "30", "hobby" : [ "drawing", "run" ] }
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "hisName", "age" : 20, "hobby" : [ "drawing", "run" ] }
>
> db.demo.find().sort({"name":-1})
{ "_id" : ObjectId("5e79a878a3edd4aa4ff0c2c4"), "name" : "yourName", "age" : "30", "hobby" : [ "drawing", "run" ] }
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "hisName", "age" : 20, "hobby" : [ "drawing", "run" ] }
>
> db.demo.find().sort({"name":1})
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "hisName", "age" : 20, "hobby" : [ "drawing", "run" ] }
{ "_id" : ObjectId("5e79a878a3edd4aa4ff0c2c4"), "name" : "yourName", "age" : "30", "hobby" : [ "drawing", "run" ] }
>