NodeJS中的Array数组中的猫鼬更新值

时间:2020-02-24 14:47:08

标签: node.js mongodb mongoose mongoose-schema

我的测试模式:

var TestSchema = new Schema({
    testName: String,
    topic: {
        topicTitle: String,
        topicQuestion: [
            {
                questionTitle: String,
                choice: [
                    {
                        name: String
                        age: Number
                    }
                ]
            }
        ]
    }
}, { collection: 'test' });
var Test = mongoose.model('test', TestSchema);

我想更新一个具有选择ID的 age ($ inc)值。

我可以具有测试ID topicQuestion ID 选择ID

如何在NodeJS中用猫鼬编写此查询?

通常我使用以下查询来更新值:

Test.findOneAndUpdate({ _id: testId }, { $inc: { ... } }, function (err, response) {
   ...
});

但是很难进入一个数组和另一个数组。谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用$[] positional operator更新嵌套数组。

router.put("/tests/:testId/:topicQuestionId/:choiceId", async (req, res) => {
  const { testId, topicQuestionId, choiceId } = req.params;

  const result = await Test.findByIdAndUpdate(
    testId,
    {
      $inc: {
        "topic.topicQuestion.$[i].choice.$[j].age": 1
      }
    },
    {
      arrayFilters: [{ "i._id": topicQuestionId }, { "j._id": choiceId }],
      new: true
    }
  );
  res.send(result);
});

假设我们有此现有文档:

{
    "_id" : ObjectId("5e53e7d9bf65ac4f5cbf2116"),
    "testName" : "Test 1",
    "topic" : {
        "topicTitle" : "Title",
        "topicQuestion" : [
            {
                "_id" : ObjectId("5e53e7d9bf65ac4f5cbf211a"),
                "questionTitle" : "Question 1 Title",
                "choice" : [
                    {
                        "_id" : ObjectId("5e53e7d9bf65ac4f5cbf211c"),
                        "name" : "A",
                        "age" : 1
                    },
                    {
                        "_id" : ObjectId("5e53e7d9bf65ac4f5cbf211b"),
                        "name" : "B",
                        "age" : 2
                    }
                ]
            },
            {
                "_id" : ObjectId("5e53e7d9bf65ac4f5cbf2117"),
                "questionTitle" : "Question 2 Title",
                "choice" : [
                    {
                        "_id" : ObjectId("5e53e7d9bf65ac4f5cbf2119"),
                        "name" : "C",
                        "age" : 3
                    },
                    {
                        "_id" : ObjectId("5e53e7d9bf65ac4f5cbf2118"),
                        "name" : "D",
                        "age" : 4
                    }
                ]
            }
        ]
    },
    "__v" : 0
}

如果我们想增加给定选择的age值,则使用类似PUT的端点发送http://.../tests/5e53e7d9bf65ac4f5cbf2116/5e53e7d9bf65ac4f5cbf211a/5e53e7d9bf65ac4f5cbf211b请求,其中

"testId": "5e53e7d9bf65ac4f5cbf2116"

"topicQuestionId": "5e53e7d9bf65ac4f5cbf211a"

"choiceId": "5e53e7d9bf65ac4f5cbf211b"

答案 1 :(得分:0)

您需要告知所需的选择,并且在更新部分,需要更改增量方式。

示例:

Test.findOneAndUpdate({ _id: testId, topicQuestion.choice._id: choiceId}, { 'topicQuestion.$.choice': {$inc: { age: <numberToIncrement> }}}, {new: true}, function (err, response) {
   ...
});