Mongodb 多个子文档
我需要一个具有如下结构的集合:
{
"_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数组类型会更好,因为有一些优点。
(1) 您可以添加一个新字段,其值例如
pt
、fr
、xy< /code>、
ab
等。例如:注意新字段
type
的引入(这可以是代表字段数据的任何名称)。(2) 您还可以在数组上建立索引子文档字段,这可以提高查询性能。数组字段索引称为多键索引。
索引可以位于查询过滤器中使用的字段上。例如,
“details.missing”
。该键也可以是复合索引的一部分。这可以帮助查询过滤器,如下所示:注意:您可以通过生成查询计划、应用
解释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: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:NOTE: You can verify the usage of an index in a query by generating a Query Plan, applying the
explain
method on thefind
.(3) Also, see Embedded Document Pattern as explained in the Model One-to-Many Relationships with Embedded Documents.