我只是好奇在mongoose Query中.in()和.all()方法之间的区别是什么? 你能用一个简单的例子来解释。
答案 0 :(得分:14)
以下是mongodb.org的解释:
<强> $所有强>
$ all运算符类似于$ in,但不是匹配指定数组中的任何值,而是必须匹配数组中的所有值。例如,对象
{a:[1,2,3]}
将匹配
db.things.find({a:{$ all:[2,3]}});
但不是
db.things.find({a:{$ all:[2,3,4}}});
数组可以包含比$ all条件指定的元素更多的元素。 $ all指定必须匹配的最小元素集。
详细了解mongodb运算符here
答案 1 :(得分:9)
$ all运算符检索包含我们传递的值子集的所有文档。子集可以是任何顺序。
$ in运算符检索包含我们传递的任一值的所有文档。
例如,考虑使用以下文档收集“技能”
{ "Name" : "Balaji", "skills" : [ "Dancing", "Cooking", "Singing" ] }
{ "Name" : "Ramesh", "skills" : [ "Cooking", "Singing" ] }
{ "Name" : "Suresh", "skills" : [ "Dancing", "Singing" ] }
db.skills.find({技能:{$ all:[“烹饪”,“唱歌”]}})将仅返回包含舞蹈和演唱技巧的文件,即Balaji和Ramesh。
db.skills.find({技能:{$ in:[“烹饪”,“唱歌”]}})`将返回所有文件,因为所有文件都包含烹饪或唱歌。
答案 2 :(得分: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-这是一个比较查询运算符,它基于列出可能条件的数组。它有助于查询各种值。它列出了查询中指定数组中存在的匹配 任何值的文档。以下内容有助于确定所有值为“ Chess”(棋)或“ C”(Cricket)
的文档db.gamesapp.find({“ favourite_games”:{$ in:[“ Chess”,“ Cricket”]}})
这将列出所有ObjectId的文档 7892de7a123be821ebcca757,7892de7a123be821ebcca758, 7892de7a123be821ebcca759和7892de7a123be821ebcca760
答案 3 :(得分:0)
$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']},
]