在这种情况下,$ and和$ all有什么区别?

时间:2019-07-01 14:00:06

标签: javascript node.js mongodb

这两行代码只是输出相同的结果,所以它们之间有什么区别?我知道我知道,文档...但是我的意思是在这种情况下。谢谢您的回答!

db.someData.find({$and: [{genre: {$eq: "action"}}, {genre: {$eq: "thriller"}}]}).pretty()

db.someData.find({genre: {$all: ["action", "thriller"]}}).pretty()

这是我的mongodb数据库中的集合。

{
        "_id" : ObjectId("5d19fe6080fc4d046d99d42b"),
        "title" : "The Last Student Returns",
        "meta" : {
                "rating" : 9.5,
                "aired" : 2018,
                "runtime" : 100
        },
        "visitors" : 1300000,
        "expectedVisitors" : 1550000,
        "genre" : [
                "thriller",
                "drama",
                "action"
        ]
}
{
        "_id" : ObjectId("5d19fe6080fc4d046d99d42c"),
        "title" : "Teach me if you can",
        "meta" : {
                "rating" : 8.5,
                "aired" : 2014,
                "runtime" : 90
        },
        "visitors" : 590378,
        "expectedVisitors" : 500000,
        "genre" : [
                "action",
                "thriller"
        ]
}
{
        "_id" : ObjectId("5d19fe6080fc4d046d99d42d"),
        "title" : "Supercharged Teaching",
        "meta" : {
                "rating" : 9.3,
                "aired" : 2016,
                "runtime" : 60
        },
        "visitors" : 370000,
        "expectedVisitors" : 1000000,
        "genre" : [
                "thriller",
                "action"
        ]
}

1 个答案:

答案 0 :(得分:1)

your exact question is actually answered there起,您提到documentation的有趣之处:

行为

  

等同于$ and操作

     

$ all 等同于指定值的 $ and 操作;   即以下语句:

{ tags: { $all: [ "ssl" , "security" ] } }

等同于:

{ $and: [ { tags: "ssl" }, { tags: "security" } ] }

但是总的来说,mongo有很多方法可以得到相同的结果,就像JS等有很多方法可以得到相同的精确结果。

相关问题