【问题标题】:MongoDB: Push related data from another collectionMongoDB:从另一个集合推送相关数据
【发布时间】:2015-08-11 02:35:24
【问题描述】:

我有一个包含生成的报告数据的集合。

产品:

{
  location: 'Spain',
  month: 5,
  year: 2015,
  name: 'Cup',
  price: 100.32,
  type: 1
},
  ...

然后我有重要的数据:

报告:

{
  location: 'Spain',
  month: 5,
  year: 2015,
  stdPrice: 110.22,
  products : [] // Here is where I'd like to insert related data (location, month, year) 
//from Products
}

数据应该被存储回数据库中。

类似:

 products.forEach(function(product){
   report.forEach(function(data){
    if(product['location'] === data['location'] && product['month'] === data['month'] && product['year'] === data['year']){
       data['products'].push(product);
     }
   });
 });

有人知道如何实现吗?我在想 map-reduce 将是一个很好的方法。我也希望能够使用 mongoose 以编程方式执行此操作。

谢谢!

【问题讨论】:

标签: javascript node.js mongodb aggregation-framework


【解决方案1】:

我能够自己完成这项工作。我没有在 x 操作后手动执行,因为我自己没有让它工作。

每组操作最多可以有 1000 个操作。如果一个组超过了这个限制,MongoDB 会将该组分成 1000 人或更少的更小的组。例如,如果批量操作列表包含 2000 个插入操作,则 MongoDB 会创建 2 个组,每个组有 1000 个操作。

Source

    var bulk = mongoose.model('Report').collection.initializeOrderedBulkOp();

    mongoose.model('Product').find({}).exec(function (error, doc) {
        doc.forEach(function (value, index) {
            if (index % 500 === 0) {
                console.log('Current index: ', index);
            }
            bulk.find({
                location: value._doc.location,
                month: {$lte: value._doc.month}
            }).updateOne({
                "$push": {
                    "products": {
                        "location": doc.location,
                        "month": doc.month,
                        "year": doc.year,
                        "name": doc.name,
                        "price": doc.price,
                        "type": doc.type
                    }
                }
            });
        });
        console.log('Bulk Executing');
        bulk.execute(function (err, results) {
            if (err)
                console.error(err);
            else
                console.log(results.toJSON());
        });
    });

感谢 @yogesh 的 MongoDB 代码。

【讨论】:

    【解决方案2】:

    使用mongo bulk操作如下:

     var bulk = db.report.initializeOrderedBulkOp(),
        count = 0;
     db.products.find().forEach(function(doc) {
        bulk.find({
            "location": doc.location,
            "month": doc.month
        }).updateOne({
            "$push": {
                "products": {
                    "location": doc.location,
                    "month": doc.month,
                    "year": doc.year,
                    "name": doc.name,
                    "price": doc.price,
                    "type": doc.type
                }
            }
        });
        count += 2;
        if(count % 500 == 0) {
            bulk.execute();
            bulk = db.report.initializeOrderedBulkOp();
        }
     });
     if(count % 500 !== 0) bulk.execute();
    

    参考check this

    【讨论】:

    • 你是如何使用 forEach 的? .find() 不返回承诺吗?顺便说一句,我正在使用猫鼬。
    猜你喜欢
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2013-09-23
    • 2022-01-19
    • 1970-01-01
    • 2015-01-18
    相关资源
    最近更新 更多