Mongoclient:如何设置embedded式文档的值
我在nodejs中使用MongoClient。 我想更新一个特定的embedded式文档值,但我不知道该怎么做。 在mongo shell中,它像{$set{doc.doc : "test"}}
但是,当我试图在节点中以完全相同的方式使用它时,它给了我一个错误,即点不能被忽略。
我试过它作为一个string,这也不工作。
有人得到这个问题的解决scheme吗?
编辑:在Mongodb的Json文档:
{ name : test, doc : {}, }
我想将以下键值对添加到“文档”文档test:test
相关的代码部分(我怎么想它应该工作)
db.collection("test").update({name:test},{$set:{doc.test:test}}, callback)
您可以尝试使用数组语法样式在variables中设置更新对象,例如:
var update = { $set: {} }; update["$set"]["doc"]["test"] = "test"; // -> same as update = {"$set": {"doc": {"test": "test" } } } var query = {"name": "test"}; db.collection("test").update(query, update, callback);