Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何使用quick way过滤旧的\u集合并将过滤后的数据插入新的\u集合_Mongodb_Mongoid_Pymongo - Fatal编程技术网

Mongodb 如何使用quick way过滤旧的\u集合并将过滤后的数据插入新的\u集合

Mongodb 如何使用quick way过滤旧的\u集合并将过滤后的数据插入新的\u集合,mongodb,mongoid,pymongo,Mongodb,Mongoid,Pymongo,我需要将旧集合过滤为新集合 现在,我可以用Ruby来做,但是在应用程序级别做这件事太慢了,但是我不知道如何在原生mongoDB查询中做这种操作 而且我的收藏规模超过了数十亿,所以很容易就会出现超大问题。(文档大小限制为16MB) 旧_集合用于记录客户的订单 新的_集合用于汇总客户订单 命令 示例 数据来源:订单 预期结果 更新 这是我当前的代码,但运行速度很慢,我想知道如何快速完成 我需要一个如何在集合中操作数据的想法find\u或\u create\u by 有没有更好的搜索问题的关键词 mo

我需要将旧集合过滤为新集合

现在,我可以用Ruby来做,但是在应用程序级别做这件事太慢了,但是我不知道如何在原生mongoDB查询中做这种操作

而且我的收藏规模超过了数十亿,所以很容易就会出现超大问题。(文档大小限制为16MB)

旧_集合用于记录客户的订单

新的_集合用于汇总客户订单

命令 示例 数据来源:订单 预期结果 更新 这是我当前的代码,但运行速度很慢,我想知道如何快速完成

我需要一个如何在集合中操作数据的想法
find\u或\u create\u by

有没有更好的搜索问题的关键词

module  CreateCustomersHelper
  def add_record_to_Customer_history(record)
      Customer = get_Customer(record)
      items.each do |item|
        Customer.add_to_set(uniq_buy_items: item)
      end
      Customer.add_to_set(buy_items_history: new_history_item(record))
      Customer.save
  end

  private
    def get_Customer(r)
      Customer.find_or_create_by(id: r["ID"])
    end

    def new_history_item(r)
      { 
        ...
      }      
    end

end

以下mongo聚合将获得预期结果

db.collectionName.aggregate({
    "$group": {
    "_id": "$customer_name",
    "birthDay": {
        "$first": "$customer_birthday"
    },
    "buy_items_history": {
        "$push": {
            "items": "$buy_items",
            "date": "$date",
            "id": "$_id"
        }
    },
    "unique": {
        "$push": "$buy_items"
    }
    }
}, {
    "$unwind": "$unique"  // Unwind twice because "unique" is now an array of arrays
}, {
    "$unwind": "$unique"
}, {  // Now use $addToSet to get the distinct values     
    "$group": {
    "_id": "$_id",
    "birthDay": {
        "$first": "$birthDay"
    },
    "buy_items_history": {
        "$first": "$buy_items_history"
    },
    "uniq_buy_items": {
        "$addToSet": "$unique"
    }
    }
}).pretty()

基于您之前提出的一系列Mongodb问题(,),它看起来像是您将SO用作一种免费的编码服务:发布需求并期望有人为您编写代码。所以还有另一个目的(帮助解决问题)。嗨@Dali,我认为大多数用户都在问他们的编码问题,我不需要有人为我写代码,相反,我需要一个想法或类似的代码样本来解决我的问题。是的,人们问他们真正的问题。但问题的提出方式有很大不同。您前面的问题是:
我有要求,请编写我可以粘贴的代码
。如果你看一下你的第一个版本,你会发现这是一个
为我工作
类的问题。另一方面,你当前的问题清楚地表明了你做了哪些尝试,以及为什么你对自己的成功不满意(这使你的成功变得更好)。对于你之前的问题,有人会花时间,可能会得到你已经拥有的东西,只是为了听到你的声音
我试过了,但不喜欢它
对不起,我的英语很差,我不是故意的,我下次会注意它。对于这个问题,我知道如何用ruby来做,但在这个应用程序级别上做起来太慢了,但我不知道如何在原生mongoDB查询中做这种操作。从你的评论中,我看到你不是有意的,我相信你下次会把你的问题做得更好(我已经看到了一个进展)。英语不是一个问题(在第二次迭代之后,我理解了你的问题)。祝你的问题好运。谢谢你的分享,我会尝试一下,希望它能比Ruby版本运行得更快
{
    order_id : 1
    customer_name : Jack
    customer_birthday : 1987-06-12
    buy_items : ["ruby cookbook", "python cookbook", "mongodb cookbook"]
    date : 2011-01-15
},
...
{
    order_id : 13
    customer_name : Jack
    customer_birthday : 1987-06-12
    buy_items : ["Java cookbook", "mongodb cookbook"]
    date : 2015-04-15
}
{
    customer_name : Jack
    customer_birthday : 1987-06-12
    buy_items_history: [{items: ["ruby cookbook", "python cookbook", "mongodb cookbook"],date: 2011-01-15, id:1]},
                        {items: ["Java cookbook", "mongodb cookbook"],date: 2015-04-15, id:13]},
                       ]
    uniq_buy_items : ["Java cookbook", "ruby cookbook", "python cookbook", "mongodb cookbook"]
}
module  CreateCustomersHelper
  def add_record_to_Customer_history(record)
      Customer = get_Customer(record)
      items.each do |item|
        Customer.add_to_set(uniq_buy_items: item)
      end
      Customer.add_to_set(buy_items_history: new_history_item(record))
      Customer.save
  end

  private
    def get_Customer(r)
      Customer.find_or_create_by(id: r["ID"])
    end

    def new_history_item(r)
      { 
        ...
      }      
    end

end
db.collectionName.aggregate({
    "$group": {
    "_id": "$customer_name",
    "birthDay": {
        "$first": "$customer_birthday"
    },
    "buy_items_history": {
        "$push": {
            "items": "$buy_items",
            "date": "$date",
            "id": "$_id"
        }
    },
    "unique": {
        "$push": "$buy_items"
    }
    }
}, {
    "$unwind": "$unique"  // Unwind twice because "unique" is now an array of arrays
}, {
    "$unwind": "$unique"
}, {  // Now use $addToSet to get the distinct values     
    "$group": {
    "_id": "$_id",
    "birthDay": {
        "$first": "$birthDay"
    },
    "buy_items_history": {
        "$first": "$buy_items_history"
    },
    "uniq_buy_items": {
        "$addToSet": "$unique"
    }
    }
}).pretty()