[英]Bulk upsert in MongoDB using mongoose
是否有任何选项可以使用猫鼬执行批量更新插入? 所以基本上有一个数组并在每个元素不存在时插入它,如果存在则更新它? (我正在使用海关 _ids)
当我确实使用.insert MongoDB 时,会为重复键(应该更新)返回错误 E11000。 插入多个新文档虽然工作正常:
var Users = self.db.collection('Users');
Users.insert(data, function(err){
if (err) {
callback(err);
}
else {
callback(null);
}
});
使用.save 会返回参数必须是单个文档的错误:
Users.save(data, function(err){
...
}
这个答案表明没有这样的选项,但是它是特定于 C# 的,并且已经有 3 年历史了。 所以我想知道是否有任何选项可以使用猫鼬来做到这一点?
谢谢!
具体不是“猫鼬”,或者至少在写作时还没有。 从 2.6 版开始,MongoDB shell 实际上在“幕后”中使用了“批量操作 API ”,因为它用于所有通用辅助方法。 在它的实现中,它首先尝试执行此操作,如果检测到旧版本服务器,则会“回退”到旧版实现。
所有 mongoose 方法“当前”都使用“遗留”实现或写关注响应和基本遗留方法。 但是任何给定的.collection
模型都有一个.collection
访问器,它本质上是从底层的“节点本机驱动程序”访问“集合对象”,在该驱动程序上实现了.collection
本身:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
var sampleSchema = new Schema({},{ "strict": false });
var Sample = mongoose.model( "Sample", sampleSchema, "sample" );
mongoose.connection.on("open", function(err,conn) {
var bulk = Sample.collection.initializeOrderedBulkOp();
var counter = 0;
// representing a long loop
for ( var x = 0; x < 100000; x++ ) {
bulk.find(/* some search */).upsert().updateOne(
/* update conditions */
});
counter++;
if ( counter % 1000 == 0 )
bulk.execute(function(err,result) {
bulk = Sample.collection.initializeOrderedBulkOp();
});
}
if ( counter % 1000 != 0 )
bulk.execute(function(err,result) {
// maybe do something with result
});
});
主要的问题是“猫鼬方法”实际上意识到可能实际上还没有建立连接并在完成之前“排队”。 您正在“深入研究”的本机驱动程序并没有做出这种区分。
所以你真的必须意识到连接是以某种方式或形式建立的。 但是您可以使用本机驱动程序方法,只要您小心自己在做什么。
您不需要像@neil-lunn 建议的那样管理限制(1000)。 猫鼬已经这样做了。 我使用他的出色回答作为这个完整的基于 Promise 的实现和示例的基础:
var Promise = require('bluebird');
var mongoose = require('mongoose');
var Show = mongoose.model('Show', {
"id": Number,
"title": String,
"provider": {'type':String, 'default':'eztv'}
});
/**
* Atomic connect Promise - not sure if I need this, might be in mongoose already..
* @return {Priomise}
*/
function connect(uri, options){
return new Promise(function(resolve, reject){
mongoose.connect(uri, options, function(err){
if (err) return reject(err);
resolve(mongoose.connection);
});
});
}
/**
* Bulk-upsert an array of records
* @param {Array} records List of records to update
* @param {Model} Model Mongoose model to update
* @param {Object} match Database field to match
* @return {Promise} always resolves a BulkWriteResult
*/
function save(records, Model, match){
match = match || 'id';
return new Promise(function(resolve, reject){
var bulk = Model.collection.initializeUnorderedBulkOp();
records.forEach(function(record){
var query = {};
query[match] = record[match];
bulk.find(query).upsert().updateOne( record );
});
bulk.execute(function(err, bulkres){
if (err) return reject(err);
resolve(bulkres);
});
});
}
/**
* Map function for EZTV-to-Show
* @param {Object} show EZTV show
* @return {Object} Mongoose Show object
*/
function mapEZ(show){
return {
title: show.title,
id: Number(show.id),
provider: 'eztv'
};
}
// if you are not using EZTV, put shows in here
var shows = []; // giant array of {id: X, title: "X"}
// var eztv = require('eztv');
// eztv.getShows({}, function(err, shows){
// if(err) return console.log('EZ Error:', err);
// var shows = shows.map(mapEZ);
console.log('found', shows.length, 'shows.');
connect('mongodb://localhost/tv', {}).then(function(db){
save(shows, Show).then(function(bulkRes){
console.log('Bulk complete.', bulkRes);
db.close();
}, function(err){
console.log('Bulk Error:', err);
db.close();
});
}, function(err){
console.log('DB Error:', err);
});
// });
这样做的好处是在连接完成后关闭连接,如果您关心,则显示任何错误,如果不关心,则忽略它们(Promise 中的错误回调是可选的。)它也非常快。 只是把这个留在这里分享我的发现。 例如,如果要将所有 eztv 节目保存到数据库中,您可以取消注释 eztv 内容。
await Model.bulkWrite(docs.map(doc => ({
updateOne: {
filter: {id: doc.id},
update: doc,
upsert: true
}
})))
或者更详细:
const bulkOps = docs.map(doc => ({
updateOne: {
filter: {id: doc.id},
update: doc,
upsert: true
}
}))
Model.bulkWrite(bulkOps)
.then(bulkWriteOpResult => console.log('BULK update OK:', bulkWriteOpResult))
.catch(err => console.error('BULK update error:', err))
我已经为 Mongoose 发布了一个插件,它公开了一个静态upsertMany
方法来执行带有承诺接口的批量 upsert 操作。
使用这个插件而不是在底层集合上初始化你自己的批量操作的另一个好处是,这个插件首先将你的数据转换为 Mongoose 模型的数据,然后在 upsert 之前转换回普通对象。 这可确保应用 Mongoose 模式验证,并减少数据填充并适合原始插入。
https://github.com/meanie/mongoose-upsert-many https://www.npmjs.com/package/@meanie/mongoose-upsert-many
希望能帮助到你!
如果您没有在 db.collection 中看到批量方法,即您收到 xxx 变量没有方法的影响的错误: initializeOrderedBulkOp()
尝试更新您的猫鼬版本。 显然,较旧的 mongoose 版本不会通过所有底层 mongo db.collection 方法。
npm 安装猫鼬
为我照顾它。
我最近必须在我的电子商务应用程序中存储产品时实现这一目标。 我的数据库曾经超时,因为我必须每 4 小时更新 10000 个项目。 对我来说,一种选择是在连接到数据库时在 mongoose 中设置 socketTimeoutMS 和 connectTimeoutMS,但它有点感觉很hacky,我不想操纵数据库的连接超时默认值。 我还看到@neil lunn 的解决方案采用了一种简单的同步方法,即在 for 循环内取模数。 这是我的一个异步版本,我相信它做得更好
let BATCH_SIZE = 500
Array.prototype.chunk = function (groupsize) {
var sets = [];
var chunks = this.length / groupsize;
for (var i = 0, j = 0; i < chunks; i++ , j += groupsize) {
sets[i] = this.slice(j, j + groupsize);
}
return sets;
}
function upsertDiscountedProducts(products) {
//Take the input array of products and divide it into chunks of BATCH_SIZE
let chunks = products.chunk(BATCH_SIZE), current = 0
console.log('Number of chunks ', chunks.length)
let bulk = models.Product.collection.initializeUnorderedBulkOp();
//Get the current time as timestamp
let timestamp = new Date(),
//Keep track of the number of items being looped
pendingCount = 0,
inserted = 0,
upserted = 0,
matched = 0,
modified = 0,
removed = 0,
//If atleast one upsert was performed
upsertHappened = false;
//Call the load function to get started
load()
function load() {
//If we have a chunk to process
if (current < chunks.length) {
console.log('Current value ', current)
for (let i = 0; i < chunks[current].length; i++) {
//For each item set the updated timestamp to the current time
let item = chunks[current][i]
//Set the updated timestamp on each item
item.updatedAt = timestamp;
bulk.find({ _id: item._id })
.upsert()
.updateOne({
"$set": item,
//If the item is being newly inserted, set a created timestamp on it
"$setOnInsert": {
"createdAt": timestamp
}
})
}
//Execute the bulk operation for the current chunk
bulk.execute((error, result) => {
if (error) {
console.error('Error while inserting products' + JSON.stringify(error))
next()
}
else {
//Atleast one upsert has happened
upsertHappened = true;
inserted += result.nInserted
upserted += result.nUpserted
matched += result.nMatched
modified += result.nModified
removed += result.nRemoved
//Move to the next chunk
next()
}
})
}
else {
console.log("Calling finish")
finish()
}
}
function next() {
current++;
//Reassign bulk to a new object and call load once again on the new object after incrementing chunk
bulk = models.Product.collection.initializeUnorderedBulkOp();
setTimeout(load, 0)
}
function finish() {
console.log('Inserted ', inserted + ' Upserted ', upserted, ' Matched ', matched, ' Modified ', modified, ' Removed ', removed)
//If atleast one chunk was inserted, remove all items with a 0% discount or not updated in the latest upsert
if (upsertHappened) {
console.log("Calling remove")
remove()
}
}
/**
* Remove all the items that were not updated in the recent upsert or those items with a discount of 0
*/
function remove() {
models.Product.remove(
{
"$or":
[{
"updatedAt": { "$lt": timestamp }
},
{
"discount": { "$eq": 0 }
}]
}, (error, obj) => {
if (error) {
console.log('Error while removing', JSON.stringify(error))
}
else {
if (obj.result.n === 0) {
console.log('Nothing was removed')
} else {
console.log('Removed ' + obj.result.n + ' documents')
}
}
}
)
}
}
您可以使用猫鼬的 Model.bulkWrite()
const res = await Character.bulkWrite([
{
updateOne: {
filter: { name: 'Will Riker' },
update: { age: 29 },
upsert: true
}
},
{
updateOne: {
filter: { name: 'Geordi La Forge' },
update: { age: 29 },
upsert: true
}
}
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.