Mongoose findOneAndUpdate和upsert没有返回错误,没有受影响的文档

时间:2022-07-26 02:35:54

I have a very minimal model:

我有一个非常小的模型:

var CompanySchema = new mongoose.Schema({
    name: { type: String, required: true, unique: true },
});

var Company = mongoose.model('Company', CompanySchema)

I am attempting to add a single document if it doesn't exist. Currently, there are no documents while I test:

如果一个文档不存在,我尝试添加它。目前,我测试的时候没有文件:

models.Company.findOneAndUpdate({
    name: 'companyName'
}, {upsert: true}, function(err, numberAffected, raw){
    console.log(err, numberAffected, raw)
})

This is using the upsert options from the Mongoose docs

这是使用来自Mongoose文档的upsert选项。

However err is null, numberAffected is null. Why isn't my document updated?

无论err为空,numberAffected为空。为什么我的文档没有更新?

2 个解决方案

#1


23  

in your code you are using the 3 parameter version of the method findOneAndUpdate so, according to the documentation you posted, the parameters are: A.findOneAndUpdate(conditions, update, callback).

在您的代码中,您使用的是方法findOneAndUpdate的3个参数版本,因此,根据您发布的文档,参数是:A。findOneAndUpdate(条件、更新回调)。

You should use the 4th parameters version of the method to specify the upsert option.

您应该使用方法的第4个参数版本来指定upsert选项。

I would like to point out that I never used the framework mongoose. Hope this helps.

我想指出的是,我从未使用过猫鼬的骨架。希望这个有帮助。

Edit: Yes, in your case, conditions and update are the same. If your object is more complex then the one showed in the example, you might want to check for the _id or a "unique" (not guaranteed by MongoDB) attribute (better if it has an index on it). For example:

编辑:是的,在你的情况下,条件和更新是一样的。如果您的对象比示例中显示的更复杂,那么您可能想要检查_id或“unique”(MongoDB不保证)属性(如果它有一个索引就更好)。例如:

var myObj = ...;
collection.findOneAndUpdate({uniqueAttr: myObj.uniqueAttr}, myObj, {upsert: true}, function(){
     ...
});

#2


19  

As of Mongoose 4+, do not forget to set new: true along with upsert or you will get the old document as a return value, not the updated one.

对于Mongoose 4+,不要忘记设置new: true和upsert一起,否则您将得到旧文档作为返回值,而不是更新后的文档。

This is quite tricky especially when the request creates a document, as if you do not specify new: true, you receive a null document (there was no existing doc), but no error.

这是相当棘手的,尤其是当请求创建一个文档时,就好像您没有指定new: true,您会接收一个空文档(没有现有的doc),但是没有错误。

    var myObj = ...;
    collection.findOneAndUpdate(
    {uniqueAttr: myObj.uniqueAttr},
    myObj,
    {upsert: true, new: true},
    function(...) {...}

#1


23  

in your code you are using the 3 parameter version of the method findOneAndUpdate so, according to the documentation you posted, the parameters are: A.findOneAndUpdate(conditions, update, callback).

在您的代码中,您使用的是方法findOneAndUpdate的3个参数版本,因此,根据您发布的文档,参数是:A。findOneAndUpdate(条件、更新回调)。

You should use the 4th parameters version of the method to specify the upsert option.

您应该使用方法的第4个参数版本来指定upsert选项。

I would like to point out that I never used the framework mongoose. Hope this helps.

我想指出的是,我从未使用过猫鼬的骨架。希望这个有帮助。

Edit: Yes, in your case, conditions and update are the same. If your object is more complex then the one showed in the example, you might want to check for the _id or a "unique" (not guaranteed by MongoDB) attribute (better if it has an index on it). For example:

编辑:是的,在你的情况下,条件和更新是一样的。如果您的对象比示例中显示的更复杂,那么您可能想要检查_id或“unique”(MongoDB不保证)属性(如果它有一个索引就更好)。例如:

var myObj = ...;
collection.findOneAndUpdate({uniqueAttr: myObj.uniqueAttr}, myObj, {upsert: true}, function(){
     ...
});

#2


19  

As of Mongoose 4+, do not forget to set new: true along with upsert or you will get the old document as a return value, not the updated one.

对于Mongoose 4+,不要忘记设置new: true和upsert一起,否则您将得到旧文档作为返回值,而不是更新后的文档。

This is quite tricky especially when the request creates a document, as if you do not specify new: true, you receive a null document (there was no existing doc), but no error.

这是相当棘手的,尤其是当请求创建一个文档时,就好像您没有指定new: true,您会接收一个空文档(没有现有的doc),但是没有错误。

    var myObj = ...;
    collection.findOneAndUpdate(
    {uniqueAttr: myObj.uniqueAttr},
    myObj,
    {upsert: true, new: true},
    function(...) {...}