Get the App
SLTechnology News&Howtos  ›  Database  › 

[MongoDB Learning Notes 17] query of MongoDB: array query in find

Shulou Source: shulou.com Published: 2022-06-01 18:33:53 10月30日 Update

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"]} >

1. $all to match a set of elements in the array

> db.post.find ({"fruit": {$all: ["apple", "banana"]}}) {"_ id": ObjectId ("54aa97b894dcf31069b590ca"), "id": 12, "fruit": ["apple", "banana", "peach"]} {"_ id": ObjectId ("54aa97f294dcf31069b590cc"), "id": 14, "fruit": ["cherry", "banana", "apple"]} > >

You can see that the order of apple and banana is irrelevant.

2. $size to query the matching array length

> db.post.find ({}, {"id": 1, "fruit": 1) "_ id": 0}) {"id": 2} {"id": 1} {"id": 3} {"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"]} >

3. The $slice operator

> db.post.find ({"id": 13}) {"_ id": ObjectId ("54aa97d794dcf31069b590cb"), "id": 13, "fruit": ["apple", "kumquat", "orange", "fruit01"]} >

Returns the first two values of the array

> db.post.find ({"id": 13}, {"fruit": {$slice:2}}) {"_ id": ObjectId ("54aa97d794dcf31069b590cb"), "id": 13, "fruit": ["apple", "kumquat"]} >

Returns the last two values of the array

> db.post.find ({"id": 13}, {"fruit": {$slice:-2}}) {"_ id": ObjectId ("54aa97d794dcf31069b590cb"), "id": 13, "fruit": ["orange", "fruit01"]} >

Returns the two values in the middle of the array

> db.post.find ({"id": 13}, {"fruit": {$slice: [1 slice 2]}}) {"_ id": ObjectId ("54aa97d794dcf31069b590cb"), "id": 13, "fruit": ["kumquat", "orange"]}

Fourth, use the following table of the array to query

> db.post.find ({"fruit.3": "fruit01"}) {"_ id": ObjectId ("54aa97d794dcf31069b590cb"), "id": 13, "fruit": ["apple", "kumquat", "orange", "fruit01"]} >

Tags: Array two queries elements operators length order notes learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn OPPO Reno Shulou Tech Info Linux