如何更新数组中的特定值-Mongodb
我有一个mongodb用户集合,如下所示:如何更新数组中的特定值-Mongodb,mongodb,mongodb-query,Mongodb,Mongodb Query,我有一个mongodb用户集合,如下所示: { "_id" : ObjectId("5dba8987b4a39c13bc104a23"), "contact" : { "firstName" : "Mark", "lastName" : "Doe", "parentsBloodType" : [ { "type" : "AB+", },
{
"_id" : ObjectId("5dba8987b4a39c13bc104a23"),
"contact" : {
"firstName" : "Mark",
"lastName" : "Doe",
"parentsBloodType" : [
{
"type" : "AB+",
},
{
"type" : "A+",
},
],
},
"createdAt" : ISODate("2019-10-31T07:13:11.278Z"),
"updatedAt" : ISODate("2019-11-26T09:59:41.611Z")
}
我需要运行一个原始查询来从AB+更新到O-。我还想在更新之前检查它们是否匹配
我尝试了这一点,但它为用户添加了一个额外字段:
db.getCollection('users').update(
{"_id": ObjectId("5dba8987b4a39c13bc104a23")},
{"$set" : { 'parentsBloodType.' + '0' + '. type' : "O-"}}
)
这一个将进行更新:
db.users.update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23") },
{ "$set": { 'contact.parentsBloodType.0.type' : "O-"} }
)
您可以选择与此匹配的复选框:
db.users.update(
{
"_id": ObjectId("5dba8987b4a39c13bc104a23"),
"contact.parentsBloodType.type": "AB+"
},
{ "$set": { 'contact.parentsBloodType.0.type': "AB+" } }
)
仅当存在类型AB+
时(在任何父级),此操作才会更新文档
或者,如果您想先检查类型是否为AB+
,则使用
db.users.update(
{
"_id": ObjectId("5dba8987b4a39c13bc104a23"),
"contact.parentsBloodType.0.type": "AB+"
},
{ "$set": { 'contact.parentsBloodType.0.type': "AB+" } }
)
但是,我假设您实际上正在寻找以下内容:
db.users.update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23") },
{ "$set": { 'contact.parentsBloodType.$[p].type': "0-" } },
{ arrayFilters: [{ "p.type": "AB+" }] }
)
它会将任何
AB+
更新为0-
,无论它出现在数组中的哪个位置。这一个将执行更新:
db.users.update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23") },
{ "$set": { 'contact.parentsBloodType.0.type' : "O-"} }
)
您可以选择与此匹配的复选框:
db.users.update(
{
"_id": ObjectId("5dba8987b4a39c13bc104a23"),
"contact.parentsBloodType.type": "AB+"
},
{ "$set": { 'contact.parentsBloodType.0.type': "AB+" } }
)
仅当存在类型AB+
时(在任何父级),此操作才会更新文档
或者,如果您想先检查类型是否为AB+
,则使用
db.users.update(
{
"_id": ObjectId("5dba8987b4a39c13bc104a23"),
"contact.parentsBloodType.0.type": "AB+"
},
{ "$set": { 'contact.parentsBloodType.0.type': "AB+" } }
)
但是,我假设您实际上正在寻找以下内容:
db.users.update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23") },
{ "$set": { 'contact.parentsBloodType.$[p].type': "0-" } },
{ arrayFilters: [{ "p.type": "AB+" }] }
)
它会将任何AB+
更新为0-
,无论它出现在数组中的哪个位置。您可以使用它更新与查询文档匹配的第一个元素:
db.getCollection('users').update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23"), "parentsBloodType.type": "AB+" },
{ "$set" : { 'parentsBloodType.$.type' : "O-" } }
)
请注意:
- 位置$operator充当与查询文档匹配的第一个元素的占位符,并且
- 数组字段必须作为查询文档的一部分出现
db.students.update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23") },
{ $set: { "parentsBloodType.$[element].type" : "O-" } },
{
arrayFilters: [ { "element.type": "AB+" } ]
}
)
您可以使用更新与查询文档匹配的第一个元素:
db.getCollection('users').update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23"), "parentsBloodType.type": "AB+" },
{ "$set" : { 'parentsBloodType.$.type' : "O-" } }
)
请注意:
- 位置$operator充当与查询文档匹配的第一个元素的占位符,并且
- 数组字段必须作为查询文档的一部分出现
db.students.update(
{ "_id": ObjectId("5dba8987b4a39c13bc104a23") },
{ $set: { "parentsBloodType.$[element].type" : "O-" } },
{
arrayFilters: [ { "element.type": "AB+" } ]
}
)
如果双亲都有
AB+
,会发生什么情况?你想同时更新还是只更新一个?谢谢你指出,我想同时更新两个。如果父母双方都有AB+
,会发生什么?您想同时更新还是只更新一个?谢谢您指出,我想同时更新两个。请注意,multi:true
表示更新多个文档,而不是更新数组的多个值。即,无论multi:true
或multi:false
如何,它都将更新数组中的所有匹配值。实际上,当您在主键上进行筛选时,\u id:ObjectId(…)
则multi:true
是毫无意义的。请注意,multi:true
意味着更新多个文档,而不是更新数组的多个值。即,无论multi:true
或multi:false
如何,它都将更新数组中的所有匹配值。实际上,当您在主键上进行筛选时,\u id:ObjectId(…)
则multi:true
是没有意义的。