如何在 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