MongoDB Simple Query statement

Source: Internet
Author: User
Tags mongodb query

    1. On the left is the MONGODB query statement, and the right is the SQL statement. Compared with the use, very convenient.
    2. Db.users.find () SELECT * from Users
    3. Db.users.find ({"age": + }) select * from the users where age =
    4. Db.users.find ({"username": "Joe", "age": + }) select * from users where "username" = "Joe" and a GE =
    5. Db.users.find ({}, {"username": 1, "email": 1}) Select Username, email from users
    6. Db.users.find ({}, {"username": 1, "_id": 0}) //No case//Instant column filter is added, _id will also return; you must explicitly block _id return
    7. Db.users.find ({"Age": {"$gte": + , "$lte": ( )}) select * from the users where age >= <= //$lt (<) $lte (<=) $gt (>) $gte (>=)
    8. Db.users.find ({"username": {"$ne": "Joe"}}) select * from users where username <> "Joe"
    9. Db.users.find ({"ticket_no": {"$in": [725, 542, 390]}) SELECT * from the users where Ticket_no in (7 542, 390)
    10. Db.users.find ({"ticket_no": {"$nin": [725, 542, 390]}) SELECT * from the users where ticket_no not in ( 725, 542, 390)
    11. Db.users.find ({"$or": [{"ticket_no": 725}, {"winner": True}]}) select * Form users where ticket_ No = 725 or winner = true
    12. Db.users.find ({"Id_num": {"$mod": [5, 1]}}) select * from Users where (id_num mod 5) = 1 /c4>
    13. Db.users.find ({"$not": {"age": +} }) select * from the users where not (age = $ )
    14. Db.users.find ({"username": {"$in": [null], "$exists": True}}) select * from the users where username is NULL //If the query is made directly through find ({"username": null}), then the "no username" record is filtered out
    15. Db.users.find ({"name":/joey?/i}) //Regular query, value is an expression that conforms to Pcre
    16. Db.food.find ({fruit: {$all: ["Apple", "Banana"}}) //array of queries, field fruit, contains both "Apple" and "banana" records
    17. Db.food.find ({"fruit.2": "Peach"}) //array of queries, field fruit, 3rd (starting from 0) element is peach record
    18. 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 /c3>
    19. Db.users.findOne (criteria, {"comments": {"$slice":} ) //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.
    20. Db.people.find ({"Name.first": "Joe", "name.last": "Schmoe"}) //nested query
    21. Db.blog.find ({"comments": {"$elemMatch": {"author": "Joe", "score": {"$gte": 5}}}) //nested queries, which are used only when nested elements are arrays,
    22. Db.foo.find ({"$where": "this.x + this.y = ="}) //complex queries, $where of course are very convenient, but inefficient. For complex queries, the order of consideration should be regular---MapReduce--$where
    23. Db.foo.find ({"$where": "function () {return this.x + This.y = = 10;}"}) //$where can support JavaScript functions as query criteria
    24. Db.foo.find (). Sort ({"x": 1}). Limit (1). Skip (10); //Return to paragraph (10, 11], sorted by "X", the order of the three limit is arbitrary, should try to avoid using Large-number in Skip

MongoDB Simple Query statement

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.