search

Home  >  Q&A  >  body text

嵌套文档修改 - mongodb第三级嵌套数组修改问题

现在的数据结构是这样的

course:{
    name:String,
    chapter: [{ 
            name: String,
            knowledge: [{ 
                name: String,
                exam: [{ 
                    name: String, 
                }]
            }]
        }]
}

要想修改knowledge.name,修改语句该怎么写?之前在修改chapter.name时我是这样写的

Course.update({
                "chapter._id": req.body.id
            }, {
                $set: {
                    "chapter.$.name": name, //名称
                }
            })
            .exec(function(err, num) {


            });

不知道在修改第三级嵌套文档时,$定位符改怎么使用。

怪我咯怪我咯2796 days ago894

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-24 16:01:39

    Actually, it’s complicated to think about. Nested queries, nested modifications, etc. If you find nodes layer by layer, the efficiency is not very high.
    Document databases, to put it bluntly, are always a string of strings in a json-like format, no matter how deep the nesting is.
    The fastest and most direct way is as follows:

    jsondb.term.update({"_id" : ObjectId("54c1a899eb21ac9c995d3eb2")}, {$set:{
        "_id" : ObjectId("54c1a899eb21ac9c995d3eb2"),
        "course" : {
            "name" : "String",
            "chapter" : [ 
                {
                    "name" : "String",
                    "knowledge" : [ 
                        {
                            "name" : "原来如此",
                            "exam" : [ 
                                {
                                    "name" : "String"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }})
    

    Just make sure your program remains unchanged. Find the results first, modify only the name of the knowledge, and then update.
    When any database operation feels awkward, troublesome, and inefficient, you should look back and think about whether the structural design of the database can be improved. This applies to both relational and non-relational databases. http://www.cnblogs.com/mokafamily/p/4102829.html, you can take a look at the section "1. Normalization and de-normalization" in this article

    reply
    0
  • Cancelreply