0

Say, i have a document in mongodb collection:

{
  _id: 'some_mongodb_id',
  name: 'john doe',
  phone: '+12345678901',
}

I have updated json object that looks like this:

{
  _id: 'some_mongodb_id',
  name: 'Dan smith',
  phone: undefined,
}

I want to update the document in mongodb so it would look like that:

{
  _id: 'some_mongodb_id',
  name: 'Dan smith',
}

How do i do that?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
stkvtflw
  • 12,092
  • 26
  • 78
  • 155
  • 1
    Already a negative response. Possibly because you should in fact be looking at the provided "keys" and their values and "stripping" the `undefined` values from them. Also as a friendly note, not my downvote here, but if you are not going to accept answers to the questions you ask then a negative response is likely what you are going to get. Your account has "a lot" of unaccepted answers to questions you have asked. They can't all be wrong. Perhaps you should spend some time responding to the nice people who answered your previous questions first. – Neil Lunn Jul 20 '17 at 05:55
  • To know this was a duplicate, you had to have more information than the poster. It's a valid question... – Diesel Mar 04 '19 at 04:47

1 Answers1

6

You can use this query to remove a field from a document.

db.yourcollection.update(
 {_id: 'some_mongodb_id'},
 { $unset: { phone: undefined} }
);
Rohit
  • 2,987
  • 3
  • 25
  • 50