db.集合.drop( ):删除整个集合,这个在实际工做中必定要谨慎使用,若是是程序,必定要二次确认。数据库
db.dropDatabase( ):删除整个数据库,在删除库时,必定要先进入数据库,而后再删除。实际工做中这个基本不用,实际工做可定须要保留数据和痕迹的。json
db.test.insert([ {"age":1}, {"age":2}, {"age":3} ])
修改update
db .集合名称.update({"id":"1"},{"$set":{age:21}})
修改嵌套内容数组
db.集合名称.update({"id:1"},$set:{user.name:"xiaoming"})
db.集合名称.update({id:1},{$unset:{age:""}})
db.集合名称.update({id:1},{$inc:{age:-2}}) //自己减2
db.集合名称。update({},{sex:'男'},{$multi:true})
db.集合名称.update({},{$set:{sex:"男"},{$upsert:true}})
db.集合名称.update({id:1},{$push:{arr:"hello"}}) // 至关于 arr.push("hello")
db.集合名称.update({id:1},{$push:{user.arr:"hello"}})
db.集合名称.update({id:1},{arr:{$ne:"hello"},{$push:{arr."hello"}})
db.集合名称.update({id:1},{$addToSet:{arr:"hello"}})
var newArr=["hello","world"]; db.集合名称.update({id:1},{$addToSet:{arr:{$each:newArr}})
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 }
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)
db.集合名称.find() //所有查询 db.集合名称.find({id:1}) //根据id等于1查询
db.集合名称.find({id:1},{name:true,age:false}) //返回数据中不含有age
db.集合名称.find({id:{$lt:5}}) //查找id字段小于5的全部数据
db.集合名称.find({id:{$in:[1,6]}}) // 查询id字段在1到6之间的数据
db.集合名称.find({id:{$or:[ {$in:[1,6]}, {age:18} ]}}) // 查询id字段在1到6之间或年龄为18岁的数据
db.集合名称.find({id:{$and:[ {$in:[1,6]}, {age:18} ]}}) // 查询id字段在1到6之间而且年龄为18岁的数据
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便可