Get the App
SLTechnology News&Howtos  ›  Database  › 

3.mongodb query

Shulou Source: shulou.com Published: 2022-06-01 16:10:29 10月27日 Update

MongoDB queries documents using the find () method.

The find () method displays all documents in an unstructured way.

The syntax format is as follows:

Db.collection.find (query, projection)

Query: optional, use the query operator to specify query conditions

Projection: optionally, use the projection operator to specify the returned key. When querying, all the key values in the document are returned, simply omitting the parameter (default omitted).

# after adding the pretty method to find, you can also display it in a format that is easier to read:

Db.tmp.find ()

{"_ id": ObjectId ("5c1757c7f4e84940e0bd578b"), "name": "aaa", "pwd": "123"," birthday ": ISODate (" 1995-09-05T00:00:00Z ")}

{"_ id": ObjectId ("5c1757f5f4e84940e0bd578c"), "name": "bbb", "pwd": "321", "birthday": ISODate ("1999-10-10T00:00:00Z")}

{"_ id": ObjectId ("5c177b84f4e84940e0bd578d"), "name": "suhaozhi", "pwd": "suhaozhi", "phone": [{"phone_num": "13333333333", "type": "cmcc"}, {"phone_num": 1555555555, "type": "chinanet"}], "type": "chinanet"}

{"_ id": ObjectId ("5c177ba1f4e84940e0bd578e"), "name": "suhaozhi", "pwd": "su", "phone": [{"phone_num": "13333333332", "type": "cmcc"}, {"phone_num": 15655555555, "type": "cnnet"}]}

{"_ id": ObjectId ("5c178a62b1d327866c98c9f7"), "x": "y"}

{"_ id": ObjectId ("5c1f226c87756129d06d2718"), "username": "aaa", "phone_num": "13333333333"}

{"_ id": ObjectId ("5c1f232587756129d06d2719"), "username": "bbb", "phone_num": ["123,456"]}

Mgset-6039947:PRIMARY > db.tmp.find (). Pretty ()

{

"_ id": ObjectId ("5c1757c7f4e84940e0bd578b")

"name": "aaa"

Pwd: 123,

Birthday: ISODate ("1995-09-05T00:00:00Z")

}

{

"_ id": ObjectId ("5c1757f5f4e84940e0bd578c")

"name": "bbb"

Pwd: 321

Birthday: ISODate ("1999-10-10T00:00:00Z")

}

{

"_ id": ObjectId ("5c177b84f4e84940e0bd578d")

"name": "suhaozhi"

"pwd": "suhaozhi"

"phone": [

{

"phone_num": "13333333333"

"type": "cmcc"

}

{

"phone_num": 155555555555

"type": "chinanet"

}

]

"type": "chinanet"

}

{

"_ id": ObjectId ("5c177ba1f4e84940e0bd578e")

"name": "suhaozhi"

"pwd": "su"

"phone": [

{

"phone_num": "13333333332"

"type": "cmcc"

}

{

"phone_num": 15655555555

"type": "cnnet"

}

]

}

{"_ id": ObjectId ("5c178a62b1d327866c98c9f7"), "x": "y"}

{

"_ id": ObjectId ("5c1f226c87756129d06d2718")

"username": "aaa"

"phone_num": "13333333333"

}

{

"_ id": ObjectId ("5c1f232587756129d06d2719")

"username": "bbb"

"phone_num": [

"123"

"456"

]

}

Query criteria:

If you are familiar with regular SQL data, you can better understand MongoDB conditional queries through the following table:

The similar statement in the operation format example RDBMS is equal to {:} db.col.find ({"by": "Rookie tutorial"}). Pretty () where by = 'Rookie tutorial' is less than {: {$lt:}} db.col.find ({"likes": {$lt:50}) .pretty () where likes

< 50小于或等于{:{$lte:}}db.col.find({"likes":{$lte:50}}).pretty()where likes 50大于或等于{:{$gte:}}db.col.find({"likes":{$gte:50}}).pretty()where likes >

= 50 is not equal to {: {$ne:}} db.col.find ({"likes": {$ne:50}}) .pretty () where likes! = 50

AND conditional query:

The find () method of MongoDB can pass in multiple keys (key), each key separated by a comma, which is the AND condition of a regular SQL.

Example:

Db.col.find ({key1:value1, key2:value2}) .pretty ()

Or conditional query:

The MongoDB OR conditional statement uses the keyword $or and the syntax format is as follows:

> db.col.find (

{

$or: [

{key1: value1}, {key2:value2}

]

}

). Pretty ()

And is used in conjunction with or:

The following example demonstrates the combined use of AND and OR, similar to the regular SQL statement: 'where age

Tags: Queries conditions documents formatting methods statements general fields operators tutorials rookie tutorials grammar rookie unions keywords keywords parameters just multiple instances Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux Xiaomi Redmi Microsoft