如何插入MongoDB中内数阵列的最后一项的数据

发布于 2025-02-01 04:15:54 字数 4364 浏览 2 评论 0原文

我正在尝试将数据插入MongoDB阵列的最后一个元素中。在第一个元素中插入使用0运算符,但是我对如何插入最后一个元素感到困惑。要解释它,请考虑以下数据:

[
    {
        "_id": "1",
        "company": "turivius",
        "forms": [
            {
                "current_version": 1,
                "history": [
                    {
                        "content": [],
                        "layout": [],
                        "responses": [
                            {
                                "client_id": 100,
                                "response_date": datetime(2022, 5, 17, 3, 0),
                                "values": [
                                    {
                                        "field_id": 0,
                                        "primeiro user": "primeiro form, resposta 1",
                                    },
                                    {
                                        "field_id": 1,
                                        "value": "aaa",
                                    },
                                ],
                            },
                            {
                                "client_id": 100,
                                "response_date": datetime(2022, 5, 17, 3, 0),
                                "values": [
                                    {
                                        "field_id": 0,
                                        "primeiro user": "primeiro form, resposta 2",
                                    },
                                    {
                                        "field_id": 1,
                                        "value": "bbb",
                                    },
                                ],
                            },
                        ],
                        "version": 0,
                    },
                    {
                        "content": [],
                        "layout": [],
                        "responses": [
                            {
                                "client_id": 100,
                                "response_date": datetime(2022, 5, 17, 3, 0),
                                "values": [
                                    {
                                        "field_id": 0,
                                        "primeiro user": "primeiro form, resposta 1 v1",
                                    },
                                    {
                                        "field_id": 1,
                                        "value": "aaa",
                                    },
                                ],
                            }
                        ],
                        "version": 1,
                    },
                ],
                "id": "33b66684-24a9-4a45-a12f-27a330152ac8",
                "is_active": True,
            },
            {
                "current_version": 0,
                "history": [],
                "id": "fa2eb9a1-c7c4-4b64-b682-7f51658bc4ab",
                "is_active": False,
            },
        ],
        "user_id": "1",
    },
    {
        "_id": "2",
        "company": "turivius",
        "forms": [
            {
                "current_version": 0,
                "history": [],
                "id": "565656",
                "is_active": True,
            },
        ],
        "user_id": "9999",
    },
]

我想用user_id = 1,form.id = 33B66684-24A9-4A9-4A45-A12F插入文档中的数据。 -27A330152AC8 和在历史记录中,其中版本 = 1我想插入一个新的wenders> wenders

{
    "client_id": 100,
    "response_date": datetime(2022, 5, 17, 3, 0),
    "values": [
        {
            "field_id": 0,
            "test": "test",
        },
        {
            "field_id": 1,
            "test2": "test3",
        },
    ],
}

版本1是最后一个元素,如果我想要在第一个元素中插入以下代码会起作用:

find_one_and_update(
            {"$and": [{"user_id": "1"}, {"forms.id": '33b66684-24a9-4a45-a12f-27a330152ac8'}]},
            {
                "$push": {"forms.$.history.0.responses": data_here},
            },
            return_document=ReturnDocument.AFTER,
)

无论如何是否有这样做,还是我需要更复杂的查询才能做到这一点?

I am trying to insert data in the last element of an array in MongoDB. insert in the first element works using the 0 operator, but I'm confused about how to insert in the last element. To explain it, consider the following data:

