Tag:mongodb find array
Db.post.find () { "_id" : objectid ("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "Joe", "age" : 30, "sex" : 1, "school" : "Marry" } { "_id" : objectid ("54a530c3ff0df3732bac1680 "), " id " : 1, " name " : " Joe ", " age " : 30, " comments " : [ "Test2", "Test9", "test5" ], "Sex" : 1, "school" : " Marry " } { " _id " : objectid (" 54a9700e1b5afd45354fd086 "), " id " : 3, "Test3" : 3 } { "_id" : objectid (" 54a9701c1b5afd45354fd087 "), " id " : 4, " test4 " : 4 } { "_id" : objectid ("54a970281b5afd45354fd088"), "id" : 5, "test5"  : 5 } { "_id" : objectid ("54a970351b5afd45354fd089"), "id" : 6, "Test6" : 6 } { "_id" : objectid (" 54a970781b5afd45354fd08a "), " id " : 7, " test7 " : 7 } { "_id" : objectid ("54a970831b5afd45354fd08b"), "id" : 8, "test8"  : 8 } { "_id" : objectid ("54a970901b5afd45354fd08c"), "id" : 9, "Test9" : 9 } { "_id" : objectid (" 54a9709c1b5afd45354fd08d "), " id " : 10, " test10 " : 10 } { "_id" : objectid ("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 } { "_id" : objectid ("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "Apple", "banana", "peach" ] } { "_id"  : objectid ("54AA97D794DCF31069B590CB"), "id" : 13, "fruit" : [ "apple", "Kumquat", "Orange" ] } { "_id" : objectid (" 54aa97f294dcf31069b590cc "), " id " : 14, " fruit " : [ " cherry ", " banana ", "Apple" ] } >
One, $all to match a set of elements in an array
> db.post.find ({"fruit": {$all: ["Apple", "Banana"]}}) {"_id": ObjectId ("54aa97b894dcf31069b590ca"), "id": "Frui" T ": [" apple "," banana "," peach "]} {" _id ": ObjectId (" 54aa97f294dcf31069b590cc ")," id ": +," fruit ": [" Cherry ", "Banana", "Apple"]} >
You can see the order of Apple and banana is not related;
Second, $size to query the matching array length;
> db.post.find ({},{"id": 1, "fruit": 1, "_id": 0}) { "id" : 2 } { "id" : 1 } { "id"  : 3 } { "id" : 4 } { "id" : 5 } { "id" : 6 } { "id"  : 7 } { "id" : 8 } { "id" : 9 } { "id" : 10 } { " ID " : 11 } { " id " : 12, " fruit " : [ " Apple ", " banana ", " peach " ] } { " id " : 13, " Fruit " : [ " apple ", " Kumquat ", " Orange ", " fruit01 " ] } { } >> db.post.find ({"fruit": {$size: 3}) { "_id" : ObjectId ("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "apple", "Banana", "peach" ] } >
Third, $slice operator
> Db.post.find ({"id": +}) {"_id": ObjectId ("54AA97D794DCF31069B590CB"), "id": +, "fruit": ["apple", "Kumquat", "Orange", "fruit01"]} >
Returns the first two values of an array
> Db.post.find ({"id": 13},{"fruit": {$slice: 2}}) {"_id": ObjectId ("54AA97D794DCF31069B590CB"), "id": "Fruit": ["Apple", "Kumquat"]} >
Returns the latter two values of an array
> Db.post.find ({"id": 13},{"fruit": {$slice: -2}}) {"_id": ObjectId ("54AA97D794DCF31069B590CB"), "id": "Fruit": ["Orange", "fruit01"]} >
Returns the two values in the middle of the array
> Db.post.find ({"id": 13},{"fruit": {$slice: [up]}}) {"_id": ObjectId ("54AA97D794DCF31069B590CB"), "id": "Fruit ": [" Kumquat "," Orange "]}
Four, using the array of the following table query
> Db.post.find ({"fruit.3": "fruit01"}) {"_id": ObjectId ("54AA97D794DCF31069B590CB"), "id": +, "fruit": ["Apple", "K Umquat "," Orange "," fruit01 "]}>
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1599470
"MongoDB Learning note 17" MongoDB query: Array query in Find