mongo //连接mongodb
show dbs //显示数据库
use test //切换数据库

插入新数据

db.users.insert({"name":"Allen","age":26,"sex":"M"}) //直接插入,若主键存在,则失败
db.users.save({"name":"Joy","age":20,"sex":"F","email":"joy@gmail.com"}) //主键不存在则插入,存在则更新

准备以下数据

db.users.insert({"name":"Allen","age":26,"sex":"M"})
db.users.insert({"name":"Joy","age":20,"sex":"F","email":"joy@gmail.com"})
db.users.insert({"name":"lancy","age":25,"sex":"F","email":"lancy@gmail.com","lang":["ja","en"]})
db.users.insert({"name":"lily","age":22,"sex":"F","email":"lily@gmail.com","lang":["ja","ru"]})

查找集合数据

db.users.find() //查询所有
db.users.find({"name":"Allen"}) //按条件查询
db.users.find({"name":"Allen"},{"name":1,"age":1}) //按条件查询并返回指定键值
db.users.find().sort({"age":-1}) //查找后排序
db.users.find({"age":{"$gt":20},"sex":"M"}) //查找age大于20,且sex为M的记录

删除数据

db.users.remove() //删除所有
db.users.remove({"name":"Joy"}) //按条件删除

修改数据

db.users.update({"name":"Allen"},{"age":25}) //覆盖式修改,修改整个数据
db.users.update({"name":"Allen"},{"$set":{"email":"allen@gmail.com"}}) //添加式修改,仅添加/修改email字段

$all

db.users.find({"lang":{"$all":["en","ja"]}}) //查找包含所有元素的记录

mongo $all

$in

db.users.find({"lang":{"$in":["en","ja"]}}) //查找包含项元素的记录

mongo $in

$nin

db.users.find({"lang":{"$nin":["en","ja"]}}) //查找不包含项元素的记录

mongo $nin

$exists

db.users.find({"lang":{"$exists":1}}) //查找存在指定字段的记录

mongo $exists

$mod

db.users.find({"age":{"$mod":[5,0]}}) //查找模5余0的记录

mongo $mod

$or

db.users.find({"$or":[{"name":"Allen"},{"name":"Joy"}]}) //查找或条件

mongo $or

$nor

db.users.find({"$nor":[{"name":"Allen"},{"name":"Joy"}]}) //查找既不,也不条件记录

mongo $nor

$size

db.users.find({"lang":{"$size":3}}) //查找数组字段长度为指定值的记录

mongo $size

$slice

db.users.find({"name":/jim/i},{"lang":{"$slice":[0,1]}}) //取出记录中的数组片段

mongo $slice

distinct()

db.users.distinct("sex") //查找不重复的字段

mongo distinct

count()

db.users.find({"age":{"$gt":20}}).count(1) //查找结果计数

mongo count

添加索引

db.users.find({"email":"joy@gmail.com"}).explain()

mongo before index

db.users.ensureIndex({"name":1})
db.users.find({"name":"Allen"}).explain()

mongo indexed

更新

db.users.update(criteria,newData,upsert,multi)

$unset

db.users.update({"name":"Allen"},{"$unset":{"city":1}}) //删除某个字段

mongo $set $unset

$push

db.users.update({"name":"Allen"},{"$push":{"lang":"en"}})

mongo $push

$pushAll

db.users.update({"name":"Allen"},{"$pushAll":{"lang":["zh","ja"]}})

mongo $pushAll

$addToSet

//插入不重复数据

mongo $addToSet

$pull

db.users.update({"name":"Allen"},{"$pull":{"lang":"en"}})

mongo $pull

$pullAll

db.users.update({"name":"Allen"},{"$pullAll":{"lang":["ja","zh"]}})

mongo $pullAll

$pop

db.users.update({"name":"lily"},{"$pop":{"lang":1}}) //弹出lang数组中的最后一个元素,value为-1时弹出第一个

mongo $pop

$rename

db.users.update({"name":"Allen"},{"$rename":{"lang":"hobby"}})

mongo $rename

mongoexport -d DATABASE -c COLLECTION -o FILE
mongoimport -d DATABASE -c COLLECTION FILE

mongodump -d DATABASE -o DIR
mongorestore -d DATABASE DIR

默认admin登陆,添加–auth参数启用用户验证,admin数据库中
db.addUser(‘root’,’1234′)
db.auth(‘root’,’1234′)

不想安装mongodb?去mongodb web shell小试身手
https://try.mongodb.org/mongo web shell

Post Navigation