12

我只是好奇猫鼬查询中的 .in() 和 .all() 方法有什么区别?你能用一个简单的例子来解释一下。

4

4 回答 4

18

以下是来自 mongodb.org 的解释:

$全部

$all 运算符类似于 $in,但不是匹配指定数组中的任何值,而是必须匹配数组中的所有值。例如,对象

{一:[1,2,3]}

将被匹配

db.things.find( { a: { $all: [ 2, 3 ] } } );

但不是

db.things.find( { a: { $all: [ 2, 3, 4 ] } } );

数组可以包含比 $all 条件指定的元素更多的元素。$all 指定必须匹配的最小元素集。

在此处阅读有关 mongodb 运算符的更多信息

于 2012-08-19T15:42:03.817 回答
17

$all运算符检索包含我们传递的值的子集的所有文档。子集可以按任何顺序排列。

$in运算符检索包含我们传递的任何一个值的所有文档。

例如,考虑包含以下文档的集合“技能”:

{ "Name" : "Balaji", "skills" : [ "Dancing", "Cooking", "Singing" ] }
{ "Name" : "Ramesh", "skills" : [ "Cooking", "Singing" ] }
{ "Name" : "Suresh", "skills" : [ "Dancing", "Singing" ] }

db.skills.find({skills: { $all : ["Cooking", "Singing" ] } } )将仅返回包含烹饪和唱歌技能的文件,即 Balaji 和 Ramesh。

db.skills.find({skills: { $in : ["Cooking", "Singing" ] } } )将返回所有文档,因为所有文档都包含烹饪或唱歌。

于 2015-06-02T19:22:58.410 回答
1
$all - This an array operator that is equivalent to an $and operation.

[
    {name:'Shubham',hobbies:['cricket','dancing','football']},
    {name:'Chandrakesha',hobbies:['cricket','singing','dancing','football']},
    {name:'Nitish',hobbies:['cricket','singing','dancing','football']},
    {name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]

Query:->

{hobbies: { $all : ["readingbooks", "singing" ] } }
                OR
can write like below query
{ 
    $and: [
        {"hobbies": "readingbooks"},
        {"hobbies": "singing"},
    ]
}

will return only the documents which contains both readingbooks and singing hobbies

output:->

[
    {name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]

-------------------------------
$in operator retrieves all the documents which contains the either of the values we pass.

[
    {name:'Shubham',hobbies:['cricket','dancing','football']},
    {name:'Chandrakesha',hobbies:['cricket','singing','dancing','football']},
    {name:'Nitish',hobbies:['cricket','singing','dancing','football']},
    {name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]

Query:->

{hobbies: { $in : ["readingbooks", "singing" ] } }

                OR
can write like below query
{ 
    $or: [
        {"hobbies": "readingbooks"},
        {"hobbies": "singing"},
    ]
}

will return all the documents which contains any of them readingbooks or singing hobbies

output:->

[
    {name:'Chandrakesha',hobbies:['cricket','singing','dancing','football']},
    {name:'Nitish',hobbies:['cricket','singing','dancing','football']},
    {name:'Lovish',hobbies:['readingbooks','singing','dancing','football']},
]
于 2021-08-11T17:03:12.800 回答
0

考虑以下集合“gamesapp”

{
   _id: ObjectId("7892de7a123be821ebcca757"),
   name: "kid1",
   games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
   favourite_games: [ "Chess", "Cricket", "Football" ]
}

{
   _id: ObjectId("7892de7a123be821ebcca758"),
   name: "kid2",
   games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
   favourite_games: [ "Chess", "Cricket" ]
}

{
   _id: ObjectId("7892de7a123be821ebcca759"),
   name: "kid3",
   games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
   favourite_games: [ "Chess" ]
}

{
   _id: ObjectId("7892de7a123be821ebcca760"),
   name: "kid4",
   games: [ "Chess", "Cricket", "Football", "Hockey", "Carrom" ],
   favourite_games: [ "Chess", "Cricket" ]
}

$all - 这是一个等效于 AND 操作的数组运算符。如果它与指定数组中的所有值匹配,这将返回集合中的文档。下面有助于确定所有具有 'Chess' 'Cricket' 价值的文件

db.gamesapp.find({"favourite_games":{$all:["Chess","Cricket"]}})

这将列出 ObjectId 为 7892de7a123be821ebcca757、7892de7a123be821ebcca758 和 7892de7a123be821ebcca760 的文档

$in - 这是一个比较查询运算符,它基于列出可能条件的数组。它有助于查询各种值它列出与查询中指定数组中存在的任何值匹配 的文档。以下有助于确定所有具有“国际象棋”“板球”值的文档

db.gamesapp.find({"favourite_games":{$in:["Chess", "Cricket"]}})

这将列出 ObjectId 7892de7a123be821ebcca757、7892de7a123be821ebcca758、7892de7a123be821ebcca759 和 7892de7a123be821ebcca760 的所有文档

于 2019-10-28T04:42:58.113 回答