如何将MongoDB文档与数组中不包含值的字段匹配?
要匹配字段不包含数组中值的文档,请使用$nin。让我们创建一个包含文档的集合-
> db.demo180.insertOne({"Scores":["80","90","110"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3988a69e4f06af551997fb")
}
> db.demo180.insertOne({"Scores":["110","70","60"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3988b79e4f06af551997fc")
}
> db.demo180.insertOne({"Scores":["40","70","1010"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3988cc9e4f06af551997fd")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo180.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3988a69e4f06af551997fb"), "Scores" : [ "80", "90", "110" ] }
{ "_id" : ObjectId("5e3988b79e4f06af551997fc"), "Scores" : [ "110", "70", "60" ] }
{ "_id" : ObjectId("5e3988cc9e4f06af551997fd"), "Scores" : [ "40", "70", "1010" ] }以下是查询以将文档与不包含数组中值的字段进行匹配-
> db.demo180.aggregate({ "$match": { "Scores": { "$nin": ["110","90"] } } }).pretty();这将产生以下输出-
{
"_id" : ObjectId("5e3988cc9e4f06af551997fd"),
"Scores" : [ "40", "70", "1010" ]
}热门推荐
10 虎年公司祝福语简短的
11 高考前祝福语简短字句
12 好听的元旦简短祝福语
13 回家长的祝福语简短
14 好朋友夫妻祝福语简短
15 写给老师祝福语简短大学
16 锡婚祝福语简短霸气
17 奶奶八十寿辰祝福语简短
18 六十岁大寿祝福语简短