question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should mongoose show an error/warning if trying to delete on non-existing properties?

See original GitHub issue

I was deleting two models based on a given court, and noticed a discrepancy in the logs (one time ObjectId and one time string):

Mongoose: bookings.remove({ court: ObjectId("5764ceaed5b3e76b78c149e9") }) {}  
Mongoose: events.remove({ court: '5764ceaed5b3e76b78c149e9' }) {}  

After further investigation, I realised the difference between the two models is that for events the court properly doesn’t exist (it should have been courts). Should mongoose not throw an error/warning if we’re deleting on properties which are not defined on the model?

What is the expected behaviour if we do this?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hasezoeycommented, Aug 26, 2022

with the current mongoose version it removes those paths from the query, which could result in unwanted (and silent) behavior:

// NodeJS: 18.7.0
// MongoDB: 5.0 (Docker)
// Typescript 4.7.4
import * as mongoose from 'mongoose'; // mongoose@6.5.2

const userSchema = new mongoose.Schema({ prop1: String });

const UserModel = mongoose.model('User', userSchema);

(async () => {
  await mongoose.connect(`mongodb://localhost:27017/`, {
    dbName: 'verifyMASTER',
  });

  mongoose.set('debug', true);

  // Mongoose: users.remove({}, {})
  // remove { acknowledged: true, deletedCount: 0 }
  console.log('remove', await UserModel.remove({ nonExistent: true }));
  // Mongoose: users.deleteOne({}, {})
  // deleteOne { acknowledged: true, deletedCount: 0 }
  console.log('deleteOne', await UserModel.deleteOne({ nonExistent: true }));
  // Mongoose: users.deleteMany({}, {})
  // deleteMany { acknowledged: true, deletedCount: 0 }
  console.log('deleteMany', await UserModel.deleteMany({ nonExistent: true }));

  await mongoose.disconnect();
})();

Note: the above code was run against a empty collection, that is why deletedCount is 0

Reproduction Repository / Branch: https://github.com/typegoose/typegoose-testing/tree/mongooseGh4251

this is probably related to strict being default now and strict removing unknown properties (though silently)

1reaction
vkarpov15commented, Jun 22, 2016

By “do nothing with them” I meant they stay in the query as is and mongoose doesn’t cast them away,

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't I delete a mongoose model's object properties?
Mongoose models are not javascript objects. So convert it into a javascript object and delete the property. The code should look like this:...
Read more >
Mongoose v6.8.2: Schemas
The strict option, (enabled by default), ensures that values passed to our model constructor that were not specified in our schema do not...
Read more >
Delete Many Documents with model.remove()
I would start with this package.json file and add mongodb@~3.6.0 and ... (node:522) Warning: Accessing non-existent property 'remove' of ...
Read more >
Mistakes You're Probably Making With MongooseJS, And ...
collection('documents', function(error, collection) { collection.findOne({ _id : collection.db.bson_serializer.ObjectID.
Read more >
Node.js v19.3.0 Documentation
All errors thrown by the node:assert module will be instances of the ... or not. code <string> Value is always ERR_ASSERTION to show...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found