索引 MongoDB 集合的内部键

huangapple go评论46阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 18:20:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定