MongoDB and MySQL operations
On the left is the mongodb query statement, and on the right is the SQL statement. It is easy to use.
Db. users. find () select * from users
Db. users. find ({"age": 27}) select * from users where age = 27
Db. users. find ({"username": "joe", "age": 27}) select * from 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 // Column Filtering is added immediately, and _ id is returned; must explicitly block _ id returned
Db. users. find ({"age": {"$ gte": 18, "$ lte": 30 }}) select * from users where age> = 18 and age <= 30 // $ 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 users where ticket_no in (725,542,390)
Db. users. find ({"ticket_no": {"$ nin": [725,542,390]}) select * from 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
Db. users. find ({"$ not": {"age": 27}) select * from users where not (age = 27)
Db. users. find ({"username": {"$ in": [null], "$ exists": true }}) select * from users where username is null // If you directly query through find ({"username": null}), the records with "no username" are filtered out together.
Db. users. find ({"name":/joey? /I}) // regular query. value is a PCRE expression.
Db. food. find ({fruit :{$ all: ["apple", "banana"] }}) // query the array. The fruit field contains "apple ", it also contains "banana" records
Db. food. find ({"fruit.2": "peach"}) // queries the array. In the fruit field, 3rd (starting from 0) elements are recorded by peach.
Db. food. find ({"fruit": {"$ size": 3}) // query the array. The number of elements in the array is 3 Records, $ size cannot be used together with other operators
Db. users. findOne (criteria, {"comments": {"$ slice": 10}) // queries an array and returns only the top 10 comments in the array, you can also {"$ 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, used only when the nested element is an array,
Db. foo. find ({"$ where": "this. x + this. y = 10 "}) // complex query, $ where is of course very convenient, but inefficient. For complex queries, the order should be regular-> MapReduce-> $ where
Db. foo. find ({"$ where": "function () {return this. x + this. y = 10;} "}) // $ where supports javascript Functions as query Conditions
Db. foo. find (). sort ({"x": 1 }). limit (1 ). skip (10); // return the number (10, 11], sorted by "x"; the order of the three limit is arbitrary, avoid using large-number in the skip