MongoDB Query Statement Learning Summary

Source: Internet
Author: User
Tags mongodb query

Read some information, corresponding only need to know how to query and use MongoDB for me, these are enough. 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 Usersdb.users.find ({' Age ': +}) SELECT * from users where age = 27db.users.find ({"username"  : "Joe", "Age": +}) SELECT * from the users where "username" = "Joe" and age = 27db.users.find ({}, {"username": 1, "email" : 1}) Select Username, email from usersdb.users.find ({}, {"username": 1, "_id": 0})//No case//immediately with column filtering, _ID will also be returned; Type of block _id returns Db.users.find ({"Age": {"$gte": "$lte": +}) SELECT * from the users where age >=18 and age <=//$lt (<) $lte (<=) $gt (>) $gte (>=) db.users.find ({"username": {"$ne": "Joe"}}) select * from Users where username & Lt;> "Joe" Db.users.find ({"Ticket_no": {"$in": [725, 542, 390]}) SELECT * from the users where Ticket_no in (725, 542, 39 0) Db.users.find ({"Ticket_no": {"$nin": [725, 542, 390]}) SELECT * from the users where ticket_no not in (725, 542, 390) db.u Sers.find ({"$or": [{"Ticket_no": 725}, {"Winner": True}]}) SELECT * Form Users where Ticket_no = 725 or winner = Truedb.users.find ({"Id_num": {"$mod": [5, 1]}}) select * from Users where (id_n Um mod 5) = 1db.users.find ({"$not": {"Age": +}}) select * from the users where not (age = $) Db.users.find ({"username": {"$ In ": [null]," $exists ": true}) the select * from the users where username is null//if queried directly through find ({" username ": null}) then" No A 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 "record Db.food.find ({" fruit.2 ":" Peach "})//array of queries, field fruit, 3rd ( Starting from 0) The element is the record of the peach 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 comments, and {"$slice": -10}, {"$slice": [23, 10]}; Return the last 10, and the middle 10 db.people.find ({"Name.first": "Joe", "Name.last": "Schmoe"})//nested query Db.blog.find ({"Comments": {"$ Elemmatch ": {" author ":" Joe "," score ": {" $gte": 5}}})//nested query, only when nested elements are arrays used, Db.foo.find ({" $where ":" this.x + this.y = = 10 "})//complex queries, $where of course is very convenient, but inefficient.  For complex queries, the order of consideration should be $wheredb. Foo.find ({"$where": "function () {return this.x + This.y = = 10;}"})// $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

Transferred from: http://www.cnblogs.com/viviman/archive/2012/11/21/2780562.html

MongoDB Query Statement Learning Summary

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.