如何在 MongoDB 中查询列表字段?
要了解列表字段查询,您还可以创建包含文档的集合。
用于创建包含文档的集合的查询如下 −
> db.andOrDemo.insertOne({"StudentName":"Larry","StudentScore":[33,40,50,60,70]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9522d316f542d757e2b444")
}
> db.andOrDemo.insertOne({"StudentName":"Larry","StudentScore":[87,67,79,98,90]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c95230916f542d757e2b445")
}使用 find() 方法显示集合中的所有文档。查询如下 −
> db.andOrDemo.find().pretty();
以下是输出 −
{
"_id" : ObjectId("5c9522d316f542d757e2b444"),
"StudentName" : "Larry",
"StudentScore" : [
33,
40,
50,
60,
70
]
}
{
"_id" : ObjectId("5c95230916f542d757e2b445"),
"StudentName" : "Larry",
"StudentScore" : [
87,
67,
79,
98,
90
]
}以下是列表字段查询。
查询如下 −
> db.andOrDemo.find({"StudentScore":70}).pretty();以下是输出
{
"_id" : ObjectId("5c9522d316f542d757e2b444"),
"StudentName" : "Larry",
"StudentScore" : [
33,
40,
50,
60,
70
]
}案例 3 − 以下是或列表字段的查询。
查询如下 −
> db.andOrDemo.find({"$or":[ {"StudentScore":60}, {"StudentScore":90}]}).pretty();示例输出 −
{
"_id" : ObjectId("5c9522d316f542d757e2b444"),
"StudentName" : "Larry",
"StudentScore" : [
33,
40,
50,
60,
70
]
}
{
"_id" : ObjectId("5c95230916f542d757e2b445"),
"StudentName" : "Larry",
"StudentScore" : [
87,
67,
79,
98,
90
]
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP