Javascript Mongoose - 如果不存在则创建文档,否则在任一情况下更新-返回文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33305623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:56:06  来源:igfitidea点击:

Mongoose - Create document if not exists, otherwise, update- return document in either case

javascriptnode.jsmongooserefactoring

提问by Connor

I'm looking for a way to refactor part of my code to be shorter and simpler, but I don't know Mongoose very well and I'm not sure how to proceed.

我正在寻找一种方法来重构我的部分代码,使其更短更简单,但我不太了解 Mongoose,我不确定如何继续。

I am trying to check a collection for the existence of a document and, if it doesn't exist, create it. If it does exist, I need to update it. In either case I need to access the document's contents afterward.

我正在尝试检查集合中是否存在文档,如果不存在,则创建它。如果它确实存在,我需要更新它。无论哪种情况,我都需要在之后访问文档的内容。

What I've managed to do so far is query the collection for a specific document and, if it's not found, create a new document. If it is found, I update it (currently using dates as dummy data for this). From there I can access either the found document from my initial findoperation or the newly saved document and this works, but there must be a better way to accomplish what I'm after.

到目前为止,我设法做的是查询特定文档的集合,如果找不到,则创建一个新文档。如果找到,我会更新它(目前使用日期作为虚拟数据)。从那里我可以访问从我的初始find操作中找到的文档或新保存的文档,这很有效,但必须有更好的方法来完成我所追求的。

Here's my working code, sans distracting extras.

这是我的工作代码,没有分散注意力的额外内容。

var query = Model.find({
    /* query */
}).lean().limit(1);

// Find the document
query.exec(function(error, result) {
    if (error) { throw error; }
    // If the document doesn't exist
    if (!result.length) {
        // Create a new one
        var model = new Model(); //use the defaults in the schema
        model.save(function(error) {
            if (error) { throw error; }
            // do something with the document here
        });
    }
    // If the document does exist
    else {
        // Update it
        var query = { /* query */ },
            update = {},
            options = {};

        Model.update(query, update, options, function(error) {
            if (error) { throw error; }
            // do the same something with the document here
            // in this case, using result[0] from the topmost query
        });
    }
});

I've looked into findOneAndUpdateand other related methods but I'm not sure if they fit my use case or if I understand how to use them correctly. Can anyone point me in the right direction?

我已经研究过findOneAndUpdate和其他相关方法,但我不确定它们是否适合我的用例,或者我是否了解如何正确使用它们。任何人都可以指出我正确的方向吗?

(Probably) Related questions:

(大概)相关问题:



Edit

编辑

I didn't come across the question pointed out to me in my searching, but after reviewing the answers there I've come up with this. It's certainly prettier, in my opinion, and it works, so unless I'm doing something horribly wrong I think my question can probably be closed.

我在搜索中没有遇到向我指出的问题,但是在查看了那里的答案后,我想出了这个。在我看来,它当然更漂亮,而且有效,所以除非我做错了什么,否则我认为我的问题可能会结束。

I would appreciate any additional input on my solution.

我将不胜感激对我的解决方案的任何额外投入。

// Setup stuff
var query = { /* query */ },
    update = { expire: new Date() },
    options = { upsert: true };

// Find the document
Model.findOneAndUpdate(query, update, options, function(error, result) {
    if (!error) {
        // If the document doesn't exist
        if (!result) {
            // Create it
            result = new Model();
        }
        // Save the document
        result.save(function(error) {
            if (!error) {
                // Do something with the document
            } else {
                throw error;
            }
        });
    }
});

回答by

You are looking for the newoption parameter. The newoption returns the newly created document(if a new document is created). Use it like this:

您正在寻找new选项参数。该new选项返回新创建的文档(如果创建了新文档)。像这样使用它:

var query = {},
    update = { expire: new Date() },
    options = { upsert: true, new: true, setDefaultsOnInsert: true };

// Find the document
Model.findOneAndUpdate(query, update, options, function(error, result) {
    if (error) return;

    // do something with the document
});

Since upsertcreates a document if not finds a document, you don't need to create another one manually.

由于upsert如果找不到文档则创建文档,因此您无需手动创建另一个文档。

回答by Jossef Harush

Since you wish to refactor parts of your code to be shorter and simpler,

由于您希望将部分代码重构为更短更简单,

  1. Use async / await
  2. Use .findOneAndUpdate()as suggested in this answer
  1. async / await
  2. .findOneAndUpdate()按照此答案中的建议使用


let query = { /* query */ };
let update = {expire: new Date()};
let options = {upsert: true, new: true, setDefaultsOnInsert: true};
let model = await Model.findOneAndUpdate(query, update, options);

回答by Rahul Sadaphal

///This is simple example explaining findByIDAndUpdate from my code added with try catch block to catch errors
try{
const options = {
            upsert: true,
            new: true,
            setDefaultsOnInsert: true
        };
        const query = {
            $set: {
                description: req.body.description,
                title: req.body.title
            }
        };
        const survey = await Survey.findByIdAndUpdate(
            req.params.id,
            query,
            options
        ).populate("questions");
}catch(e){
console.log(e)
}

回答by hongkongbboy

Here's an example:

下面是一个例子:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/rsvp', {useNewUrlParser: true, useUnifiedTopology: true});

const db = mongoose.connection;

db.on('error', () => {
  console.log('mongoose connection error');
});

db.once('open', () => {
  console.log('mongoose connected successfully');
});

const rsvpSchema = mongoose.Schema({
  firstName: String,
  lastName: String,
  email: String,
  guests: Number
});

const Rsvp = mongoose.model('Rsvp', rsvpSchema);


// This is the part you will need... In this example, if first and last name match, update email and guest number. Otherwise, create a new document. The key is to learn to put "upsert" as the "options" for the argument.
const findRsvpAndUpdate = (result, callback) => {

  Rsvp.findOneAndUpdate({firstName: result.firstName, lastName: result.lastName}, result, { upsert: true }, (err, results) => {
    if (err) {
      callback(err);
    } else {
      callback(null, results);
    }
  })
};


// From your server index.js file, call this...
app.post('/rsvps', (req, res) => {
  findRsvpAndUpdate(req.body, (error, result) => {
    if (error) {
      res.status(500).send(error);
    } else {
      res.status(200).send(result);
    }
  })
});