有什么方法可以使用 mongoose 从 mongodb 的批量操作中获取修改后的 IDS? [英] Any way to get the modified IDS from mongodb's bulk operations using mongoose?

查看:36
本文介绍了有什么方法可以使用 mongoose 从 mongodb 的批量操作中获取修改后的 IDS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let dbOperations = Edge.collection.initializeOrderedBulkOp()
edges.forEach(edge => {
    dbOperations.find({_id: edge.id}).upsert().updateOne({
        $set: {
            value: edge.value
        },
        $setOnInsert: {
            _id: edge.id
        }
    })
})
dbOperations.execute()
    .then(result => {
        console.log(result.nModified) // This shows the number of edges that is actually modified
        console.log(result.getModifiedIds()) // This is what I want to achieve
    })

有什么方法可以实现吗?

Any way to achieve this?

推荐答案

嗯,从一个角度来看,答案是不",这是有充分理由的.

Well from one point of view the anwer is "no" and there is a very good reason for that.

一般来说,MongoDB 的更新"操作旨在处理通常的多个"文档,因此意味着与条件匹配的任何内容.因此,这里的一般情况是您要求以单数形式更新或通过选择进行更新的内容是否已更新,具体取决于是否匹配任何内容.

Generally speaking, MongoDB "update" operations are intended to work across what is commonly "multiple" documents, therefore meaning whatever matched the criteria. So the general case here is whatever you either asked to be updated in either singular or by selection was either updated or not depending on whether anything was matched.

在批量"上下文中,大部分相同的事情都适用,因为有一个条件匹配或不匹配,在这种情况下,您将获得 nMatchednModified 的返回值 反过来,因为也有可能匹配"的文档实际上并未更新,其中要修改的数据已经是修改目标的值.

In the "Bulk" context, much of the same thing applies, in that there was either a criteria match or not, in which case you will get returned values for nMatched and nModified repectively, as there is also the possibility that a "matched" document is not actually updated where the data present to be modified is already the value which is the target of the modification.

nMatchednModified 之间的最后一个区别是 你不能可靠地做到这一点"的主要原因,因为并非所有匹配的东西都是必然的修改.

That last distinction between nMatched and nModified is the prime reason why "you cannot reliably do this", since not everything matched is necessarily modified.

然而,在区分更新插入"操作和实际更新"的情况下,您可以创建一个估计值.由于上述区别,它不会是 100% 准确的,但基本过程是将您的输入列表与 getUpsertedIds() 的返回值进行比较,这是一个有效的调用.

You can however make a guestimate value in the case of discerning between "upsert" actions and actual "updates". It won't be 100% acurate because of the noted distintion, but the basic process is to compare your input list to the returned value from getUpsertedIds(), which is a valid call.

目前在世界其他地方避免使用 ES6 语法:

Eschewing the ES6 syntax for the rest of the world at present:

var upserted = result.getUpsertedIds();    // get this from the bulk result

upserted = upserted.map(function(up) { return up._id }); // filter out just the _id values

var modifiedIds = edges.map(function(edge) {    // get _id only from source 
    return edge.id;  
}).filter(function(edge) {
    return upserted.indexOf(edge) == -1;        // and return only non upserted
});

其中从 .getUpsertedIds() 返回的结果是一个对象数组,其中包含来自批量更新的索引"位置和生成或提供的 _id 值的upsert".

Where the returned result of from .getUpsertedIds() is an array of objects containing both the "index" position from the bulk update and the generated or supplied _id value of the "upsert".

[ { index: 0, _id: 1 } ]

因此将您的输入列表与更新"列表进行匹配以查看不存在的内容",基本上返回可能刚刚修改的内容.当然需要注意的是,如果该值已经与修改相同,那么它真的根本就不是修改.

So matching out your input list against the "upserted" list to see "what is not there", basically returns things that where probably just modified. With of course the caveat that if the value was already the same as the modification, then it really was not a modification at all.

但由于 API 的工作方式,这与您将要获得的一样接近.

But due to how the API is meant to work, that's as close as you are going to get.

这篇关于有什么方法可以使用 mongoose 从 mongodb 的批量操作中获取修改后的 IDS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