MongoDB Introduction
MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.
MongoDB is a product between relational database and non relational database, and is the most powerful and relational database in the relational database.
Here are some examples of MongoDB query document operations
Delete the student first, then reinsert the data
> Db.student.drop ()
true
> Db.student.insert ([{"_id": 1, "name": "Zhangsan", "Age": "," Sex ": 1}, {" _i D ": 2," name ":" Lisi "," Age ":" n ", {" _id ": 3," name ":" Wangwu "," age ": {" _id ": 4," name ":" Zhaoliu "," Age ": }, {"_id": 5, "name": "Qianliu", "Age": 6}, {"_id":, "name": "Sunba", "Age":}])
Bulkwriteresult ({
"Writeerrors": [],
"writeconcernerrors": [],
"ninserted": 6,
"nupserted": 0,
"nmatched": 0,
" Nmodified ": 0,
" nremoved ": 0,
" upserted ": []
})
> Db.student.find ()
{" _id ": 1," name ": ' Zhangsan ', ' age ':
{' _id ': 2, ' name ': ' Lisi ', ' age ': '
{' _id ': 3, ' name ': ' Wangwu ', ' Age ': 30}
{"_id": 4, "name": "Zhaoliu", "Age":}
{"_id": 5, "name": "Qianliu", "Age": 33}
1, the query specified key
Db. Collection name. Find ({query criteria},{specified key})
Specified key: 1 for display, 0 for no display, _id default display
> Db.student.find ({},{name:1})
{"_id": 1, "name": "Zhangsan"}
{"_id": 2, "name": "Lisi"}
{"_id": 3, "name": "Wangwu"}
{"_id": 4, "name": "Zhaoliu"}
{"_id": 5, "name": "Qianliu"}
{"_id": 6, "name" : "Sunba"}
> Db.student.find ({},{_id:0, age:0})
{"Name": "Zhangsan", "Sex": 1}
{"name": "Lisi"}
{' name ': ' Wangwu '}
{' name ': ' Zhaoliu '}
{' name ': ' Qianliu '}
{' name ': ' Sunba '}
> Db.st Udent.find ({},{_id:0, name:1}) {"name": "
Zhangsan"}
{"name": "Lisi"}
{"name": "Wangwu"}
{"NA" Me ': ' Zhaoliu '}
{' name ': ' Qianliu '}
2. Various inquiry methods
$lt:<
$lte: <=
$gt:>
$gte: >=
$ne:!=
> Db.student.find ({age:{$lt:}})
{"_ ID ": 1," name ":" Zhangsan "," Age ":" Sex ": 1}
{" _id ": 2," name ":" Lisi "," age ":
{" _id ": 4," Nam E ': ' Zhaoliu ', ' age ':
> Db.student.find ({age:{$ne:}})
{"_id": 3, "name": "Wangwu", "Age":}
{"_id": 4, "name": "Zhaoliu", "Age": [}
{"_id": 5, "name": "Qianliu", "Age": 33}
$in: Contains
$nin: does not contain
> Db.student.find ({age:{$in: [27,28]}})
{"_id": 1, "name": "Zhangsan", "Age":, "Sex": 1}
{"_id": 2, ' Name ': ' Lisi ', ' Age ':}
{' _id ': 4, ' name ': ' Zhaoliu ', ' age ':
> Db.student.find ({age:{$nin: [27 , []}}}}
{"_id": 3, "name": "Wangwu", "age":
{"_id": 5, "name": "Qianliu", "Age": 33}
$or: Or
> Db.student.find ({$or: [{age:{$lt:}}, {name: "Sunba"}}})
{"_id": 1, "name": "Zhangsan", "Age": "," Sex ": 1}
{"_id": 2, "name": "Lisi", "Age":}
{"_id": 4, "name": "Zhaoliu", "Age": 28}
Null: null value
> Db.student.find ({sex:null})
{"_id": 2, "name": "Lisi", "Age": "
{_id": 3, "name": "Wangwu", "Age" :
{"_id": 4, "name": "Zhaoliu", "Age": [}
{"_id": 5, "name": "Qianliu", "Age": 33}
$type: The key is of some type
Double:1
String:2
...
> Db.student.insert ({_id:7, name:7, age:70})
Writeresult ({"ninserted": 1})
> Db.student.find ({name: {$ Type:2}})
{"_id": 1, "name": "Zhangsan", "Age": ", Sex": 1}
{"_id": 2, "name": "Lisi", "Age":}
{"_id": 3, "name": "Wangwu", "age":
{"_id": 4, "name": "Zhaoliu", "age":
{"_id": 5, "name": ' Qianliu ', ' age ':
{' _id ': 6, ' name ': ' Sunba ', ' age ':
> Db.student.find ({name: {$type: 1}})
Regular expressions
> Db.student.find ({name:/si\b/})
Db. Collection name. FindOne ({query condition},{specified key})
Query the first data that meets the criteria
Db. Collection name. Find ({query condition},{specified key}). Limit (number)
Query the first few data
> Db.student.find (). Limit (3)
{"_id": 1, "name": "Zhangsan", "Age":, "Sex": 1}
{"_id": 2, "name": "L ISI "," Age ": 27}
Db. Collection name. Find ({query condition},{specified key}). Skip (number)
Skip the first few data
> Db.student.find (). Skip (2)
{"_id": 3, "name": "Wangwu", "age":
{"_id": 4, "name": "Zhaoliu", "Age" :
{"_id": 5, "name": "Qianliu", "age":
{"_id": 6, "name": "Sunba", "Age": 32}
You can use limit () and skip () to implement paging
> Db.student.find (). Skip (0). Limit (3)
{"_id": 1, "name": "Zhangsan", "Age":, "Sex": 1}
{"_id": 2, "NA Me: "Lisi", "age":
{"_id": 3, "name": "Wangwu", "Age":
> Db.student.find (). Skip (3). Limit (3)
{"_id": 4, "name": "Zhaoliu", "Age": [}
{"_id": 5, "name": "Qianliu", "Age": +}
{"_id": 6, "name" : "Sunba", "Age":
> Db.student.find (). Skip (6). Limit (3)
Db. Collection name. Find (). Sort ({key: number})
The number is 1 for ascending, and the number is 2 for descending
> Db.student.find (). Sort ({age:1})
{"_id": 1, "name": "Zhangsan", "Age":, "Sex": 1}
{"_id": 2, "name" : ' Lisi ', ' age ': '
{_id ': 4, ' name ': ' Zhaoliu ', ' age ': '
{' _id ': 3, ' name ': ' Wangwu ', ' Age ': 30}
{"_id": 6, "name": "Sunba", "Age":
{"_id": 5, "name": "Qianliu", "Age":
{"_id": 7, "name": 7, "Age": M}
> Db.student.find (). Sort ({age:1, _id:-1})
{"_id": 2, "name": "Lisi", "Age": "
{_id": 1, "name": "En Angsan "," Age ":" Sex ": 1}
{" _id ": 4," name ":" Zhaoliu "," age ":
{" _id ": 3," name ":" Wangwu "," Age ':
{' _id ': 6, ' name ': ' Sunba ', ' age ': '
{' _id ': 5, ' name ': ' Qianliu ', ' Age ': 33}
The above content is small to introduce the MongoDB Quick Start Note (iv) MONGODB query document operation Instance code, I hope to help you!