MongoDB Advanced Query

Source: Internet
Author: User

The logical structure of MongoDB is a hierarchical structure. Mainly consists of three parts: Document, Collection (collection), database. A logical structure is a logical structure used by user-facing users to develop applications using MONGODB.

The documentation document for MongoDB is equivalent to a row of records in a relational database.

Multiple Documents form a collection collection a table equivalent to a relational database.

Multiple collections collection logically together are database databases.

A MongoDB instance supports multiple databases.

MongoDB Conditional Query

Db.collection.find ({"field": {$gt: Value}})//greater Than

Db.collection.find ({"field": {$lt: Value}}); Less than: Field < value

Db.collection.find ({"field": {$gte: Value}}); Greater than or equal to: field >= value

Db.collection.find ({"field": {$lte: Value}}); Less than equals: field <= value

You can do this if you want to satisfy multiple conditions at the same time

Db.collection.find ({"field": {$gt: value1, $lt: value2}}); value1 < Field < value

$all match all the values that must be met in [] for example:

Db.users.find ({age: {$all: [6, 8]}});

Can query {name: ' David ', age:26, Age: [6, 8, 9]}

But the query does not show {name: ' David ', age:26, Age: [6, 7, 9]}


$exists determine if a field exists

Querying all records that exist in the age field

Db.users.find ({age: {$exists: true}});

Query all records that do not exist in the name field

Db.users.find ({name: {$exists: false}});

$ne Not equal to

The value of the query x is not equal to 3 of the data

Db.things.find ({x: {$ne: 3}});


$in contains

Querying the value of x within the 2,4,6 range of data

Db.things.find ({x:{$in: [2,4,6]}});

$nin does not contain

Querying the value of x outside the 2,4,6 range of data

Db.things.find ({x:{$nin: [2,4,6]}});

$size number of array elements

For {name: ' David ', age:26, Favorite_number: [6, 7, 9]} record

Match Db.users.find ({favorite_number: {$size: 3}});

Mismatch Db.users.find ({favorite_number: {$size: 2}});

Regular expression Matching

Query mismatch name=b* lead record

Db.users.find ({name: {$not:/^b.*/}});


Skip limit Returns the start of a record

Returns 5 records starting from 3rd record (limit 3, 5)

Db.users.find (). Skip (3). Limit (5);


Sort sorts

With age ascending ASC

Db.users.find (). Sort ({age:1});

Descending desc by age

Db.users.find (). Sort ({Age:-1});


MongoDB Advanced Query

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.