首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb查询花费的时间太长

mongodb查询花费的时间太长
EN

Stack Overflow用户
提问于 2018-01-05 05:27:06
回答 1查看 1K关注 0票数 2

我的mongodb集合中有以下文档:

代码语言:javascript
复制
{'name' : 'abc-1','parent':'abc', 'price': 10}
{'name' : 'abc-2','parent':'abc', 'price': 5}
{'name' : 'abc-3','parent':'abc', 'price': 9}
{'name' : 'abc-4','parent':'abc', 'price': 11}

{'name' : 'efg', 'parent':'', 'price': 10}
{'name' : 'efg-1','parent':'efg', 'price': 5}
{'name' : 'abc-2','parent':'efg','price': 9}
{'name' : 'abc-3','parent':'efg','price': 11}

我想采取以下行动:

代码语言:javascript
复制
a. Group By distinct parent
b. Sort all the groups based on price
c. For each group select a document with minimum price
  i. check each record's parent sku exists as a record in name field
  ii. If the name exists, do nothing
  iii. If the record does not exists, insert a document with parent as empty and other values as the  value of the record selected previously (minimum value).

我很想用以下每一种方法:

代码语言:javascript
复制
db.file.find().sort([("price", 1)]).forEach(function(doc){
          cnt = db.file.count({"sku": {"$eq": doc.parent}});
          if (cnt < 1){
               newdoc = doc;
               newdoc.name = doc.parent;
               newdoc.parent = "";
              delete newdoc["_id"];
              db.file.insertOne(newdoc);
          }
});

问题是它需要太多的时间。这里怎么了?如何对其进行优化?如果是的话,聚合管道是否是一个好的解决方案?

EN

回答 1

Stack Overflow用户

发布于 2018-01-06 09:12:46

  1. 检索一组产品名称✔

def product_names():db.file.aggregate中的乘积({$group:{_id:“$name”}):乘积‘_id’product_names = set(product_names())

  1. 从✔组中以最低价格检索产品

result_set = db.file.aggregate( {‘$排序’:{‘价格’:1,},},{ '$group':{ '_id':'$parent','name':{ '$first':'$name',}“价格”:{ '$min':'$price',} },{‘$排序’:{‘价格’:1,})

  1. 如果名称不在1.✔检索的一组产品名称中,则插入2中检索的产品 从pymongo.operations import InsertOne def insert_request(产品):返回InsertOne({名称:产品名称,价格:产品‘价格’,父级:‘}’)请求=(insert_request(产品),如果产品名称不在product_names中)db.file.bulk_write(请求)

可以在aggregation 管道中实现步骤2和3。

代码语言:javascript
复制
db.file.aggregate([
    {
        '$sort': {'price': 1}
    }, 
    {
        '$group': {
            '_id': '$parent',
            'name': {
                '$first': '$name'
            }, 
            'price': {
                '$min': '$price'
            },
        }
    }, 
    {
        '$sort': {
            'price': 1
        }
    }, 
    {
        '$project': {
            'name': 1, 
            'price': 1,
            '_id': 0, 
            'parent':''
        }
    }, 
    {
        '$match': {
            'name': {
                '$nin': list(product_names())
            }
        }
    }, 
    {
        '$out': 'file'
    }
])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48107682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档