本文共 1007 字,大约阅读时间需要 3 分钟。
判断是否存在:
db.course.find({"filename": { $exists: true } }).count()移除字段:db.course.update({},{$unset:{"filename":""}},{multi:true})db.test.update({},{$unset:{"file_path":""}},{multi:true})批量增加字段:db.course.update({},{$set:{"filename":"mongo"}},{multi:true})db.test.update({},{$set:{"UpdateMark":"true"}},{multi:true})查询字段值相同的数量:db.test.find({"$where": "this.fields1 == this.fields2"}).count()重命名键:db.test.update({}, {$rename : {"name" : "NAME"}}, false, true)
导出json:mongoexport -u root -p "admin123" -d test_db -c test -o xxxxxxx.json --authenticationDatabase "admin"
-u:用户名
-p:密码
-d:目标数据库
-c:数据库collections(表名)
-o:导出文件名
--authenticationDatabase:选填
导入json:mongoimport -u root -p "admin123" -d test_db -c test --file xxxxxxx.json --authenticationDatabase "admin"
--file:导入文件
pymongo的部分操作:
from bson import ObjectId
import pymongoconn = pymongo.MongoClient('mongodb://root:123456@127.0.0.1/admin')db = conn.test_dbstr = "5sdfadsfasdfxzcasdfasdfas"findinfo = db.Report.find({"_id" : ObjectId(str)})转载地址:http://jxpez.baihongyu.com/