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 15] MongoDB query: find query condition

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

Share

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

Find can match more conditions than precise query.

I. comparison operator

$lt stands for =

> 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} > db.post.find ({"id": {"$gte": 5, "$lte": 7}}) {"_ id": ObjectId ("54a970281b5afd45354fd088"), "id": 5, "test5": 5} {"_ id": ObjectId ("54a970351b5afd45354fd089"), "id": 6 "test6": 6} {"_ id": ObjectId ("54a970781b5afd45354fd08a"), "id": 7, "test7": 7} >

$ne does not mean:

> db.post.find ({"id": {"$ne": 8}}) {"_ id": ObjectId ("54a530c3ff0df3732bac1681"), "id": 2, "name": "joe", "age": 30, "sex": 1, "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 ("54a970901b5afd45354fd08c"), "id": 9, "test9": 9} {"_ id": ObjectId ("54a9709c1b5afd45354fd08d") "id": 10, "test10": 10} >

$in can query multiple key values:

> db.post.find ({"id": {"$in": [4je 2je 8]}}) {"_ id": ObjectId ("54a530c3ff0df3732bac1681"), "id": 2, "name": "joe", "age": 30, "sex": 1, "school": "marry"} {"_ id": ObjectId ("54a9701c1b5afd45354fd087"), "id": 4 "test4": 4} {"_ id": ObjectId ("54a970831b5afd45354fd08b"), "id": 8, "test8": 8} >

$nin usage:

> db.post.find ({"id": {"$nin": [4je 2je 8]}}) {"_ 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 ("54a970281b5afd45354fd088"), "id": 5, "test5": 5} {"_ id": ObjectId ("54a970351b5afd45354fd089"), "id": 6, "test6": 6} {"_ id": ObjectId ("54a970781b5afd45354fd08a"), "id": 7, "test7": 7} {"_ id": ObjectId ("54a970901b5afd45354fd08c") "id": 9, "test9": 9} {"_ id": ObjectId ("54a9709c1b5afd45354fd08d"), "id": 10, "test10": 10} >

Usage of $or:

> db.post.find ({"$or": [{"sex": 1}, {"id": 5}) {"_ 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 ("54a970281b5afd45354fd088"), "id": 5, "test5": 5} >

Mod divides the value of the query by the first given value. If the remainder matches the second value, the match is successful.

> 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} > db.post.find ({"id": {$mod: [5jue 1]}}) {"_ id": ObjectId ("54a530c3ff0df3732bac1680"), "id": 1 "name": "joe", "age": 30, "comments": ["test2", "test9", "test5"], "sex": 1, "school": "marry"} {"_ id": ObjectId ("54a970351b5afd45354fd089"), "id": 6, "test6": 6} {"_ id": ObjectId ("54aa8a90652d8bdfa0566d34"), "id": 11 "test10": 11} >

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