$unset is not working in mongoose
Asked Answered
S

3

6

My schema is as follows : -

var Schema1 = new mongoose.Schema({
  name: String,
  description: String,
  version: [{
    id: String,
    status: Number
 }]
});

I want to unset the version field. I try the following code :-

Schema1.update({}, {
 $unset: {
  version: 1
  }
 }, {
 multi: true
}).exec(function(err, count) {
 console.log(err, count)
});

It gives me the following output :-

null 10

But the output contain the version field :-

{
  name : 'a',
  description : 'sdmhf',
  version : []
}

The above code remove the data but I want to remove the version field from my collection as well. Can you tell me how to do that?

Scaffolding answered 28/5, 2014 at 10:30 Comment(4)
fyi, in my answer below I'm assuming that Schema1 points to the Schema Model and not the Schema itselfRaney
yes, I am using the simple name to understand. But I have to remove the version field from my collection and all documents.Scaffolding
The short answer is the code is doing just that. But if you use mongoose without updating your schema, it will keep returning the version property with an empty array.Raney
So if i remove it from schema it will remove the version field from existed document. I think it will work for new document.But I want to remove it from existing document as well.Scaffolding
R
10

There's nothing wrong with your code. Mongoose is actually deleting those fields in the documents (which I assume is what you expected). You can see by opening a mongo shell into your database and searching all your documents before and after the update (use db.yourcollection.find({}))

Why does an empty array still appear even when it's removed from every document in the collection? Mongoose will ensure the documents returned will obey the schema that you define. So even if Mongoose finds no version property pointing to an Array in the actual document, it will still present an empty array when the matching documents are returned.

You can verify this yourself by adding some arbitrary property (pointing to an Array) to your schema and running a .find({}) again. You'll see that Mongoose will return these properties in every document even though you never saved them to the database. Similarly, if you add non-Array properties like Strings, Booleans, etc, Mongoose will return those as long as you specify a default value.

If you want to drop version for good (as you mentioned in your comment) you can drop it from your Mongoose schema after you've completed the $unset.

Raney answered 28/5, 2014 at 12:24 Comment(0)
B
2

This worked for me

doc.field = undefined
await doc.save()
Bloxberg answered 20/7, 2020 at 14:5 Comment(0)
T
0

I too had the same issue. It turns out I forget to await the update operation. So, my mongoose was disconnecting from the db before the operation.

This fixed my problem

await Schema1.update({}, {
 $unset: {
  version: 1
  }
 }, {
 multi: true
}).exec(function(err, count) {
 console.log(err, count)
});

Don't forget the await

Thyrsus answered 11/10, 2024 at 23:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.