Examples of common query commands for MongoDB

Source: Internet
Author: User

Take the maximum value of the Time field:

Db.getcollection (' Calllog '). Find (). Sort ({"Time": -1}). Limit (1)

Minimum value only need to change –1 to 1

Db.getcollection (' Calllog '). Find (). Sort ({"Time": 1}). Limit (1)

Appcode field is not equal to "100001" record

Db.getcollection (' Calllog '). Find ({"Appcode": {$ne: "100001"}})

Support <, <=, >= query, with symbolic substitution for $lt, $lte, $GT, $gte
Db.colls.find ({"field": {$gt: Value}});
Db.colls.find ({"field": {$lt: Value}});
Db.colls.find ({"field": {$gte: Value}});
Db.colls.find ({"field": {$lte: Value}});


You can also make a range query for a field
Db.colls.find ({"field": {$gt: value1, $lt: value2}});


Not equal to query character $ne
Db.colls.find ({x: {$ne: 3}});


In query with character $in
Db.colls.find ({"field": {$in: Array}});
Db.colls.find ({j:{$in: [2,4,6]}});


Not in query with character $nin
Db.colls.find ({j:{$nin: [2,4,6]}});


Character $mod for modulo query
Db.colls.find ({A: {$mod: [ten, 1]}})//where a% 10 = = 1


$all Query
Db.colls.find ({A: {$all: [2, 3]}});//Specifies that a satisfies any value in the array


$size Query
Db.colls.find ({A: {$size: 1});//The number of objects queried, this query queries the number of child objects of a 1 record


$exists Query
Db.colls.find ({A: {$exists: true}}); There is data for the A object
Db.colls.find ({A: {$exists: false}}); There is no data for the A object


$type Query the type value of the $type value to bsonhttp://bsonspec.org/data
Db.colls.find ({A: {$type: 2}}); Match A to string type data
Db.colls.find ({A: {$type: 16}}); Match A to int type data

Examples of common query commands for MongoDB

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.