MongoDB Query Statement Learning Summary

Source: Internet
Author: User
Tags arrays comments mongodb mongodb query

On the left is the MONGODB query statement, and the right is the SQL statement. Compared with the use, very convenient.

Db.users.find () SELECT * from Users

Db.users.find ({"Age": +}) SELECT * from the users where age = 27

Db.users.find ({"username": "Joe", "Age": +}) SELECT * from the users where "username" = "Joe" and age = 27

Db.users.find ({}, {"username": 1, "email": 1}) Select Username, email from users

Db.users.find ({}, {"username": 1, "_id": 0})//no case//Instant column filter is added, _id will also return; you must explicitly block _id return

Db.users.find ({"Age": {"$gte": +, "$lte": ()}) select * from the users where age >=18 and age <=//$lt (<) $l Te (<=) $gt (>) $gte (>=)

Db.users.find ({"username": {"$ne": "Joe"}}) select * from users where username <> "Joe"

Db.users.find ({"Ticket_no": {"$in": [725, 542, 390]}) SELECT * from the users where Ticket_no in (725, 542, 390)

Db.users.find ({"Ticket_no": {"$nin": [725, 542, 390]}) SELECT * from the users where ticket_no not in (725, 542, 390)

Db.users.find ({"$or": [{"Ticket_no": 725}, {"Winner": True}]}) select * Form users where Ticket_no = 725 or winner = TR Ue

Db.users.find ({"Id_num": {"$mod": [5, 1]}}) select * from Users where (id_num mod 5) = 1

Db.users.find ({"$not": {"Age": +}}) select * from the users where not (age = 27)

Db.users.find ({"username": {"$in": [null], "$exists": True}}) select * from the users where username is null//if directly through find ( {"username": null}) For enquiries, then the "no username" record is screened out

Db.users.find ({"Name":/joey?/i})//Regular query, value is an expression that conforms to Pcre

Db.food.find ({fruit: {$all: ["Apple", "Banana"}})//array of queries, field fruit, contains both "Apple" and "banana" records

Db.food.find ({"fruit.2": "Peach"})//array of queries, field fruit, 3rd (starting from 0) element is peach Record

Db.food.find ({' fruit ': {' $size ': 3}})//array of queries, the number of query arrays is 3 records, $size previously cannot be combined with other operators

Db.users.findOne (criteria, {"Comments": {"$slice": 10}})//array of queries, returning only the first 10 of the arrays comments, or {"$slice": -10}, {"$slice": [ 23, 10]}; Return the last 10, and the middle 10, respectively.

Db.people.find ({"Name.first": "Joe", "Name.last": "Schmoe"})//nested query

Db.blog.find ({"Comments": {"$elemMatch": {"author": "Joe", "score": {"$gte": 5}}})//nested query, only if nested elements are arrays are used,

Db.foo.find ({"$where": "this.x + this.y = = 10"})//complex query, $where of course is very convenient, but inefficient. For complex queries, the order of consideration should be regular---MapReduce--$where

Db.foo.find ({"$where": "function () {return this.x + This.y = = Ten;}"})//$where can support JavaScript functions as query criteria

Db.foo.find (). Sort ({"X": 1}). Limit (1). Skip (10); Return to article (10, 11], sorted by "X"; The order of three limit is arbitrary, should try to avoid using Large-number in Skip

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.