1. Preparatory work
(1) Start MONGO
Enter the bin/directory of the MONGO installation directory,./mongod
(2) Start MONGO client
./mongo
(3) View all libraries
Show DBS;
(4) Switch to LVYF Library
Use LVYF
(5) View all collections
Show collections;
(6) Check data from a collection
Db.first_col.find ();
2. Enquiry
(1) View all, pretty () function is to format the JSON data
Db.collection_name.find (). pretty ();
>"_id": ObjectId ("575e879380d09b176a58ce64"), "name": "Lvyafei", "age": +, "sex": "M", "Address": "http: www.lvyf.com "_id": ObjectId ("575e880980d09b176a58ce65"), "name": "Chenjl", "Age": $, "Sex": "W", "address ":" http://www.chenjl.com "" _id ": ObjectId (" 575e889880d09b176a58ce66 ")," name ":" Child "," Age ": 2," Sex ":" M " , "Address": "Http://www.child.com", "status": "Cry and Laugh"}
(2) Conditional query
Operation |
Format |
Example |
The corresponding SQL statement |
Equals |
{Key:value} |
{NAME:LVYF} |
where Name= ' Lvyf ' |
Less than |
{key: {<:value}} |
{age: {<:20}} |
Where Age < 20 |
Less than or equal to |
{key: {<e:value}} |
{age: {<e:20}} |
where Age <=20 |
Greater than |
{key: {>:value}} |
{age: {>:3}} |
Where Age > 3 |
Greater than or equal to |
{key: {>e:vlaue}} |
{age: {>e:3}} |
where Age >=3 |
Not equal to |
{key: {&ne:value}} |
{name: {&NE:LVYF}} |
WHERE name! = ' LVYF ' |
(3) and conditions
Query records based on gender and name, corresponding SQL statement where name= ' Lvyafei ' and sex = ' M '
Db.first_col.find ({name: "Lvyafei", Sex: "M"}). Pretty (){ "_id": ObjectId (" 575e879380d09b176a58ce64 "), " name ":" Lvyafei ", " age ": +, " sex ":" M " , " Address ":" Http://www.lvyf.com "}
(4) or condition, grammar
Db.collection_name.find ({$or: [{key1:value1}, {key2:value2}] });
Search examples by name or gender:
>db.first_col.find ({$or: [{name: ' Lvyafei '},{sex: ' M '}]})' _id ': ObjectId (" 575e879380d09b176a58ce64 ")," name ":" Lvyafei "," age ": +," sex ":" M "," Address ":" http://www.lvyf.com "" _id ": ObjectId ("575e889880d09b176a58ce66"), "name": "Child", "Age": 2, "Sex": "M", "Address": "Http://www.child.com", "stat US ":" Cry and Laugh "}
(5) combined with and and or, equivalent to where age >= 3 and (name= ' Lvyafei ' or sex= ' M ')
Db.first_col.find ({age: {$gte: 3}, $or: [{name: ' Lvyafei '}, {set: ' M '}]}). Pretty (){ "_ ID ": ObjectId (" 575e879380d09b176a58ce64 "), " name ":" Lvyafei ", " age ": "Sex": "M", "address": "Http://www.lvyf.com"}
(6) greater than and less than union query, equivalent to where age > 3 and Age < 21
Db.first_col.find ({"Age": {$lt:, $GT: 3}})"_id": ObjectId ("575e880980d09b176a58ce65"), "name": " Chenjl "," age ": +," sex ":" W "," Address ":" Http://www.chenjl.com "}
(7) $type operator
If you want to query that name is a string type of record
> Db.first_col.find ({name: {$type: 2}});" _id ": ObjectId (" 575e879380d09b176a58ce64 ")," name ":" Lvyafei "," age ": +," sex ":" M "," Address ":" Http://www.lvyf.com "_id": ObjectId ("575e880980d09b176a58ce65"), "name": "Chenjl", "age": +, "sex": "W", "Address": "Http://ww w.chenjl.com "_id": ObjectId ("575e889880d09b176a58ce66"), "name": "Child", "Age": 2, "Sex": "M", "Address": "Http://www.child.com", "status": "Cry and Laugh"}
mongdb--3. Query operations