Mongodb 多个子文档

发布于 2025-01-11 05:27:07 字数 1187 浏览 0 评论 0原文

我需要一个具有如下结构的集合:

{
    "_id" : ObjectId("5ffc3e2df14de59d7347564d"),
    "name" : "MyName",
    "pays" : "de",
    "actif" : 1,
    "details" : {
        "pt" : {
            "title" : "MongoTime PT",
            "availability_message" : "In stock",
            "price" : 23,
            "stock" : 1,
            "delivery_location" : "Portugal",
            "price_shipping" : 0,
            "updated_date" : ISODate("2022-03-01T20:07:20.119Z"),
            "priority" : false,
            "missing" : 1,
        }, 
        "fr" : {
            "title" : "MongoTime FR",
            "availability_message" : "En stock",
            "price" : 33,
            "stock" : 1,
            "delivery_location" : "France",
            "price_shipping" : 0,
            "updated_date" : ISODate("2022-03-01T20:07:20.119Z"),
            "priority" : false,
            "missing" : 1,
        }
    }
}

如何为“详细信息”中的每个子文档创建索引? 或者也许做一个数组更好?

目前执行这样的查询非常长(1 小时)。我该怎么办?

query = {"details.pt.missing": {"$in": [0, 1, 2, 3]}, "pays": 'de'}
db.find(query, {"_id": false, "name": true}, sort=[("details.pt.updated_date", 1)], limit=300)

I need a collection with structure like this:

{
    "_id" : ObjectId("5ffc3e2df14de59d7347564d"),
    "name" : "MyName",
    "pays" : "de",
    "actif" : 1,
    "details" : {
        "pt" : {
            "title" : "MongoTime PT",
            "availability_message" : "In stock",
            "price" : 23,
            "stock" : 1,
            "delivery_location" : "Portugal",
            "price_shipping" : 0,
            "updated_date" : ISODate("2022-03-01T20:07:20.119Z"),
            "priority" : false,
            "missing" : 1,
        }, 
        "fr" : {
            "title" : "MongoTime FR",
            "availability_message" : "En stock",
            "price" : 33,
            "stock" : 1,
            "delivery_location" : "France",
            "price_shipping" : 0,
            "updated_date" : ISODate("2022-03-01T20:07:20.119Z"),
            "priority" : false,
            "missing" : 1,
        }
    }
}

How can i create an index for each subdocument in 'details' ?
Or maybe it's better to do an array ?

Doing a query like this is currently very long (1 hour). How can I do ?

query = {"details.pt.missing": {"$in": [0, 1, 2, 3]}, "pays": 'de'}
db.find(query, {"_id": false, "name": true}, sort=[("details.pt.updated_date", 1)], limit=300)

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

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

发布评论

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

评论(1

咽泪装欢 2025-01-18 05:27:07

数组类型会更好,因为有一些优点。

(1) 您可以添加一个新字段,其值例如 ptfrxy< /code>、ab等。例如:

details: [
    { type: "pt", title : "MongoTime PT", missing: 1, other_fields: ... },
    { type: "fr", title : "MongoTime FR", missing: 1, other_fields: ... },
    { type: "xy", title : "MongoTime XY", missing: 2, other_fields: ... },
    // ...
]

注意新字段type的引入(这可以是代表字段数据的任何名称)。

(2) 您还可以在数组上建立索引子文档字段,这可以提高查询性能。数组字段索引称为多键索引

索引可以位于查询过滤器中使用的字段上。例如,“details.missing”。该键也可以是复合索引的一部分。这可以帮助查询过滤器,如下所示:

{ pays: "de", "details.type": "pt", "details.missing": { $in: [ 0, 1, 2, 3 ] } }

注意:您可以通过生成查询计划、应用解释find 上的 code> 方法。

(3) 另请参阅嵌入式文档模式,如与嵌入式文档建立一对多关系模型中所述。

An array type would be better, as there are advantages.

(1) You can include a new field which has values like pt, fr, xy, ab, etc. For example:

details: [
    { type: "pt", title : "MongoTime PT", missing: 1, other_fields: ... },
    { type: "fr", title : "MongoTime FR", missing: 1, other_fields: ... },
    { type: "xy", title : "MongoTime XY", missing: 2, other_fields: ... },
    // ...
]

Note the introduction of the new field type (this can be any name representing the field data).

(2) You can also index on the array sub-document fields, which can improve query performance. Array field indexes are referred as Multikey Indexes.

The index can be on a field used in a query filter. For example, "details.missing". This key can also be part of a Compound Index. This can help a query filter like below:

{ pays: "de", "details.type": "pt", "details.missing": { $in: [ 0, 1, 2, 3 ] } }

NOTE: You can verify the usage of an index in a query by generating a Query Plan, applying the explain method on the find.

(3) Also, see Embedded Document Pattern as explained in the Model One-to-Many Relationships with Embedded Documents.

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