Arrays 更新Mongo集合内数组中的一条记录
大家好,我正在尝试添加一些字段,并将现有字段更新为现有mongodb文档数组。因为我是mongo的新手,所以我不太知道如何操作数组中的嵌套文档。我的数组就像Arrays 更新Mongo集合内数组中的一条记录,arrays,mongodb,mongoose,mongodb-query,nosql,Arrays,Mongodb,Mongoose,Mongodb Query,Nosql,大家好,我正在尝试添加一些字段,并将现有字段更新为现有mongodb文档数组。因为我是mongo的新手,所以我不太知道如何操作数组中的嵌套文档。我的数组就像 "productVersions" : [ { "productVersion" : "1.0", "active" : true, "effectiveDate" : &qu
"productVersions" : [
{
"productVersion" : "1.0",
"active" : true,
"effectiveDate" : "01/09/2020",
"versionDescription" : "Initial version"
},
{
"productVersion" : "2.0",
"active" : true,
"effectiveDate" : "01/09/2020",
"versionDescription" : "Initial version"
}
]
首先我想找到正确的productVersion,然后在该productVersion中更新现有字段,如果字段不存在,则添加它。
下面是我想添加到productVersion2中的一些字段,可以看到effectiveDate已经存在,必须更新,其余字段应该添加
{signatureId: 123,
signatureURL: 'www.abc.com',
effectiveDate: "01/09/2020",
configurationStatus: "active",
name: "john"}
非常感谢您的任何帮助或想法。谢谢您可以使用更新阵列。如果该字段已经存在,则会更新该字段的值,如果未添加新字段,则会添加该字段的值
db.collection.update(
{“productVersions.productVersion”:“2.0”},
{
$set:{
“productVersions.$.signatureId”:123,
“productVersions.$.signatureURL”:“www.abc.com”,
“productVersions.$.effectiveDate”:“2020年9月1日”,
“productVersions.$.configurationStatus”:“活动”,
“productVersions.$.name”:“john”,
},
}
);