mongo_2 $in 和 $all 区别
in 只需满足( )内的某一个值即可, 而$all 必须满足[ ]内的所有值,
> db.table1.find({});
{ "_id" : ObjectId("56a5ce00dd3b78b7f50a298d"), "name" : [ 1, 2, 34, 54, 5, 6, 5 ], "age" : 1222 }
{ "_id" : ObjectId("56a5ce18dd3b78b7f50a298e"), "name" : [ 1, 2, 88, 77, 66, 5, 9, 54 ], "age" : 1222 }
只查5 都能查出来:
db.table1.find({'name':{$in:[5]}})
{ "_id" : ObjectId("56a5ce00dd3b78b7f50a298d"), "name" : [ 1, 2, 34, 54, 5, 6, 5 ], "age" : 1222 }
{ "_id" : ObjectId("56a5ce18dd3b78b7f50a298e"), "name" : [ 1, 2, 88, 77, 66, 5, 9, 54 ], "age" : 1222 }
> db.table1.find({'name':{$in:[88]}})
{ "_id" : ObjectId("56a5ce18dd3b78b7f50a298e"), "name" : [ 1, 2, 88, 77, 66, 5, 9, 54 ], "age" : 1222 }
用all
> db.table1.find({'name':{$all:[5,88]}})
{ "_id" : ObjectId("56a5ce18dd3b78b7f50a298e"), "name" : [ 1, 2, 88, 77, 66, 5, 9, 54 ], "age" : 1222 }
>