Mongodb笔记
1、forEach
var opsUsers = db.getCollection('clients').find({},{"opsUsers":1}).limit(1) opsUsers.forEach(function(x){ printjson(db.getCollection("clients").findOne({_id:ObjectId("5b63dda54a19d8235f381221")})) })
注意点:如果是查询的化,findOne()可正常打印结果,但是find只会打印出一个对象的引用
也可以在循环内更新
2、动态的为属性赋值,可以在循环内调用
var i = 0; var whereStr = {_id:ObjectId("5b63dda54a19d8235f381221")}; var setStr = {} setStr["opsUsers." + i + ".nickname"] = "ops_1"; var updateStr = {$set:setStr}; db.getCollection("clients").updateOne(whereStr,updateStr);