安装mongo后可在命令提示符中使用mongo命令:
mongo回车连接本地数据库
mongo IP:port 连接其他数据库
show dbs 查看所有数据库
use database 使用数据库
db 查看当前使用数据库
(如需验证:db.auth('name','password')用户验证)
show collections 查看表
批量修改:
db.collection.find({过滤条件e.g."fieldName":2}).forEach(
function(item){
item.fieldName1 = item.fieldName1.substr(1);
item.fieldName2 = item.fieldName2.substr(1);
db.collection.save(item);
})
批量删除:
db.collection.remove({"":""}) //删除符合条件的数据
db.collection.remove() //删除所有数据
db.collection.update(criteria,objNew,upsert,multi)
参数说明:
criteria:查询条件
objNew:update对象和一些更新操作符
upsert:如果不存在update的记录,是否插入objNew这个新的文档,true为插入,默认为false,不插入。
multi:默认是false,只更新找到的第一条记录。如果为true,把按条件查询出来的记录全部更新。
db.collection.update({"count":{$gt:20}},{$set:{"name":"c3"}},false,true) //更新所有符合条件
db.collection.update({"count":{$gt:20}},{$set:{"name":"c3"}}) //只更新一条记录
模糊查询:
对某一字段的值模糊查询
db.collection.find({'fieldName' : {$regex :/keyword/}})
如果字段下为数组,对数组下某一字段模糊查询:
db.collection.find({'fieldName.arrayFieldName' : {$regex :/keyword/}})
例如对于可视化工具RoboMongo来说,只查看表中部分字段信息
db.collection.find({}, {fieldName1 : 1, fieldName2 : 1})
按指定字段排序:
db.collection.find({}).sort({"fieldName":-1})
//1:正序;-1:倒序