MongoDB query document

Source: Internet
Author: User
Tags install mongodb mongodb query mongodb tutorial

MongoDB query document
Syntax

The syntax format of MongoDB data query is as follows:

>db.COLLECTION_NAME.find()

The find () method displays all documents in an unstructured manner.

If you want to read data in a readable manner, you can use the pretty () method. The syntax format is as follows:

>db.col.find().pretty()

The pretty () method is formatted to display all documents.

Instance

The following instances query data in the Set col:

> Db. col. find (). pretty () {"_ id": ObjectId ("56063f17ade2f21f36b03133"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database ", "by": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes ": 100}

In addition to the find () method, there is also a findOne () method, which returns only one document.

Comparison between MongoDB and RDBMS Where statements

If you are familiar with the regular SQL data, you can better understand the MongoDB conditional statement query through the following table:

Operation Format Example Similar statements in RDBMS
Equal {<key>:<value>} Db. col. find ({"by": ""}). pretty () Where by = 'cainiao tutorial'
Less {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}).pretty() where likes < 50
Less than or equal {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater than or equal {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not equal {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}}).pretty() where likes != 50
MongoDB AND conditions

The find () method of MongoDB can be used to input multiple keys. Each key is separated by commas (,) AND the AND condition of conventional SQL statements.

The syntax format is as follows:

>db.col.find({key1:value1, key2:value2}).pretty()
Instance

The following example uses the by and title keys to query the data of the MongoDB tutorial in the cainiao tutorial.

> Db. col. find ({"by": "cainiao tutorial", "title": "MongoDB tutorial "}). pretty () {"_ id": ObjectId ("56063f17ade2f21f36b03133"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database ", "by": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes ": 100}

The above example is similar to the WHERE statement: WHERE by = 'cainiao 'AND title = 'mongodb'

MongoDB OR conditions

The keyword $ OR is used in the MongoDB or condition statement. The syntax format is as follows:

>db.col.find(   {      $or: [     {key1: value1}, {key2:value2}      ]   }).pretty()
Instance

In the following example, we demonstrate how to query documents with a key by value of the cainiao tutorial or a key title value of the MongoDB tutorial.

> Db. col. find ({$ or: [{"by": "" },{ "title": "MongoDB tutorial"}]}). pretty () {"_ id": ObjectId ("56063f17ade2f21f36b03133"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database ", "by": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes ": 100}>
Combined use of AND OR

The following example demonstrates the combined use of and or, similar to the conventional SQL statement: 'Where likes> 50 AND (by = 'cainiao 'OR title = 'mongodb ')'

> Db. col. find ({"likes": {$ gt: 50}, $ or: [{"by": "cainiao tutorial" },{ "title ": "MongoDB tutorial"}]}). pretty () {"_ id": ObjectId ("56063f17ade2f21f36b03133"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database ", "by": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes ": 100}

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

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.