MongoDB查询更新操作语句命令大全
SQL:SELECT * FROM users
db.users.find()
SQL:SELECT * FROM users WHERE username= 'Lucy'
db.users.find({
"username" : "Lucy"})
SQL:SELECT * FROM users WHERE username = "Lucy" AND age = 18
db.users.find({
"username" : "Lucy", "age" : 18})
SQL:SELECT username, email FROM users
db.users.find({}, {
"username" : 1, "email" : 1}) // 只返回`username`, `email`
_id
返回)SQL:SELECT username FROM users
db.users.find({}, {
"username" : 1, "_id" : 0}) // 阻止`_id`返回:查询特定列,其中MongoDB的`_id`列是默认查询出来的,不管该记录中是否存在`username`这个字段,`userName:1`就是显示该字段,`userName:0`就是不显示该字段,所以要不显示`_id`,就可以加上`_id:0`即可
SQL:SELECT * FROM users WHERE age >=18 AND age <= 30
db.users.find({
"age" : {
"$gte" : 18, "$lte" : 20}}) // $lt(<) $lte(<=) $gt(>) $gte(>=)
username
的值不等于Lucy
的这个条件的所有字段SQL:SELECT * FROM users WHERE username <> "Lucy"
db.users.find({
"username" : {
"$ne" : "Lucy"}})
IN
SQL:SELECT * FROM users WHERE age IN(18, 20, 22)
db.users.find({
"age" : {
"$in" : [18, 20, 22]}})
NOT IN
SQL:SELECT * FROM users WHERE age NOT IN(18, 20, 22)
db.users.find({
"age" : {
"$nin" : [18, 20, 22]}})
OR
SQL:SELECT * FROM users WHERE age = 18 OR adopt = true
db.users.find({
"$or" : [{
"age" : 18}, {
"adopt" : true}]})
NOT
SQL:SELECT * FROM users WHERE NOT(age = 18)
db.users.find({
"$not": {
"age" : 18}})
find({"username" : null})
进行查询,那么连带"没有username"
的纪录一并筛选出来SQL:SELECT * FROM users WHERE username IS NULL
db.users.find({
"username" : {
"$in" : [null], "$exists" : true}})
LIKE
模糊查询SQL:SELECT * FROM users WHERE username LIKE "%A%"
db.users.find({
"username" : /A/}) // `username`包含`A`字母的数据(`%A%`)
LIKE
模糊查询(以x开头)SQL:SELECT * FROM users WHERE username LIKE "A%"
db.users.find({
"username" : /^A/}) // `username`以字母A开头的数据(`A%`)
ORDER BY
字段ASC
排序SQL:SELECT * FROM users WHERE username = "Lucy" ORDER BY _id ASC
db.users.find({
"username ": "Lucy"}).sort({_id : 1})
ORDER BY
字段DESC
排序SQL:SELECT * FROM users WHERE userName ="Lucy" ORDER BY _id DESC
db.users.find({
"username ": "Lucy"}).sort({_id : -1})
// 正则查询,value是符合PCRE的表达式
db.users.find({
"name" : /joey?/i})
// 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录
db.users.find({
fruit : {
$all : ["apple", "banana"]}})
// 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录
db.users.find({
"fruit.2" : "peach"})
// 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用
db.users.find({
"fruit" : {
"$size" : 3}})
// 对数组的查询,只返回数组comments中的前十条,还可以`{"$slice" : -10}, {"$slice" : [23, 10]};` 分别返回最后10条,和中间10条
db.users.findOne(criteria, {
"comments" : {
"$slice" : 10}})
// 嵌套查询
db.users.find({
"name.first" : "Joe", "name.last" : "Schmoe"})
// 嵌套查询,仅当嵌套的元素是数组时使用
db.users.find({
"comments" : {
"$elemMatch" : {
"author" : "joe", "score" : {
"$gte" : 5}}}})
// 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where
db.users.find({
"$where" : "this.x + this.y == 10"})
// $where可以支持javascript函数作为查询条件
db.users.find({
"$where" : "function() { return this.x + this.y == 10; }"})
// 返回第(10, 11]条,按"x"进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number
db.users.find().sort({
"x" : 1}).limit(1).skip(10)
db.form_product.find({
"xisShelf": true}).forEach(function(item){
db.form_product.update({
"id": item.id},{
$set:{
"xisShelf": false}});
})