"MongoDB Learning Note 15" MongoDB query: Find query criteria

Source: Internet
Author: User
Tags mongodb query

Find in addition to the exact query, can match more conditions;

First, comparison operators

$LT Representative <;

$lte Representative <=;

$GT Representative >;

$GTE Representative >=;

> 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" &NBSP;:  4 }    {  "_id"  : objectid ("54a970281b5afd45354fd088"),  "id"  : 5,  "Test5"  : 5&nbSP;}     {  "_id"  : objectid ("54a970351b5afd45354fd089"),  "id"  : 6 ,  "Test6"  : 6 }    {  "_id"  : objectid (" 54a970781b5afd45354fd08a "), " id " : 7, " test7 " : 7 }     {  "_id"  : objectid ("54a970831b5afd45354fd08b"),  "id"  : 8,  "test8" &NBSP;:  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 represents not equal to:

> db.post.find ({"id": {"$ne": 8}})    {  "_id"  : objectid (" 54a530c3ff0df3732bac1681 "), " id " : 2, " name " : " Joe ", " Age " : 30,   "Sex"  : 1,  "school"  :  "Marry"  }    {  "_id" &NBSP;:  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": [4,2,8]}}) {"_id": ObjectId ("54a530c3ff0df3732bac1681"), "id": 2, "name": "Joe", "age ": +," 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": [4,2,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" &NBSP;:  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" &NBSP;:  10 }    >

Usage of $or:

> Db.post.find ({"$or": [{"Sex": 1},{"id": 5}]}) {"_id": ObjectId ("54a530c3ff0df3732bac1681"), "id": 2, "name": "Joe"  , "age": +, "sex": 1, "school": "Marry"} {"_id": ObjectId ("54a530c3ff0df3732bac1680"), "id": 1, "name": "Joe", "Age": "Comments": ["Test2", "Test9", "Test5"], "sex": 1, "school": "Marry"} {"_id": ObjectId ("54a970281b 5afd45354fd088 ")," id ": 5," TEST5 ": 5} >


The $mod divides the value of the query by the first given value, and if the remainder matches the second value, the match succeeds;

> 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" &NBSP;:  4 }    {  "_id"  : objectid ("54a970281b5afd45354fd088"),  "id"  : 5,  "Test5"  : 5&nbSP;}     {  "_id"  : objectid ("54a970351b5afd45354fd089"),  "id"  : 6 ,  "Test6"  : 6 }    {  "_id"  : objectid (" 54a970781b5afd45354fd08a "), " id " : 7, " test7 " : 7 }     {  "_id"  : objectid ("54a970831b5afd45354fd08b"),  "id"  : 8,  "test8" &NBSP;:  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: [5,1]}})      {  "_id"  : objeCtId ("54a530c3ff0df3732bac1680"),  "id"  : 1,  "name"  :  "Joe",  "Age" &NBSP;:  30,  "Comments"  : [  "Test2",  "Test9",  "test5"  ],  "Sex" &NBSP;:  1,  "School"  :  "Marry"  }    {  "_id"  : objectid (" 54a970351b5afd45354fd089 "), " id " : 6, " test6 " : 6 }     {  "_id"  : objectid ("54aa8a90652d8bdfa0566d34"),  "id"  : 11,  "test10" &NBSP;:  11 }    >



This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1599446

"MongoDB Learning Note 15" MongoDB query: Find query criteria

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.