db.collection.find(query_condition, projection_field):
query_condition:查询的条件,可以设置单个条件、多个条件(and、or)、对于field可以进行类似shell编程中的关系运算符,但是并不完全相同。例如:在shell中不小于即为:$ge ,而在mongoDB中需要使用$gte。
projection_field:设定返回的field;
???自动生成的ID是必须要进行设定说明,如果不设定是默认为1则返回,设定为0,则不返回;?
? ?其他filed,如果都不设置即默认为1进行全部返回,如果任何field设置为1,其他字段则默认为0,不返回。
关于查询条件使用的案例
- 关于关系运算符:
关于运算符的说明
关系运算符 | 说明 | $eq | 等于 | $ne | 不等于 | $gt | 大于 | $gte | 不小于 | $lt | 小于 | $lte | 不大于 | - 依赖于上面的顺序,现在举例说明
全量的文档:
> db.localfindb.localfidb.localfdb.localdb.local.find()db.local.find() { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" } >? 提取年龄=90的 >db.local.find({"age":{$eq:90}}) { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } >? > db.local.find({"age":90}) { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } >?
提取年龄不等于90的
> db.local.find({"age":{$ne:90}})db.local.find({"age":{$ne:90}}) { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" } >?
提取年龄大于90的
> db.local.find({"age":{$gt:90}})db.local.find({"age":{$gt:90}}) { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" } >?
提取年龄不小于90的
> db.local.find({"age":{$gte:90}})db.local.find({"age":{$gte:90}}) { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" } >? >?
提取年龄小于90的
> db.local.find({"age":{$lt:90}})db.local.find({"age":{$lt:90}}) { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } >?
提取年龄不大于90的
> db.local.find({"age":{$lte:90}})db.local.find({"age":{$lte:90}}) { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } >?
关于and条件的使用
全量文档如下:
> db.localfindb.localfidb.localfdb.localdb.local.find()db.local.find() { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
> db.local.find({$and:[{"age":100,"sex":"femal"}]}) { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } >? >db.local.find({"age":100,"sex":"femal"}) { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
关于or条件的使用
全量文档如下:
> db.localfindb.localfidb.localfdb.localdb.local.find()db.local.find() { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
> db.local.find({$or:[{"age":70},{"sex":"man"}]})db.local.find({$or:[{"age":70},{"sex":"man"}]}) { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } >? >?
关于and条件和or条件的联合使用
> db.local.find({"age":100,$or:[{"sex":"man"},{"sex":"femal"}]}) { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
关于查询字段的使用样例:
-
查询全部的文档全部的field:
> db.local.find() { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
> db.local.find({},{})db.local.find({},{}) { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" } >?
-
查询全部文档中的两个field(显示ID)
> db.local.find({},{"age":1,"sex":1} ?) { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "age" : 100 } { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "age" : 100, "sex" : "man" } { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d7"), "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f3deea30f42c635791d8"), "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f40dea30f42c635791d9"), "age" : 90, "sex" : "femal" } { "_id" : ObjectId("6139f41bea30f42c635791da"), "age" : 70, "sex" : "femal" } { "_id" : ObjectId("6139f42bea30f42c635791db"), "age" : 100, "sex" : "femal" } { "_id" : ObjectId("6139f446ea30f42c635791dc"), "age" : 110, "sex" : "femal" }
-
查询全部文档中的两个field(不显示ID) >db.local.find({},{"_id":0,"age":1,"sex":1})
{ "age" : 100 }
{ "age" : 100, "sex" : "man" }
{ "age" : 100, "sex" : "femal" }
{ "age" : 100, "sex" : "femal" }
{ "age" : 100, "sex" : "femal" }
{ "age" : 90, "sex" : "femal" }
{ "age" : 70, "sex" : "femal" }
{ "age" : 100, "sex" : "femal" }
{ "age" : 110, "sex" : "femal" }
>
|