Query statements
Environment Preparation:
> Use MyDB
Switched to DB MyDB
> db.createcollection (' User ')
{"OK": 1}
>
>
> Db.user.insert ({name: ' Niuh ', age:11})
> Db.user.insert ({name: ' KK ', age:22})
>
>
> Db.post.find
Find () statement
Find () Usage: db. Collection_name.find ()
Here you can see that each document automatically creates a _id property that is useful in the logical relationship between the documents you see above
Db.user.find ()
{"_id": ObjectId ("58e761399a538671f6972318"), "name": "Niuh", "Age":
11}
{"_id": ObjectId ("58e7614b9a538671f6972319"), "name": "KK", "Age": 2
2}
>
And in MongoDB
When multiple key-value pairs are passed in Find (), MongoDB handles them as an and query. Usage: Db.mycol.find ({key1:value1, key2:value2}). Pretty ()
Example: Db.user.find ({name: ' KK '})
Or: Db.user.find ({name: ' KK ', age:22})
The Oror in MongoDB
In MongoDB, OR query statements with $or as keywords, use the following:
Find data with name equal to Niuh or age equal to 22
Db.user.find ({$or: [{name: ' Niuh '},{age:22}]})
{"_id": ObjectId ("58e761399a538671f6972318"), "name": "Niuh", "Age":
11}
{"_id": ObjectId ("58e7614b9a538671f6972319"), "name": "KK", "Age": 2
2}
Use both and and OR
"MongoDB" Learning note _02