MongoDB 查询文档使用 find()
方法。find()
方法以非结构化的方式来显示所有文档。
db.collection.find()语法
db.collection.find( , )
- query filter 查询条件,指明返回哪些文档
- projection 查询映射,指定查询的文档要返回哪些字段
为空的时候就是{},可以省略。所以db.collection.find()等价于db.collection.find({})
接着上一篇的实战,我们先来看一下users集合现在还有哪一些文档
> db.users.find()
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
{ "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }
下面我们通过命令来演示find()
方法的用法
1.查询符合条件的文档
如:查询age>25的全部文档
> db.users.find({"age": {$gt: 25}})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }
2.查询符合条件的文档的指定字段
如:查询age>25的人的name
> db.users.find({"age": {$gt: 25}}, {name:1})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy" }
如我们所愿返回了
name
字段,但是同时也返回了_id
字段。_id
是默认返回的,如果不想要它返回许指定_id
:0
> db.users.find({"age": {$gt: 25}}, {name:1, _id:0})
{ "name" : "Jack" }
{ "name" : "Lucy" }
3.指定AND条件
如:查询年龄大于25的男性
> db.users.find({"age": {$gt: 25}, "gender": "male"})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
4.指定OR条件
如:查询age=18的或者name是tom的文档
> db.users.find({$or: [{"name":"tom"}, {"age":18}]})
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
5.指定AND和OR条件
如一下面的筛选满足2个条件
- 年龄大于18
- 女性或者名字叫"Jack"
> db.users.find({"age":{$gt: 18}, $or:[{"gender":"female"}, {"name":"Jack"}]})
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }
如果某一个字段对应的是数组,我们可以修改数组里面的某一个字段吗?
先给每一个文档插入一个字段points,它对应的值是个数组
> var points={[ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ]}
> db.users.updateMany({"age":{$gt:0}}, {$set:{"points":points}})
{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
> tmp=[{"points":90, "bonus":20}, {"points":88, "bonus":11}]
[
{
"points" : 90,
"bonus" : 20
},
{
"points" : 88,
"bonus" : 11
}
]
> db.users.update({"name": "tom"}, {$set: {"points": tmp}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.users.find().pretty()
> db.users.find().pretty()
{
"_id" : ObjectId("5c77499137955b945af73220"),
"name" : "tom",
"age" : 20,
"gender" : "male",
"points" : [
{
"points" : 90,
"bonus" : 20
},
{
"points" : 88,
"bonus" : 11
}
]
}
{
"_id" : ObjectId("5c77499137955b945af73221"),
"name" : "Linda",
"age" : 18,
"gender" : "female",
"points" : [
{
"points" : 85,
"bonus" : 20
},
{
"points" : 85,
"bonus" : 10
}
]
}
{
"_id" : ObjectId("5c77731d37955b945af73222"),
"name" : "Hanmeimei",
"age" : 17,
"gender" : "female",
"points" : [
{
"points" : 85,
"bonus" : 20
},
{
"points" : 85,
"bonus" : 10
}
]
}
{
"_id" : ObjectId("5c7774b736397a1c0aa425ea"),
"name" : "Jack",
"age" : 28,
"gender" : "male",
"is_active" : 1,
"points" : [
{
"points" : 85,
"bonus" : 20
},
{
"points" : 85,
"bonus" : 10
}
]
}
{
"_id" : ObjectId("5c7774b736397a1c0aa425eb"),
"name" : "Lucy",
"age" : 28,
"gender" : "female",
"is_active" : 1,
"points" : [
{
"points" : 85,
"bonus" : 20
},
{
"points" : 85,
"bonus" : 10
}
]
}
这里我想查询points字段里面第一个points为90的
> db.users.find({"points.0.points": 90}).pretty()
{
"_id" : ObjectId("5c77499137955b945af73220"),
"name" : "tom",
"age" : 20,
"gender" : "male",
"points" : [
{
"points" : 90,
"bonus" : 20
},
{
"points" : 88,
"bonus" : 11
}
]
}
不指定数组索引匹配字段
> db.users.find({"points.bonus": 11}).pretty()
{
"_id" : ObjectId("5c77499137955b945af73220"),
"name" : "tom",
"age" : 20,
"gender" : "male",
"points" : [
{
"points" : 90,
"bonus" : 20
},
{
"points" : 88,
"bonus" : 11
}
]
}
只有points数组里面的bonus满足11就返回
还可以指定数组文档的多个查询条件,这里我就不掩饰了
6.null
存在性筛选
> db.student.find()
{ "_id" : 1, "name" : "tom", "hobby" : null }
{ "_id" : 2, "name" : "Jack" }
> db.student.find({"hobby":null})
{ "_id" : 1, "name" : "tom", "hobby" : null }
{ "_id" : 2, "name" : "Jack" }
>
注意一下,我们筛选 hobby为
null
的,没有hobby这个字段的夜筛选出来了
> db.student.find({"hobby":{$exists:false}})
{ "_id" : 2, "name" : "Jack" }
> db.student.find({"hobby":{$exists:true}})
{ "_id" : 1, "name" : "tom", "hobby" : null }
通过
$exists
来指定
其他查询文档的方法 findOne()
看方法名就知道值查询返回满足条件的第一条记录
> db.users.findOne({"age": {$gt: 20}})
{
"_id" : ObjectId("5c7774b736397a1c0aa425ea"),
"name" : "Jack",
"age" : 28,
"gender" : "male",
"is_active" : 1,
"points" : [
{
"points" : 85,
"bonus" : 20
},
{
"points" : 85,
"bonus" : 10
}
]
}