gpt4 book ai didi

mongodb - 如何更新嵌套子文档数组中的子文档

转载 作者:可可西里 更新时间:2023-11-01 10:43:13 24 4
gpt4 key购买 nike

将 mongodb 与 pymongo 结合使用,我有以下文档:

{
"_id" : ObjectId("5515d697453d9a1975123e0b"),
"Study_Attributes" : [
{
"value" : "183",
"tag" : "height",
"unit" : "cm"
},
{
"value" : "92",
"tag" : "weight",
"unit" : "kg"
}
],
"Center_Project_Name" : "prj001",
"Study_Abstract" : "THIS IS THE ABSTRACT",
"Study_Description" : "---",
"Study_Title" : "Study of Wheat",
"Center_Name" : "cc001",
"samples" : [
{
"Scientific_Name" : "aribido",
"Anonymized_Name" : "XXX001",
"Description" : "cress",
"Characteristics" : [
{
"value" : "",
"id" : ObjectId("5515d6f5453d9a1975123e0c"),
"tag" : "",
"unit" : ""
}
],
"Sample_Name" : "XXX001",
"Common_Name" : "mustard cress",
"Term_Accession_Number" : "19879",
"Individual_Name" : "xx1",
"Taxon_ID" : "19879",
"_id" : ObjectId("5515d6f5453d9a1975123e0d"),
"Term_Source_REF" : "TODO:ONTOTLOGY_ID",
"Protocol_REF" : "TODO:PROTOCOL_STRING",
"Source_Name" : "cthulu"
}
]
}

我想更新示例子文档(这是一个数组元素,因为可能有多个示例)。我有以下代码,但它不起作用...

EnaCollections.update(
{"_id": o.ObjectId(study_id), "samples._id": o.ObjectId(sample_id)},
{'$set:': {
"samples.$.Source_Name": sample['Source_Name'],
"samples.$.Characteristics": spec_attr,
"samples.$.Term_Source_REF": "TODO:ONTOLOGY",
"samples.$.Protocol_REF": "TODO:PROTOCOL_STRING",
"samples.$.Sample_Name": sample['Anonymized_Name'],
"samples.$.Individual_Name": sample['Individual_Name'],
"samples.$.Description": sample['Description'],
"samples.$.Taxon_ID": sample['Taxon_ID'],
"samples.$.Scientific_Name": sample['Scientific_Name'],
"samples.$.Common_Name": sample['Common_Name'],
"samples.$.Anonymized_Name": sample["Anonymized_Name"],
}}
)

谁能告诉我这有什么问题吗?

最佳答案

查看您的更新语句,您在其中指定了带有冗余 $set 修饰符:

...{'$set:': {...

只需删除它,一切都会正常:)

关于mongodb - 如何更新嵌套子文档数组中的子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29349839/

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