- 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 =
- Db.users.find ({"username": "Joe", "age": + }) select * from users where "username" = "Joe" and a GE =
- 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 >= <= //$lt (<) $lte (<=) $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 (7 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 = true
- Db.users.find ({"Id_num": {"$mod": [5, 1]}}) select * from Users where (id_num mod 5) = 1 /c4>
- Db.users.find ({"$not": {"age": +} }) select * from the users where not (age = $ )
- 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
- 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 /c3>
- 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.
- Db.people.find ({"Name.first": "Joe", "name.last": "Schmoe"}) //nested query
- Db.blog.find ({"comments": {"$elemMatch": {"author": "Joe", "score": {"$gte": 5}}}) //nested queries, which are used only when nested elements are arrays,
- 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
- Db.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 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