[
    {
        "_id": "1",
        "company": "turivius",
        "forms": [
            {
                "current_version": 1,
                "history": [
                    {
                        "content": [],
                        "layout": [],
                        "responses": [
                            {
                                "client_id": 100,
                                "response_date": datetime(2022, 5, 17, 3, 0),
                                "values": [
                                    {
                                        "field_id": 0,
                                        "primeiro user": "primeiro form, resposta 1",
                                    },
                                    {
                                        "field_id": 1,
                                        "value": "aaa",
                                    },
                                ],
                            },
                            {
                                "client_id": 100,
                                "response_date": datetime(2022, 5, 17, 3, 0),
                                "values": [
                                    {
                                        "field_id": 0,
                                        "primeiro user": "primeiro form, resposta 2",
                                    },
                                    {
                                        "field_id": 1,
                                        "value": "bbb",
                                    },
                                ],
                            },
                        ],
                        "version": 0,
                    },
                    {
                        "content": [],
                        "layout": [],
                        "responses": [
                            {
                                "client_id": 100,
                                "response_date": datetime(2022, 5, 17, 3, 0),
                                "values": [
                                    {
                                        "field_id": 0,
                                        "primeiro user": "primeiro form, resposta 1 v1",
                                    },
                                    {
                                        "field_id": 1,
                                        "value": "aaa",
                                    },
                                ],
                            }
                        ],
                        "version": 1,
                    },
                ],
                "id": "33b66684-24a9-4a45-a12f-27a330152ac8",
                "is_active": True,
            },
            {
                "current_version": 0,
                "history": [],
                "id": "fa2eb9a1-c7c4-4b64-b682-7f51658bc4ab",
                "is_active": False,
            },
        ],
        "user_id": "1",
    },
    {
        "_id": "2",
        "company": "turivius",
        "forms": [
            {
                "current_version": 0,
                "history": [],
                "id": "565656",
                "is_active": True,
            },
        ],
        "user_id": "9999",
    },
]

I want to insert data in the document with user_id = 1, form.id = 33b66684-24a9-4a45-a12f-27a330152ac8 and in the history array where the version = 1 I want to insert a new response:

{
    "client_id": 100,
    "response_date": datetime(2022, 5, 17, 3, 0),
    "values": [
        {
            "field_id": 0,
            "test": "test",
        },
        {
            "field_id": 1,
            "test2": "test3",
        },
    ],
}

The history with version 1 is the last element, if I wanted insert in the first element the following code would works:

find_one_and_update(
            {"$and": [{"user_id": "1"}, {"forms.id": '33b66684-24a9-4a45-a12f-27a330152ac8'}]},
            {
                "$push": {"forms.$.history.0.responses": data_here},
            },
            return_document=ReturnDocument.AFTER,
)

Is there anyway to do it that way or I need a more complex query to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-02-08 04:15:54

当这些条件为真时,您需要在响应数组中添加新文档:

{
  user_id: '1',
  'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
  'forms.history.version': 1
}

forms是一个数组,因此您想更新与条件匹配的数组元素,因此您可以使用 $ [dissentifier] 操作员。在表单数组中,您只想使用特定版本更新历史记录,因此您需要多次使用$ [dissidenifier]运算符,您要更新的字段将是这样:

forms.$[f].history.$[h].responses 

f是与条件和h匹配其他条件的历史记录数组的元素相匹配的每个元素的标识符。 arrayfilters对象用于定义条件。

完整的查询可能是这样的:

db.collection.update({
  user_id: "1",
  'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
  'forms.history.version': 1
},
{
  $push: {
    'forms.$[f].history.$[h].responses': {
      "client_id": 100,
      "response_date": "datetime(2022, 5, 17, 3, 0)",
      "values": [
        {
          "field_id": 0,
          "test": "test",              
        },
        {
          "field_id": 1,
          "test2": "test3",              
        },            
      ],          
    }
  }
},
{
  arrayFilters: [
    {
      'h.version': 1
    },
    {
      'f.id': '33b66684-24a9-4a45-a12f-27a330152ac8'
    }
  ]
})

在mongodb playground上工作示例

You want to add a new document to the responses array when those conditions are true:

{
  user_id: '1',
  'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
  'forms.history.version': 1
}

forms is an array, so you want to update the array element that matches the condition, so you can use the $[identifier] operator. Inside the array of forms, you only want to update the history with a specific version, so you need to use the $[identifier] operator multiple times, the field you want to update would be like this:

forms.$[f].history.$[h].responses 

f is the identifier of each elements in forms that matches the condition and h the elements of the history array that match the other condition. An arrayFilters object is used to define the conditions.

The complete query could be like this:

db.collection.update({
  user_id: "1",
  'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
  'forms.history.version': 1
},
{
  $push: {
    'forms.$[f].history.$[h].responses': {
      "client_id": 100,
      "response_date": "datetime(2022, 5, 17, 3, 0)",
      "values": [
        {
          "field_id": 0,
          "test": "test",              
        },
        {
          "field_id": 1,
          "test2": "test3",              
        },            
      ],          
    }
  }
},
{
  arrayFilters: [
    {
      'h.version': 1
    },
    {
      'f.id': '33b66684-24a9-4a45-a12f-27a330152ac8'
    }
  ]
})

Working example at MongoDB playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文