MongoDB:从另一个集合推送相关数据
MongoDB: Push related data from another collection
我有一个由生成的报表数据组成的集合。
产品:
{
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
}
数据应该被存储回DB。
类似:
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是一个很好的方法。我也希望能够对猫鼬进行编程。
谢谢!
我可以自己完成这项工作。我没有在x操作后手动执行,因为我自己没有使它工作。
来源每组操作最多有1000个操作。如果一个组超过了这个限制,MongoDB会将该组划分为1000个或更小的组。例如,如果大容量操作列表包含2000个插入操作,MongoDB创建2个组,每个组有1000个操作。
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代码
使用mongo批量操作如下:
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();
for ref check this
相关文章:
- 使用数据上的角度更改设置集合的第一个元素的动画
- 我们可以用参数对象集合而不是原始数据来调用JavaScript collection.reduce()方法吗
- 如何将会话数据从集合传递到视图?(Backbone JS/Coffeescapept)
- 用于筛选无模式集合的最快数据结构
- 如何更新集合数据并使用骨干木偶重新渲染
- Meteor:如何使用空格键创建包含两个集合数据的树列表
- 使用Meteor JS的反应联接在模板中呈现集合数据
- 集合数据和jQuery
- 显示行动通过铁:路由器,模板是't从集合传递数据.findOne方法
- Backbonej集合返回错误的数据
- 将数据存储在模型&集合呼叫&取来
- 如何将带有普通对象集合的 Js 数组对象传递给 ajax post'html' 数据类型中的控制器
- 基于 MongoDB 中另一个集合中的数据查询一个集合中的数据
- Metero 通过 id 从两个集合映射中获取数据
- 主干仅保存具有已更改数据的集合中已更改的模型
- 流星 - 将数据从一个集合移动到另一个集合
- 使用Backbone.js在待办事项中对数据集合进行分页.js Web应用程序
- 使用JavaScript从SharePoint组合和排序两个数据集合
- 如何从Objective C代码中使用nsarray传递数据集合到javascript函数
- 在哪里/如何存储数据集合