MongoDB字段必须是一个数组

发布于 2025-02-01 13:13:37 字数 2570 浏览 1 评论 0原文

目前,我有一个包含以下文档的集合:

[
    {
        "_id": ObjectId("628e6bd640643f97d6517c75"),
        "company": "bau",
        "current_version": 0,
        "form_name": "don't know",
        "history": [],
        "id": "23421123-24a9-4a45-a12f-27a330152ax3",
        "is_active": True,
        "user_id": "999",
    },
    {
        "_id": ObjectId("628eaffe4b8ae2ccdeb9305c"),
        "company": "vrau",
        "current_version": 0,
        "form_name": "exemplo",
        "history": [
            {
                "content": [
                    {
                        "field_id": 0,
                        "label": "insira um texto",
                        "placeholder": "qualquer texto",
                        "type": "text",
                    }
                ],
                "layout": [
                    {"field_id": 0, "h": 10, "type": "text", "w": 100, "x": 0, "y": 0}
                ],
                "responses": [
                    {
                        "client_id": 100,
                        "response_date": "2020-01-02",
                        "values": [{"field_id": 0, "value": "um texto"}],
                    },
                    {
                        "client_id": 2,
                        "response_date": "2020-01-01",
                        "values": [{"field_id": 0, "value": "roi"}],
                    },
                ],
                "version": 0,
            }
        ],
        "id": "33b66684-24a9-4a45-a12f-27a330152ac8",
        "is_active": True,
        "user_id": "1",
    },
]

我想通过收到以下错误将响应从client_id ='2'更改:

pymongo.errors.WriteError: The field 'history.0.responses.1' must be an array but is of type object in document {_id: ObjectId('628eaffe4b8ae2ccdeb9305c')}, full error: {'index': 0, 'code': 2, 'errmsg': "The field 'history.0.responses.1' must be an array but is of type object in document {_id: ObjectId('628eaffe4b8ae2ccdeb9305c')}"}

我不知道我在做什么错,并且这个错误对我来说没有意义,因为回归是一个数组。

我当前的查询:

collection.update_many(
    {"id": "33b66684-24a9-4a45-a12f-27a330152ac8", "history.version": 0},
    {
        "$push": {
            "history.$[h].responses.$[r]": {
                "client_id": 2,
                "response_date": "2020-01-01",
                "values": [{"field_id": 0, "value": "roi"}],
            }
        }
    },
    array_filters=[{"h.version": 0}, {"r.client_id": "2"}],
)

还有其他问题吗?

Currently I have a collection with the following documents:

[
    {
        "_id": ObjectId("628e6bd640643f97d6517c75"),
        "company": "bau",
        "current_version": 0,
        "form_name": "don't know",
        "history": [],
        "id": "23421123-24a9-4a45-a12f-27a330152ax3",
        "is_active": True,
        "user_id": "999",
    },
    {
        "_id": ObjectId("628eaffe4b8ae2ccdeb9305c"),
        "company": "vrau",
        "current_version": 0,
        "form_name": "exemplo",
        "history": [
            {
                "content": [
                    {
                        "field_id": 0,
                        "label": "insira um texto",
                        "placeholder": "qualquer texto",
                        "type": "text",
                    }
                ],
                "layout": [
                    {"field_id": 0, "h": 10, "type": "text", "w": 100, "x": 0, "y": 0}
                ],
                "responses": [
                    {
                        "client_id": 100,
                        "response_date": "2020-01-02",
                        "values": [{"field_id": 0, "value": "um texto"}],
                    },
                    {
                        "client_id": 2,
                        "response_date": "2020-01-01",
                        "values": [{"field_id": 0, "value": "roi"}],
                    },
                ],
                "version": 0,
            }
        ],
        "id": "33b66684-24a9-4a45-a12f-27a330152ac8",
        "is_active": True,
        "user_id": "1",
    },
]

I want to change the response fromthe client_id = '2' by I'm receiving the following error:

pymongo.errors.WriteError: The field 'history.0.responses.1' must be an array but is of type object in document {_id: ObjectId('628eaffe4b8ae2ccdeb9305c')}, full error: {'index': 0, 'code': 2, 'errmsg': "The field 'history.0.responses.1' must be an array but is of type object in document {_id: ObjectId('628eaffe4b8ae2ccdeb9305c')}"}

I don't know what I'm doing wrong and this error doesnt make sense to me cuz reponses is an array.

my current query:

collection.update_many(
    {"id": "33b66684-24a9-4a45-a12f-27a330152ac8", "history.version": 0},
    {
        "$push": {
            "history.$[h].responses.$[r]": {
                "client_id": 2,
                "response_date": "2020-01-01",
                "values": [{"field_id": 0, "value": "roi"}],
            }
        }
    },
    array_filters=[{"h.version": 0}, {"r.client_id": "2"}],
)

Is there another to do it?

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

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

发布评论

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

评论(1

过期情话 2025-02-08 13:13:37

这是因为您还在r上执行过滤器,该过滤器已经在响应 array中解析为对象级别。

如果您只想推到响应数组,则可以简单地放弃r arrayfilter。

collection.update_many(
    {"id": "33b66684-24a9-4a45-a12f-27a330152ac8", "history.version": 0},
    {
        "$push": {
            "history.$[h].responses": {
                "client_id": 2,
                "response_date": "2020-01-01",
                "values": [{"field_id": 0, "value": "roi"}],
            }
        }
    },
    array_filters=[{"h.version": 0}],
)

这是 mongo playground 供您参考。 (在本机JS语法中)


您应该使用$ set而不是$ push如果要更新条目而不是添加条目。在您给定的示例中,client_id是int,而您的arrayfilter为字符串。如果不打算,它可能会导致问题。

collection.update_many(
    {"id": "33b66684-24a9-4a45-a12f-27a330152ac8", "history.version": 0},
    {
        "$set": {
            "history.$[h].responses.$[r]": {
                "client_id": 2,
                "response_date": "2020-01-01",
                "values": [{"field_id": 0, "value": "roi"}],
            }
        }
    },
    array_filters=[{"h.version": 0}, {"r.client_id": 2}],
)

这是 mongo playground 供您参考。 (在本地JS语法中)

It is because you are also performing filter on r, which already resolves to object level in responses array.

You can simply abandon the r arrayFilter if you simply want to push to responses array.

collection.update_many(
    {"id": "33b66684-24a9-4a45-a12f-27a330152ac8", "history.version": 0},
    {
        "$push": {
            "history.$[h].responses": {
                "client_id": 2,
                "response_date": "2020-01-01",
                "values": [{"field_id": 0, "value": "roi"}],
            }
        }
    },
    array_filters=[{"h.version": 0}],
)

Here is the Mongo playground for your reference. (in native js syntax)


You should use $set instead of $push if you want to update the entry instead of adding an entry. In your given example, the client_id is int while your arrayFilter is string. It could cause problem if it is not intended.

collection.update_many(
    {"id": "33b66684-24a9-4a45-a12f-27a330152ac8", "history.version": 0},
    {
        "$set": {
            "history.$[h].responses.$[r]": {
                "client_id": 2,
                "response_date": "2020-01-01",
                "values": [{"field_id": 0, "value": "roi"}],
            }
        }
    },
    array_filters=[{"h.version": 0}, {"r.client_id": 2}],
)

Here is the Mongo playground for your reference. (in native js syntax)

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