在MongoDB数组中有效搜索
在我正在从事的电子商务项目中,我们正在为给定的平台存储产品价格映射。我们正在遵循存储模式来存储价格。 JSON结构看起来像这样。
{
"_id": ObjectId("6242b344ba42fdc0fab27b05"),
"prices": [
{
"pincode": ObjectId("6241999ad7367f6e0eeb7abc"),
"price": 12625
},
{
"pincode": ObjectId("6241999ad7367f6e0eeb7abd"),
"price": 13625
}
... // 998 more items
],
"product": ObjectId("6242b344ba42fdc0fab27b05"),
"count": 1000
}
详细信息如下。
计数:表示数组中的元素数量(我们要修复每个水桶1000个元素)
产品:代表产品ID 每个平台产品的相应价格。
由于我们遵循存储桶模式,因此对于任何给定的产品,都有最大的1000个数组元素,并且状形胶模式超过1000,则将为该产品创建新文档。
如果我使用以下类似的聚合查询,
[
{
'$match': {
'product': new ObjectId('6242b344ba42fdc0fab27b05'),
'prices.pincode':ObjectId('6241999ad7367f6e0eeb7ad7')
}
}, {
'$project': {
'price': {
'$filter': {
'input': '$prices',
'as': 'prices',
'cond': {
'$eq': [
'$prices.pincode', new ObjectId('6241999ad7367f6e0eeb7ad7')
]
}
}
},
'_id': 0
}
}
]
我将获得匹配我的Pincode ID和产品ID的数组元素。我们在查询中使用$ filter,它将仅循环数组并过滤匹配pincode ID的数组元素,并返回相应的价格。
如果对于单个产品,那么上述查询就可以了,但是如果产品列表很大,则说我们是通过类别搜索(给定的类别可以具有大量产品列表),以获取每种产品的价格,并通过循环循环搜索pincode通过数组,一旦我们获得文档是非常昂贵的
,我的问题是,我想为给定的平台上获取价格,并且产品是否有更好的方法可以有效地从数组中返回匹配元素,而不仅仅是通过数组循环循环($ filter)。
注意:化合物索引是在(产品,Price.Pincode)上创建的
In the e-commerce project I am working on, we are storing mapping of price of product for a given pincode. We are following bucket pattern to store the prices. The json structure looks like this.
{
"_id": ObjectId("6242b344ba42fdc0fab27b05"),
"prices": [
{
"pincode": ObjectId("6241999ad7367f6e0eeb7abc"),
"price": 12625
},
{
"pincode": ObjectId("6241999ad7367f6e0eeb7abd"),
"price": 13625
}
... // 998 more items
],
"product": ObjectId("6242b344ba42fdc0fab27b05"),
"count": 1000
}
Details is as follows.
count: represents number of elements in the array(we are fixing 1000 elements per bucket)
product: represents product id
and corresponding price of product per each pincode.
Since we are following bucket pattern, for any given product there are utmost 1000 array elements and if the pincodes are exceeding 1000, then new document will be created for that given product.
If I query using aggregations like below
[
{
'$match': {
'product': new ObjectId('6242b344ba42fdc0fab27b05'),
'prices.pincode':ObjectId('6241999ad7367f6e0eeb7ad7')
}
}, {
'$project': {
'price': {
'$filter': {
'input': '$prices',
'as': 'prices',
'cond': {
'$eq': [
'$prices.pincode', new ObjectId('6241999ad7367f6e0eeb7ad7')
]
}
}
},
'_id': 0
}
}
]
I will get the array element matching my pincode id and product id. We are using $filter in the query which will just loop the array and filter the array element matching the pincode id and corresponding price is returned.
If for a single product, then the above query is just fine, but if the product list is just huge, say we are searching via category(a given category can have big list of products) fetching price for each product and given pincode by looping through array once we get the document is very costly
My questions is, I want to fetch price for a given pincode and product is there any better approach to return the matching element from array efficiently instead of just looping($filter) through the array .
Note: compound index is created on ( product , prices.pincode )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论