Mongodb query, mongodb

Source: Internet
Author: User
Tags findone mongodb query

Mongodb query, mongodb
1. query a single record

> db.choice.findOne()

2. Specify query conditions during Query

> db.choice.find({"_id":"005a38d5"})

All queries by default {}

Return Column

> db.choice.find({"_id":"005a38d5"},{"title":1,"results":1}){ "_id" : "005a38d5", "title" : "While some maintain that the recent proliferation of uncredited web sources will have a(n) (i)_____ effect on scholarship, others argue that the effects will be far more (ii)_____, claiming that academics are sensible enough not to (iii)_____ unattributed sources.", "results" : [  [  "1" ],  [  "1" ],  [  "0" ] ] }>

Specify the returned title and results attributes

_ Id is returned by default, or you can set not to return

> db.choice.find({"_id":"005a38d5"},{"title":1,"results":1,"_id":0}){ "title" : "While some maintain that the recent proliferation of uncredited websources will have a(n) (i)_____ effect on scholarship, others argue that the effects will be far more (ii)_____, claiming that academics are sensible enough not to (iii)_____ unattributed sources.", "results" : [  [  "1" ],  [  "1" ],  ["0" ] ] }>

3. Conditional Query


Conditional Operators
"$ Lt" ====================> "<"
"$ Lte" ===============> "<="
"$ Gt" ====================> ">"
"$ Gte" ==================> ">="
"$ Ne" =================> "! ="


Query a record with a value greater than or equal to 1 and a value less than or equal to 2.
> db.choice.findOne({"blankCount":{$lte:2,$gte:1}},{"blankCount":1}){ "_id" : "006526ff", "blankCount" : 2 }>

4. $ or
> db.choice.findOne({$or:[{"blankCount":2},{"type":3}]})

Query a record whose value is 2 or whose type is 3.


5. $ not

> db.choice.findOne({"type":{"$not":{$gt:3}}})


Query a record whose type is not greater than 3.

6. query null records

> db.questionSet.findOne({source:null})


When the field is set to null, the record that the field is null or does not contain this field will also match.


If you do not query fields that do not exist, use $ exists: true


> db.questionSet.findOne({source:null,$exists:true})

7. Use a regular expression when querying

> db.choice.findOne({title:/^While/})

Query a record whose title starts with "While"

8. array Query

> db.questionSet.findOne({"questionIds":'6188e9fc'},{"questionIds":1}){        "_id" : "030eeeba",        "questionIds" : [                "6188e9fc",                "a380e38c",                "addff709",                "b6bc4eff",                "5095b99f",                "c8352e48",                "ecca3626",                "c31125f7"        ]}



Query records in the array questionIds that contain 6188e9fc


If $ all is used to query multiple elements in the array, It is not sequential.
> db.questionSet.findOne({"questionIds":{$all:['6188e9fc','a380e38c']}},{"questionIds":1}){        "_id" : "030eeeba",        "questionIds" : [                "6188e9fc",                "a380e38c",                "addff709",                "b6bc4eff",                "5095b99f",                "c8352e48",                "ecca3626",                "c31125f7"        ]}

Exact match:
> db.questionSet.findOne({"questionIds":['6188e9fc','a380e38c']},{"questionIds":1})null


9. $ slice Operator
Retrieve the first three records in the array.
> db.questionSet.findOne({},{"questionIds":{$slice:3}}){        "_id" : "030eeeba",        "catQuestionSet" : 2,        "orderNo" : 2,        "source" : 1,        "type" : 2,        "level" : 3,        "questionCount" : 10,        "questionIds" : [                "6188e9fc",                "a380e38c",                "addff709"        ]}

Retrieve the last three records in the array.
> db.questionSet.findOne({},{"questionIds":{$slice:-3}}){        "_id" : "030eeeba",        "catQuestionSet" : 2,        "orderNo" : 2,        "source" : 1,        "type" : 2,        "level" : 3,        "questionCount" : 10,        "questionIds" : [                "c8352e48",                "ecca3626",                "c31125f7"        ]}

10. embedded document Query


There are two ways to query a document: one is to query the document completely, and the other is to query the key-value pair. The exact match query of the embedded document is the same as the exact match query of the array. The number of key-value pairs in the embedded document must be consistent in order to match.

Exact match:
> db.choice.findOne({"explain":{"ccurlList":"3DC334A16B187EBF9C33DC5901307461","textExplain":"Answers"}})

Key-value pair matching (commonly used ):
> db.choice.findOne({"explain.ccurlList":"3DC334A16B187EBF9C33DC5901307461","explain.textExplain":"Answers"})

$ ElemMatch
> db.choice.findOne({"explain":{$elemMatch:{"ccurlList":"3DC334A16B187EBF9C33DC5901307461","textExplain":"Answers"}}})

11. $ where Query
Query a record with the same type as that of the balancer instance.
> db.choice.findOne({$where:"this.blankCount==this.type"}){        "_id" : "005a38d5",        "blankCount" : 3,        "explain" : {                "ccurlList" : [ ]        },        "type" : 3,        "questionSetId" : "affccc14"     }

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.