MongoDB Query Document

Source: Internet
Author: User
Tags findone mongodb query

When it comes to queries, we generally think of relational database queries, such as order by (sort), limit (paging), range query (greater than a value, less than a value), in queries, on queries, like queries waiting a lot, and MongoDB also supports these operations, Just different syntax, such as the sort: MongoDB inside using the Skip (field:1/-1) method, the following to introduce:

A. MongoDB Query document

1. Query the document find (): No condition is to query the collection of all the data

    Syntax: Db.collectionName.find ({condition},{field:true/false})

1.1 Querying all data

      

      Displays the specified field (with contrast, find () the second parameter is the field of the specified query, the format [field name: True/false]true is displayed, False is not displayed):

      

1.2 Query total data count ()

      Syntax: Db.collectionName.find ({condition}). Count ()

      

2. Querying a Document FindOne (): Default is the first document that the query meets the criteria

    Syntax: Db.collectionName.findOne ({condition},{field:true/false})    

    

3.Pretty () method: Format The document that displays the query

    

The conditional operators ("$and", "$nor", "$not", "$or", "$exists", "$mod", "$regex", "$where", "$slice ", "$elemMatch", "null query", "$all", "$size", "$in", "$nin")

1. The following is a list of conditional operators, meaning, syntax, and comparison with relational databases

Equals {<key>:<value>} db.col.find({"by":"zgw"}) where by = ‘zgw‘
Less than ($LT) {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}) where likes < 50
Less than or equal to ($lte) {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}) where likes <= 50
Greater than ($GT) {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}) where likes > 50
Greater than or equal to ($GTE) {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}) where likes >= 50
Not equal to ($ne) {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}})   where likes != 50

  

    Syntax: Db.collectionName.find ({field:{operators1:value1,operators2:value2}})--[operators: These are the operators above ()]

PHP Format: Array (Field=>array (' $lte ' =>value)) or array (Field=>array (' $gte ' =>value1, ' $lte ' =>value2))

    

The following is a query that is not equal to an operator:

    

2. operators:"$and", "$nor", "$not", "$or", "$exists", "$mod", "$regex", "$where", "$slice", " $elemMatch "," null query "," $all "," $size "," $in "," $nin "

    2.1 $and :

Continued: http://www.cnblogs.com/egger/p/3135847.html

Http://www.cnblogs.com/zhaoyang/archive/2012/01/10/2317664.html

  

  

MongoDB Query Document

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.