-1

I want to delete a row in a document via using Mongoose, a wrapper for MongoDB

// this is the document
{
  'username': 'Timmy',
  'password': 'username',
  'token': '83b234bj1n23q8w' // <= I want to delete this row in the document
}

and how do I delete it?

Felix Isaac
  • 312
  • 6
  • 13

1 Answers1

2
db.users.update(
   { username: "Timmy" },
   { $unset: { token: "" } }
)

check out the MongoDB $unset documentation for more information.

To do it strictly through Mongoose, it looks like this stackover flow post goes into some workarounds for unsetting fields directly at the Mongoose model level, although some of them look relatively hacky and are version-dependent.

jakemingolla
  • 1,613
  • 1
  • 10
  • 13
  • Umm... I'm using mongoose for MongoDB like ```js db.users.findOneAndUpdate({ username: 'Timmy' }, { $unset: { 'token': 'or something' }, function (err, res) { if (err) console.log(err); }) ``` – Felix Isaac Mar 20 '19 at 15:41
  • yep, the `findOneAndUpdate` is a wrapper around the MongoDB [`db.collection.findOneAndUpdate`](https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/) method. Unless I am mistaken, Mongoose should expose the underlying collection for you to make the update against – jakemingolla Mar 20 '19 at 15:43
  • Yep it works, thanks! – Felix Isaac Mar 20 '19 at 16:04