我只是好奇mongoose查询中的.in()和.all()方法有什么区别?你能通过一个简单的例子来解释一下吗?
$all
运算符检索包含我们传递的值子集的所有文档。子集可以以任何顺序出现。
$in
运算符检索包含我们传递的任一值的所有文档。
例如,考虑以下文档的“skills”集合:
{ "Name" : "Balaji", "skills" : [ "Dancing", "Cooking", "Singing" ] }
{ "Name" : "Ramesh", "skills" : [ "Cooking", "Singing" ] }
{ "Name" : "Suresh", "skills" : [ "Dancing", "Singing" ] }
db.skills.find({skills:{$all:["烹饪","唱歌"]}})
将仅返回包含烹饪和唱歌技能的文档作为一组,即Balaji和Ramesh。
db.skills.find({skills:{$in:["烹饪","唱歌"]}})
将返回所有文档,因为所有文档都包含烹饪或唱歌中的一个或多个技能。
以下是来自mongodb.org的说明:
$all
$all运算符类似于$in,但不同之处在于匹配指定数组中的所有值,而不仅仅是任意一个值。例如,对象
{ a: [ 1, 2, 3 ] }
将被以下查询所匹配
db.things.find( { a: { $all: [ 2, 3 ] } } );
但不会被以下查询所匹配
db.things.find( { a: { $all: [ 2, 3, 4 ] } } );
一个数组可以有比$all条件指定的元素更多。$all指定了必须匹配的最小元素集。
在这里阅读有关mongodb运算符的更多信息。
$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']},
]
考虑以下集合“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' 或 'Cricket'的文档。
db.gamesapp.find({"favourite_games":{$in:["Chess", "Cricket"]}})