mongoDB 经常使用语法使用

mongoDB 经常使用语法使用

删除数据库或集合

db.集合.drop( ):删除整个集合,这个在实际工做中必定要谨慎使用,若是是程序,必定要二次确认。数据库

db.dropDatabase( ):删除整个数据库,在删除库时,必定要先进入数据库,而后再删除。实际工做中这个基本不用,实际工做可定须要保留数据和痕迹的。json

批量插入

db.test.insert([  
     {"age":1},  
     {"age":2},  
     {"age":3}  
    ])
修改update

基础update修改器

$set修改器 (用来修改一个指定的键值(key))

db .集合名称.update({"id":"1"},{"$set":{age:21}})

修改嵌套内容数组

db.集合名称.update({"id:1"},$set:{user.name:"xiaoming"})

$unset用于将key删除

db.集合名称.update({id:1},{$unset:{age:""}})

$inc 用于计算而且修改

db.集合名称.update({id:1},{$inc:{age:-2}}) //自己减2

$multi选项 (用于批量插入/循环插入)

db.集合名称。update({},{sex:'男'},{$multi:true})

$upsert选项** (有更新/没有添加)

db.集合名称.update({},{$set:{sex:"男"},{$upsert:true}})

update数组修改器 (例如:集合中有一个空数组:arr=[])

$push (追加数组)

db.集合名称.update({id:1},{$push:{arr:"hello"}}) // 至关于 arr.push("hello")

嵌套属性追加数组

db.集合名称.update({id:1},{$push:{user.arr:"hello"}})

$ne 查找是否存在 (存在不追加,不存在追加)

db.集合名称.update({id:1},{arr:{$ne:"hello"},{$push:{arr."hello"}})

$addToSet (相似于$ne,她更简单一些)

db.集合名称.update({id:1},{$addToSet:{arr:"hello"}})

$each 批量追加

var newArr=["hello","world"];  
db.集合名称.update({id:1},{$addToSet:{arr:{$each:newArr}})

$pop 删除数组

db.集合名称.update({id:1},{$pop:{arr:1}}) // 1 表明尾部删除  \-1 表明头部删除

数组指定位数修改 (相似于对象形式,毕竟数组也是对象嘛)

db.集合名称.update({id:1},{$set:{arr.1:"world"}})

状态返回

db.集合名称.update({id:1},{$set:{age:18}})  
            var status=db.rancommand({getLastError:true})  
            printjson(status)   
            返回:{  
             "connectionId" : 1,  
             "updatedExisting" : true,  // 判断是否操做成功  
             "n" : 2,  
             "syncMillis" : 0,  
             "writtenTo" : null,  
             "err" : null,  
             "ok" : 1  
             }

操做安全 findAndModify 翻译: 找到并修改

var findAndModifyObj ={  
             findAndModify:"集合名称",  
             query:{id:1}, // 经过什么查找  
             sort:false, //排序  
             remove:false, 是否删除 与update冲突 只能写一个  
             upsert:false, 没有找到是否添加  
             fields:[],//须要返回的字段  
             update:{$set:{age:18}}, //更新  
             new:true    //更新完成,须要查看结果,若是为false不进行查看结果  
            }  
            var result=db.runCommand(findAndModifyObj);  
            ​  
            printjson(result)

查询 find()

简单查询

db.集合名称.find() //所有查询  
db.集合名称.find({id:1}) //根据id等于1查询

筛选字段

db.集合名称.find({id:1},{name:true,age:false}) //返回数据中不含有age

不等修饰符

image.png

db.集合名称.find({id:{$lt:5}}) //查找id字段小于5的全部数据

$in 查询一个区间

db.集合名称.find({id:{$in:[1,6]}}) // 查询id字段在1到6之间的数据

$or 条件 或

db.集合名称.find({id:{$or:[  
             {$in:[1,6]},  
             {age:18}  
            ]}}) // 查询id字段在1到6之间或年龄为18岁的数据

$and 条件 与

db.集合名称.find({id:{$and:[  
             {$in:[1,6]},  
             {age:18}  
            ]}}) // 查询id字段在1到6之间而且年龄为18岁的数据

find() 数组查询

db.集合名称.find({arr:['hello','world']}) //精确查找 只有arr等于['hello','world']  
            ​  
db.集合名称.find({arr:'hello'}) //模糊查找 arr中含有'hello'便可  
            ​  
db.集合名称.find({arr:{$all:['hello','world']}}) //而且关系 含有'hello'而且含有'world' 参能知足条件  
            ​  
db.集合名称.find({arr:{$in:['hello','world']}}) //或关系 含有'hello'或者含有'world' 参能知足条件  
            ​  
db.集合名称.find({arr:{$size:2}}) //根据数组长度查找  
            ​  
db.集合名称.find({},{arr:{$slice:2}}) //截取 返回数据只显示数组前两位 最后一项的话 直接写-1便可
相关文章
相关标签/搜索