如何使用 MongoDB & 插入多个对象节点.js? [英] How can I upsert multiple objects with MongoDB & Node.js?
问题描述
假设我有一系列类似这样的电影类型:
<预><代码>[{ id: 28, name: 'Action' },{ id: 12, name: '冒险' },{ id: 16, name: '动画' },{ id: 35, name: '喜剧' },{ id: 80, name: '犯罪' },{ id: 99, name: '纪录片' },{ id: 18, name: '戏剧' },{ id: 10751, name: 'Family' },{ id: 14, name: 'Fantasy' },{ id: 10769, name: 'Foreign' },{ id: 36, name: '历史' },{ id: 27, name: '恐怖' },{ ID:10402,名称:'音乐'},{ id: 9648, name: 'Mystery' },{ id: 10749, name: '浪漫' },{ id: 878, name: '科幻' },{ id: 10770, name: '电视电影' },{ id: 53, name: 'Thriller' },{ ID:10752,名称:'战争'},{ id: 37, name: '西方' }]并且我有一个到 MongoDB (v3.2) 实例的连接:db
,我使用的是标准的 mongodb Node.js 驱动程序 (const mongodb = require('mongodb').MongoClient
).
我想要做的是对集合进行批量更新插入操作,比如 genres
,其中 _id
字段映射到 id
类型对象的字段.
现在,我知道我可以遍历数组中的每个项目,并执行一个简单的 upsert:
for (let i = 0; i
但这感觉浪费和错误.
有没有更简单的方法来完成应该相对简单的任务?
谢谢
使用 bulkWrite
API 来执行更新:
var bulkUpdateOps = Genes.map(function(doc) {返回 {更新一":{过滤器":{_id":doc.id},"update": { "$set": { "name": doc.name } },更新":真的}};});db.collection('genres').bulkWrite(bulkUpdateOps, function(err, r) {//对结果做一些事情}
<小时>
如果您正在处理更大的数组,即 > 1000,那么请考虑将写入以 500 为一组发送到服务器,这会给您带来更好的性能,因为您不会将每个请求都发送到服务器,而是每 500 个请求发送一次:
var bulkUpdateOps = [],计数器 = 0;流派.forEach(功能(文档){批量更新操作.push({更新一":{过滤器":{_id":doc.id},"update": { "$set": { "name": doc.name } },更新":真的}});计数器++;如果(计数器 % 500 == 0){db.collection('genres').bulkWrite(bulkUpdateOps, function(err, r) {//对结果做一些事情});批量更新操作 = [];}})如果(计数器 % 500 != 0){db.collection('genres').bulkWrite(bulkUpdateOps, function(err, r) {//对结果做一些事情}}
Let's say I have an array of Movie genres like so:
[
{ id: 28, name: 'Action' },
{ id: 12, name: 'Adventure' },
{ id: 16, name: 'Animation' },
{ id: 35, name: 'Comedy' },
{ id: 80, name: 'Crime' },
{ id: 99, name: 'Documentary' },
{ id: 18, name: 'Drama' },
{ id: 10751, name: 'Family' },
{ id: 14, name: 'Fantasy' },
{ id: 10769, name: 'Foreign' },
{ id: 36, name: 'History' },
{ id: 27, name: 'Horror' },
{ id: 10402, name: 'Music' },
{ id: 9648, name: 'Mystery' },
{ id: 10749, name: 'Romance' },
{ id: 878, name: 'Science Fiction' },
{ id: 10770, name: 'TV Movie' },
{ id: 53, name: 'Thriller' },
{ id: 10752, name: 'War' },
{ id: 37, name: 'Western' }
]
and I have a connection to a MongoDB (v3.2) instance: db
, and I'm using the standard mongodb Node.js driver (const mongodb = require('mongodb').MongoClient
).
What I want to be able to do is one bulk upsert operation onto a collection, say genres
, where the _id
field maps to the id
field of our genre objects.
Now, I know I could loop through each item in the array, and do a simple upsert:
for (let i = 0; i < genres.length; i++) {
await db.collection('genres').update(
{ _id: genres[i].id },
genres[i],
{ upsert: true }
);
}
But this feels wasteful and wrong.
Is there an easier way to do what should be a relatively simple task?
Thanks
Use the bulkWrite
API to carry out the updates:
var bulkUpdateOps = genres.map(function(doc) {
return {
"updateOne": {
"filter": { "_id": doc.id },
"update": { "$set": { "name": doc.name } },
"upsert": true
}
};
});
db.collection('genres').bulkWrite(bulkUpdateOps, function(err, r) {
// do something with result
}
If you're dealing with larger arrays i.e. > 1000 then consider sending the writes to the server in batches of 500 which gives you a better performance as you are not sending every request to the server, just once in every 500 requests:
var bulkUpdateOps = [],
counter = 0;
genres.forEach(function(doc) {
bulkUpdateOps.push({
"updateOne": {
"filter": { "_id": doc.id },
"update": { "$set": { "name": doc.name } },
"upsert": true
}
});
counter++;
if (counter % 500 == 0) {
db.collection('genres').bulkWrite(bulkUpdateOps, function(err, r) {
// do something with result
});
bulkUpdateOps = [];
}
})
if (counter % 500 != 0) {
db.collection('genres').bulkWrite(bulkUpdateOps, function(err, r) {
// do something with result
}
}
这篇关于如何使用 MongoDB & 插入多个对象节点.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!