最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to know if Mongoose's upsert created a new document? - Stack Overflow

matteradmin6PV0评论

I have this code in node.js/express.js:

var User = mongoose.model('User');
var usersRouter = express.Router();
usersRouter.put('/:id', function(req, res) {
    req.body._id = req.params.id;
    var usr = new User(req.body);

    usr.validate(function (err) {
        if (err) {
            res.status(400).json({});
            return;
        }

        var upsertData = usr.toObject();
        delete upsertData._id;

        User.update({_id: usr._id}, upsertData, {upsert: true}, function(err) {
            if (err) {
                res.status(500).json({});
                return;
            }

            res.status(204).json({});
        });
    });
});

It works fine, but I would like to send a different response to the client if a new document has been created (status 201 with json in response body) or an existing one has been updated (status 204).

Is there a way to tell the difference from the callback of User.update?

I have this code in node.js/express.js:

var User = mongoose.model('User');
var usersRouter = express.Router();
usersRouter.put('/:id', function(req, res) {
    req.body._id = req.params.id;
    var usr = new User(req.body);

    usr.validate(function (err) {
        if (err) {
            res.status(400).json({});
            return;
        }

        var upsertData = usr.toObject();
        delete upsertData._id;

        User.update({_id: usr._id}, upsertData, {upsert: true}, function(err) {
            if (err) {
                res.status(500).json({});
                return;
            }

            res.status(204).json({});
        });
    });
});

It works fine, but I would like to send a different response to the client if a new document has been created (status 201 with json in response body) or an existing one has been updated (status 204).

Is there a way to tell the difference from the callback of User.update?

Share Improve this question edited Oct 27, 2014 at 17:02 Ionică Bizău 114k94 gold badges310 silver badges487 bronze badges asked Oct 27, 2014 at 16:24 hornobsterhornobster 7361 gold badge7 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Use the third parameter from callback function:

...
User.update({_id: usr._id}, upsertData, {upsert: true}, function(err, num, n) {
  if (err) {
     res.status(500).json({});
     return;
   }

   if (!n.updatedExisting) { 
       /* new document */
   }

   res.status(204).json({});
});
...

n is an object like this:

{ updatedExisting: false,
  upserted: <ObjectId>,
  n: 1,
  connectionId: 11,
  err: null,
  ok: 1 }

updatedExisting property is true when a document was updated -- so it was created before. If it's false, then it means that the new document was created during this call.

Post a comment

comment list (0)

  1. No comments so far