node.js - mongo update content in nested structure
PHP中文网
PHP中文网 2017-05-02 09:26:29
0
3
594

A questions table, which stores all the questions. Each question has a key of answers, which is an array object. Each answer has its own _id and an up attribute. Now I want to pass this _id to update the up attribute.
How do seniors do it? I tried a lot but still can’t update it correctly, thank you qaq

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
刘奇
db.questions.update({
    "answers._id": ObjectId("58f575ca09c601c45f6c281a")
}, {
    $set: {
        "answers.$.up": 1
    }
});

Note the $ operator, we call it Positional Operator, please see the link for more information.

滿天的星座

I think you shouldn’t nest the data table so deeply. I think it’s more reasonable to build a relational table.

过去多啦不再A梦

Okay, assuming you know the index of the answer you want to modify in the answers array, you can use question.answers.index.up to update it. If you don’t know the index now, you can use $ to place it, and use {answers._id for Filter. :123456} update using {answers.$.up:321}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template