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.

mongoose ver 6 deleteMany() removes ALL documents when search options contains field not from schema

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.4

Node.js version

12.22.12

MongoDB server version

4.4

Description

There is a collection with documents, there is it schema. At the same time, not all fields of documents are described in the schema, i.e. some documents have fields not described in the schema. We call model.deleteMany({x:<some_value>}) where “x” not in schema. This call removes all documents in the collection.

This behavior was not in version 5

Steps to Reproduce

Simple nodejs program, file index.js

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test', {});

// User model
const User = mongoose.model('User', {
	name: { type: String },
	age: { type: Number }
	// type: { type: Number }
});

User.deleteMany({}, function (err, result) { // clear collection
  if (err) return console.log(err)
  User.collection.insertMany([  // with .collection - through mongo driver, without mongoose validation
    { name: 'Vadim', age: 30 },
    { name: 'Dmitry', age: 27 },
    { name: 'Nikita', age: 20, type: 2 },
    { name: 'Mark', age: 45 },
    { name: 'Serge', age: 40, type: 2 }],
    function (err, result) {
      if (err) return console.log(err)
      console.log(result)
      // now want to delete documents with type==2
      User.deleteMany({ type: 2 }, function (err, result) {
        if (err) return console.log(err)
        console.log(result)
      })
    }
  )
}
)

file package.js:

{
  "name": "mongoose-issue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^6.5.4"
  }
}

result:

C:\Bin\node-v12.22.12-win-x64\node.exe .\index.js
{acknowledged: true, insertedCount: 5, insertedIds: {…}}
{acknowledged: true, deletedCount: 5}

same program in mongoose 5.13.15 produce

C:\Bin\node-v12.22.12-win-x64\node.exe .\index.js
(node:17288) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:17288) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
{result: {…}, ops: Array(5), insertedCount: 5, insertedIds: {…}}
insertedCount: 5
insertedIds: {0: ObjectID, 1: ObjectID, 2: ObjectID, 3: ObjectID, 4: ObjectID}
ops: (5) [{…}, {…}, {…}, {…}, {…}]
result: {ok: 1, n: 5}
__proto__: Object
{n: 2, ok: 1, deletedCount: 2}
deletedCount: 2
n: 2
ok: 1
__proto__: Object

Expected Behavior

Expected behavior - exactly as in version 5 - only documents with {x:<some_value>} should be deleted.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:9

github_iconTop GitHub Comments

2reactions
Vadim725commented, Sep 5, 2022

@hasezoey thanks, after more careful study of the problems, I also found other issue - #10946 BTW - despite of strictQuery - it is very dangerous default in v6

1reaction
hasezoeycommented, Sep 26, 2022

Is there any fix for this issue in the works? We’ve ran into a similar issue

because this would be a breaking change, it has been put off to the next major version, see #11861

the manual fix would be to use strictQuery: false as either a schema-option, or on a per-query level

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can you remove all documents from a collection with ...
In MongoDB, the db.collection.remove() method removes documents from a collection. You can remove all documents ...
Read more >
db.collection.deleteMany() — MongoDB Manual
deleteMany() throws a WriteError exception if used on a capped collection. To remove all documents from a capped collection, use db.collection.drop() .
Read more >
Mongoose | deleteMany() Function - GeeksforGeeks
Mongoose | deleteMany() Function ... The deleteMany() function is used to delete all of the documents that match conditions from the collection.
Read more >
Mongoose v6.8.2: API docs
This option is passed to Node.js socket#setTimeout() function after the MongoDB driver successfully completes. [options.family=0] «Number» Passed transparently ...
Read more >
delete all user references on other schema when ... - You.com
I understand that Mongodb/Mongoose is a non-relation database and is unlike ... ver 6 deleteMany() removes ALL documents when search options contains field...
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