db-migrate / db-migrate/mongodb

Remove deleteMany not working

Open
#38 0 comments 8 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
25
Forks
57
PR merge metrics
No merged PRs in 30d

Description

Looking at https://github.com/db-migrate/mongodb/blob/master/index.js#L337 the `deleteMany` method is not going to work.

We check if `options` is an array ` if(util.isArray(options))` and then add it in the query.

```js
case 'remove':
// options is the records to insert in this case
if(util.isArray(options))
db.collection(collection).deleteMany(options, callbackFunction);
else
db.collection(collection).deleteOne(options, callbackFunction);
break;
```
The issue is `filter` from `deleteMany(filter)` is expected to be an [object](https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/) and not an array and will return an error: `MongoError: BSON field 'delete.deletes.q' is the wrong type 'array', expected type 'object'`

Here the code I was using:
```js

db._run("remove", "pets3", { id: "003" }, callback); // This removes one entry.
db._run("remove", "pets3", [{ id: "003" }], callback); // This fails obviously.

// This code works and it's using directly the mongo instance.
const getDbInstance = await db._run("getDbInstance");
getDbInstance.collection("pets3").deleteMany({ id: "003" }); // This removes every entries.
getDbInstance.close();
```

---
Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/75967800-remove-deletemany-not-working?utm_campaign=plugin&utm_content=tracker%2F12293389&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F12293389&utm_medium=issues&utm_source=github).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.