2

Is .collection() cheap? Because in the example below, a and b are not the same...

import { MongoClient } from "mongodb";

const mongoClient = new MongoClient('mongodb://localhost:27017', { useUnifiedTopology: true });

mongoClient.connect().then(client => {
    const a = client.db("test").collection("demo");
    const b = client.db("test").collection("demo");
    console.log(a == b); // false

    mongoClient.close();
});

Should we use a single .collection() instance all over the application? Or is creating a .collection() cheap?

zhulien
  • 5,145
  • 3
  • 22
  • 36
Stone
  • 241
  • 1
  • 4
  • 18
  • I questioned it here, Because [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) currently not accepting any new issues... – Stone Mar 18 '21 at 19:15
  • 1
    If you're that concerned about the performance, you can check what exactly is happening here: https://github.com/mongodb/node-mongodb-native/blob/86bddf1ef516d6b8c752082e33c15624753579ab/lib/db.js#L448. This will lead you to the `Collection` constructor here: https://github.com/mongodb/node-mongodb-native/blob/86bddf1ef516d6b8c752082e33c15624753579ab/lib/collection.js#L100. – zhulien Mar 23 '21 at 16:43
  • You can also measure this. Create a few thousands of these objects and compare how much more time it took vs creating just one. – Sergio Tulentsev Mar 23 '21 at 16:59
  • Right! However, I would like to use a single `.collection()` instance anyway... – Stone Mar 25 '21 at 14:09
  • Please see [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects)] – Joe Mar 27 '21 at 09:34

2 Answers2

0

I think the line console.log(a == b); is returning False because a and b are two different unique cursors, which though have the same data, but have different unique addresses.

zhulien
  • 5,145
  • 3
  • 22
  • 36
Shreyesh Desai
  • 569
  • 4
  • 19
  • 2
    `.collection()` doesn't return a cursor, it returns an object of type `Collection`. You have a cursor when you return actual data. – zhulien Mar 23 '21 at 16:25
  • Yes correct, my bad. So is it correct if I say, those two are collections having the same data but since they are two separate instances of collection and hence cannot be equated? – Shreyesh Desai Mar 23 '21 at 19:00
  • Yes, indeed. They are 2 separate instances of `Collection` hence the difference in their reference equality. – zhulien Mar 23 '21 at 19:25
0

I think it isn't because .collection, its because client.db.

MongoDB Node driver is not single connection based. It uses connection pooling and the default poolSize is 5.

Since you try to connect the same database with different connections from connection pool, the cursor you're using is different aswell.

From my knowledge, you should create and use new collection objects from your database object, the moment you'll going to use it. If you use the same collection object for your every database operation, you'll end up using single database connection instead of multiple connections in connection pool.

And if you want you can increase the poolSize of MongoDB Node driver aswell.

You can check the other options from the docs aswell. http://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html#.connect

Eagleclaw
  • 369
  • 1
  • 13