0

I nedd to get the new document back with _id field only. Something like this:

db.users.insert({name: 'Jonh', age: 27}, {_id: true} , function (err, user) {
   if (err) {}

   // need to be user with only _id field.
});

How can I do this?

Erik
  • 14,060
  • 49
  • 132
  • 218

1 Answers1

0

UPDATED

The second parameter to the insert callback is always an array of the inserted objects, and you can't prevent the other fields from being included. However, you can use Array#map to create the result you're looking for:

db.users.insert({name: 'Jonh', age: 27}, {_id: true} , function (err, users) {
    if (err) {}

    users = users.map(function(user) {
        return {_id: user._id};
    });

    // users now contains objects with just the _id field   
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471