Delete the student first, then reinsert the data
>Db.student.drop ()true> Db.student.insert ([{"_id": 1, "name": "Zhangsan", "age": +, "Sex": 1}, {"_id": 2, "name": "Lisi", "Age": 27} {"_id": 3, "name": "Wangwu", "Age": $}, {"_id": 4, "name": "Zhaoliu", "Age":-}, {"_id": 5, "name": "Qianli U "," Age ": +}, {" _id ": 6," name ":" Sunba "," Age ": 32}]) Bulkwriteresult ({"Writeerrors" : [ ], "Writeconcernerrors" : [ ], "Ninserted": 6, "Nupserted": 0, "Nmatched": 0, "Nmodified": 0, "Nremoved": 0, "Upserted" : [ ]})>Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 27 }{ "_id": 2, "name": "Lisi", "age": 27 }{ "_id": 3, "name": "Wangwu", "Age": 30 }{ "_id": 4, "name": "Zhaoliu", "Age": 28 }{ "_id": 5, "name": "Qianliu", "Age": 33 }{ "_id": 6, "name": "Sunba", "Age": 32}
1. Query the specified key
Db. Collection name. Find ({query condition},{specified key})
Specify key: 1 for display, 0 for not 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.student.find ({},{_id:0, name:1}){ "Name": "Zhangsan" }{ "Name": "Lisi" }{ "Name": "Wangwu" }{ "Name": "Zhaoliu" }{ "Name": "Qianliu" }{ "Name": "Sunba"}
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, "name": "Zhaoliu", "Age": > Db.student.find ({age:{$ne: "_id": 3, "name": "Wangwu", "Age": "_id": 4, "name": "Zhaoliu", "Age":"_id": 5, "name": "Q Ianliu "," Age ":" _id ": 6," name ":" Sunba "," Age ": 32}
$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,28"_id": 3, "name": "Wangwu", "Age":"_id": 5, "name": "Qianliu", "Age": "_id" ": 6," "Name": "Sunba", "Age": 32}
$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":"_id": 6, "name": "Sunba", "Age": 32}
Null: null value
Null"_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 ": 32}
$type: The key is of some kind
Double:1
String:2
...
> Db.student.insert ({_id:7, name:7, age:70"ninserted": 1 })> Db.student.find ({name: {$type: 2< c2>"_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" _id ": 7," "Name": 7, "Age": 70}
Regular expressions
> Db.student.find ({name:/si\b/"_id": 2, "name": "Lisi", "Age": 27}
Db. Collection name. FindOne ({query condition},{specified key})
Query the first data that meets the criteria
>"_id": 1, "name": "Zhangsan", "age": +, "Sex": 1}
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": " Lisi "," age ":" _id ": 3," name ":" Wangwu "," Age ": 30}
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": "_id": 7 , "name": 7, "Age": 70}
Paging can be implemented using limit () and Skip ()
> Db.student.find (). Skip (0). Limit (3"_id": 1, "name": "Zhangsan", "Age":, "Sex": 1"_id": 2, "n Ame ":" 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" _id ": 7," name ": 7," Age ": 70}
Db. Collection name. Find (). Sort ({key: number})
A number of 1 means ascending, and a number of 2 means descending
> Db.student.find (). Sort ({age:1}){ "_id": 1, "name": "Zhangsan", "age": +, "sex": 1 }{ "_id": 2, "name": "Lisi", "age": 27 }{ "_id": 4, "name": "Zhaoliu", "Age": 28 }{ "_id": 3, "name": "Wangwu", "Age": 30 }{ "_id": 6, "name": "Sunba", "Age": 32 }{ "_id": 5, "name": "Qianliu", "Age": 33 }{ "_id": 7, "name": 7, "age": 70 }> Db.student.find (). Sort ({age:1, _id:-1}){ "_id": 2, "name": "Lisi", "age": 27 }{ "_id": 1, "name": "Zhangsan", "age": +, "sex": 1 }{ "_id": 4, "name": "Zhaoliu", "Age": 28 }{ "_id": 3, "name": "Wangwu", "Age": 30 }{ "_id": 6, "name": "Sunba", "Age": 32 }{ "_id": 5, "name": "Qianliu", "Age": 33 }{ "_id": 7, "name": 7, "Age": 70}
MongoDB Quick Start Learning Note 4 MongoDB document query operation