Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report