0

我正在尝试使用点符号和 $ 运算符更新 MongoDB 中的多级嵌入式文档。下面,我有一个由一个文档组成的集合:

{
"_id" : ObjectId("55da48520549875d8480707c"),
"queried" : [],
"field" : "materials science",
"subfields" : [ 
    {
        "subfield_name" : "electronic materials",
        "queried" : [],
        "subfields_2" : [ 
            {
                "subfield_2_name" : "electronics",
                "queried" : [],
                "keywords" : [ 
                    {
                        "queried" : [],
                        "name" : "silicon"
                    }, 
                    {
                        "queried" : [],
                        "name" : "graphene"
                    }, 
                    {
                        "queried" : [],
                        "name" : "carbon nanotube"
                    }, 
                    {
                        "queried" : [],
                        "name" : "black phosphorus"
                    }, 
                    {
                        "queried" : [],
                        "name" : "phophorene"
                    }, 
                    {
                        "queried" : [],
                        "name" : "molybdenum disulphide"
                    }
                ],
            }, 
            {
                "subfield_2_name" : "dielectrics",             
                "queried" : [],
                "keywords" : [ 
                    {
                        "queried" : [],
                        "name" : "silicon oxide"
                    }, 
                    {
                        "queried" : [],
                        "name" : "aluminum oxide"
                    }, 
                    {
                        "queried" : [],
                        "name" : "hafnium dioxide"
                    }, 
                    {
                        "queried" : [],
                        "name" : "hexagonal boron nitride"
                    }, 
                    {
                        "queried" : [],
                        "name" : "Zirconium dioxide"
                    }
                ],
            },
        ],
    }
]
}

我想通过以下方式更新此集合:
1)查询文档以匹配"name"关键字数组(在第三级嵌入文档中)中的键与值"carbon nanotube"

2)然后我想将时间戳(time())附加到"queried":[]键数组中具有键值对的同一嵌入文档"name":"carbon nanotube"

{
"_id" : ObjectId("55da48520549875d8480707c"),
"queried" : [],
"field" : "materials science",
"subfields" : [ 
    {
        "subfield_name" : "electronic materials",
        "queried" : [],
        "subfields_2" : [ 
            {
                "subfield_2_name" : "electronics",
                "queried" : [],
                "keywords" : [ 
                    {
                        "queried" : [],
                        "name" : "silicon"
                    }, 
                    {
                        "queried" : [],
                        "name" : "graphene"
                    }, 
                    {
                        "queried" : [1359147763.02],
                        "name" : "carbon nanotube"
                    }, 
                    {
                        "queried" : [],
                        "name" : "black phosphorus"
                    }, 
                    {
                        "queried" : [],
                        "name" : "phophorene"
                    }, 
                    {
                        "queried" : [],
                        "name" : "molybdenum disulphide"
                    }
                ],
            }, 
            {
                "subfield_2_name" : "dielectrics",             
                "queried" : [],
                "keywords" : [ 
                    {
                        "queried" : [],
                        "name" : "silicon oxide"
                    }, 
                    {
                        "queried" : [],
                        "name" : "aluminum oxide"
                    }, 
                    {
                        "queried" : [],
                        "name" : "hafnium dioxide"
                    }, 
                    {
                        "queried" : [],
                        "name" : "hexagonal boron nitride"
                    }, 
                    {
                        "queried" : [],
                        "name" : "Zirconium dioxide"
                    }
                ],
            },
        ],
    }
]
}

我知道这都可以在 update() 命令中完成:

topics.collection.update({"subfields.subfields_2.keywords.name":"carbon nanotube"}, {$push: {"subfields.subfields_2.keywords.$.queried":time()}})

但我认为我的点符号存在错误,因为我收到了一个错误SyntaxError: invalid syntax。我是否需要更改我的架构才能执行此 update() 或者是否有其他方法来更新此嵌入式文档

4

1 回答 1

0

This isn't possible with the current schema. In order to perform updates in an array without explicitly specifying the position of the element, you need to use the positional $ operator. Because there are 2 nested queries your update would need to be (note I've added another $):

{$push: {"subfields.subfields_2.$.keywords.$.queried":time()}}

But this won't work because the $ operator cannot be used for nested arrays - you can only have one positional $ operator in the update.

You will need to modify your schema to support this.

For example you could store all subfields in their own individual documents, and link to their parent documents to allow navigation of the tree structure (an index on the parent field might be needed)

{
    "_id" : ObjectId("55da48520549875d84800000"),
    "queried" : [],
    "field" : "materials science"
}

{
    "_id" : ObjectId("55da48520549875d84801111"),
    "queried" : [],
    "field" : "electronic materials",
    "parent" : ObjectId("55da48520549875d84800000")
}

{
    "_id" : ObjectId("55da48520549875d84802222"),
    "queried" : [],
    "field" : "electronics",
    "parent" : ObjectId("55da48520549875d84801111"),
    "keywords" : [{
            "queried" : [],
            "name" : "silicon"
        }, {
            "queried" : [],
            "name" : "graphene"
        }, {
            "queried" : [],
            "name" : "carbon nanotube"
        }]    
}

If you'd like to look at more options to fit your requirements, the Mongo documentation has a section on how to Model Tree Structures. The example above would be Model Tree Structures with Parent References but there are other pattern thats that might be better depending on how else you query the collection.

于 2015-08-30T18:40:05.680 回答