gpt4 book ai didi

node.js - 蒙古客户端 : how to set a value of an embedded document

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:13 25 4
gpt4 key购买 nike

我在nodejs中使用MongoClient。我想更新特定的嵌入文档值,但我不知道该怎么做。在 mongo shell 中,它就像一个魅力 {$set{doc.doc : "test"}}

但是当我尝试在 Node 中以完全相同的方式使用它时,它给了我一个错误,即该点不是不可理解的。

我已经尝试过将其作为字符串,但也不起作用。

有人能解决这个问题吗?

编辑:Mongodb 中的 Json 文档:

{
name : test,
doc : {},
}

我想将以下键值对添加到“doc”文档中测试:测试

相关代码部分(我认为它应该如何工作)

db.collection("test").update({name:test},{$set:{doc.test:test}}, callback)

最佳答案

您可以尝试使用数组语法样式在变量中设置更新对象,例如:

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);

关于node.js - 蒙古客户端 : how to set a value of an embedded document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29685869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com