Automattic / Automattic/monk

db.create UnhandledPromiseRejectionWarning: MongoError: a collection 'mytest.test' already exists even when it's new

Open
#310 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.8k
Forks
180
PR merge metrics
No merged PRs in 30d

Description

If I try to create a new database and add a few entries to it, I get an `UnhandledPromiseRejectionWarning`.

This is the simplest script I can get to see the error (it does need to insert data):

```js
const monk = require('monk');

require('dotenv').config();
const db = monk(process.env.MONGO_URI);

const test = async () => {
var collection = db.create('test');

var records = [1, 2, 3];

const promises = records.map(a => {
return collection.insert({
name: a
});
});

await Promise.all(promises);

db.close();
};

test();

```

If the collection **_does not_** exist before running the script, I get the following error:

```
$ node src/test.js
(node:77118) UnhandledPromiseRejectionWarning: MongoError: a collection 'mytest.test' already exists
at MessageStream.messageHandler (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/connection.js:261:20)
at MessageStream.emit (events.js:198:13)
at processIncomingData (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/home/mat/dev/mytest/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at doWrite (_stream_writable.js:415:12)
at writeOrBuffer (_stream_writable.js:399:5)
at MessageStream.Writable.write (_stream_writable.js:299:11)
at Socket.ondata (_stream_readable.js:710:20)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
(node:77118) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:77118) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
```

The collection is created and the documents are successfully added.

If the collection _**does already exist**_, output is silent. The documents are added to the collection.

It's almost like it's operating backwards!

monk 7.3.0; mongodb 1:3.6.3-0ubuntu2.1 (yes, I need to shift to a newer version of Ubuntu to upgrade this)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the reproduction in src/test.js, focusing on db.create('test'), collection.insert, and the concurrent Promise.all call. Compare behavior when the collection exists versus when it is created, then verify the result against the reported MongoDB and monk versions; done means the new-collection case inserts all documents without an unhandled rejection.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.