英文:
Index MongoDB Collection by inner keys
问题
我在一个集合中有这样的文档:
{
"_id": { "$oid": "63c6c823131d0b01d353b8d7" },
"customer_id": "5a4c8b63b7055a9109477c5b",
"couponId": "63c6c823131d0b01d353b8d6",
"prefix": 999,
"amount": 1000,
"used": 12,
"keys": {
"375354522485": {
"id": "375354522485",
"used": true
},
"375354538550": {
"id": "375354538550",
"used": false
},
"375354549291": {
"id": "375354549291",
"used": false
}
}
}
对象中的键数量可能会达到数千(20万)。
我试图像这样在mongoose中对键进行索引:
CouponSeriesSchema.index({ 'keys.*$.id': 1 });
但是索引的大小看起来不合理,它只有20.5KB,而 `_id` 索引有36.9KB。我本来希望这个索引的大小会更大。我应该如何索引这些id?
英文:
I have documents in a collection which look like this:
`{
"_id": { "$oid": "63c6c823131d0b01d353b8d7" },
"customer_id": "5a4c8b63b7055a9109477c5b",
"couponId": "63c6c823131d0b01d353b8d6",
"prefix": 999,
"amount": 1000,
"used": 12,
"keys": {
     "375354522485": {
         "id": "375354522485",
         "used": true
     },
     "375354538550": {
         "id": "375354538550",
         "used": false
     },
     "375354549291": {
         "id": "375354549291",
         "used": false
     }
   }
}`
the amount of keys in the object can be thousands (200,000)
I am trying to index the keys by id in mongoose like this:
CouponSeriesSchema.index({ 'keys.*$*.id': 1 });
but the index size does not make sense, it is 20.5KB while the _id index is 36.9KB
I would expect this index size to be much bigger
How should I index the id's?
答案1
得分: 2
{
"A smarter design would be this:": "更智能的设计如下:",
"{": "{",
""_id": { "$oid": "63c6c823131d0b01d353b8d7" },": ""_id": { "$oid": "63c6c823131d0b01d353b8d7" },",
""customer_id": "5a4c8b63b7055a9109477c5b",": ""customer_id": "5a4c8b63b7055a9109477c5b",",
""couponId": "63c6c823131d0b01d353b8d6",": ""couponId": "63c6c823131d0b01d353b8d6",",
""prefix": 999,": ""prefix": 999,",
""amount": 1000,": ""amount": 1000,",
""used": 12,": ""used": 12,",
""keys": [": ""keys": [",
"    {": "    {",
"        "id": "375354522485",": "        "id": "375354522485",",
"        "used": true": "        "used": true",
"    },": "    },",
"    {": "    {",
"        "id": "375354538550",": "        "id": "375354538550",",
"        "used": false": "        "used": false",
"    },": "    },",
"    {": "    {",
"        "id": "375354549291",": "        "id": "375354549291",",
"        "used": false": "        "used": false",
"    }": "    }",
"  ]": "  ]",
"}": "}",
"Then an index { 'keys.id': 1 } would work.": "然后一个索引 { 'keys.id': 1 } 将起作用。"
}
英文:
A smarter design would be this:
{
"_id": { "$oid": "63c6c823131d0b01d353b8d7" },
"customer_id": "5a4c8b63b7055a9109477c5b",
"couponId": "63c6c823131d0b01d353b8d6",
"prefix": 999,
"amount": 1000,
"used": 12,
"keys": [
     {
         "id": "375354522485",
         "used": true
     },
     {
         "id": "375354538550",
         "used": false
     },
     {
         "id": "375354549291",
         "used": false
     }
   ]
}
Then an index { 'keys.id': 1 } would work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论