Questions tagged [node-mongodb-native]

The MongoDB Native Node.js driver is an officially supported driver providing a native asynchronous Node.js interface to MongoDB. It can be used on its own, but it also serves as the basis of several object mapping libraries, such as Mongoose.

The MongoDB Native Node.js driver is an officially supported driver written in pure JavaScript to provide a native asynchronous Node.js interface to MongoDB. The driver is optimized for simplicity. It can be used on its own, but it also serves as the basis of several object mapping libraries, such as Mongoose ODM.

Installing/upgrading

The easiest way to install the driver is to use npm:

$ npm install mongodb

Documentation

3.0 Driver

2.2 Driver

Related links

482 questions
57
votes
12 answers

How to check if a collection exists in Mongodb native nodejs driver?

I need to check if a collection exists on a certain database and create it if it doesn't. I know that db.createCollection(collName, {strict:true}, function(error, collection)) checks for existance of collection collName before creating it and sets…
Nasser Torabzade
  • 6,490
  • 8
  • 27
  • 36
33
votes
5 answers

Why am I getting error "Trying to open unclosed connection."?

I am trying to connect my node app to mongodb via mongoose. It seems to be working, as I can add documents, but I get the error { [Error: Trying to open unclosed connection.] state: 2 }. I created a very simple app, just to make sure everything is…
hal
  • 4,845
  • 7
  • 34
  • 57
32
votes
1 answer

MongoDB sorting by nested object value

I'm trying to sort results based on the values of a nested object. Using node-mongodb-native, I'm doing this: this.collection.find({ "_id": ObjectID(item_id) }, { "items": 1 }, { sort : { items.date : 1 } },…
dzm
  • 22,844
  • 47
  • 146
  • 226
30
votes
4 answers

How to connect with username/password to mongodb using native node.js driver

I'm using native mongo driver in Joyent cloud, the node.js application runs fine locally but in Joyent when I run with the username/password that they provided it fails to connect. This is the code I use to connect: var db = new MongoDB(dbName, new…
santosh
  • 301
  • 1
  • 3
  • 5
27
votes
1 answer

Using binary data from Mongo collection as image source

I have an express app, storing data in mongo, using Jade as the view engine. I have a simple route that gets the docs in a particular collection, each doc corresponding to a product. The image is base64 encoded. When I try and render as an image…
Philip O'Brien
  • 4,146
  • 10
  • 46
  • 96
25
votes
7 answers

What is best way to handle global connection of Mongodb in NodeJs

I using Node-Mongo-Native and trying to set a global connection variable, but I am confused between two possible solutions. Can you guys help me out with which one would be the good one? 1. Solution ( which is bad because every request will try to…
NitinD
  • 489
  • 2
  • 7
  • 21
25
votes
5 answers

How can I promisify the MongoDB native Javascript driver using bluebird?

I'd like to use the MongoDB native JS driver with bluebird promises. How can I use Promise.promisifyAll() on this library?
Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
22
votes
6 answers

Remove record by id?

Why I can't remove record by _id? Code: db.collection('posts', function(err, collection) { collection.remove({_id: '4d512b45cc9374271b00000f'}); });
Sable
  • 357
  • 2
  • 4
  • 6
20
votes
3 answers

Fail to connect Mongoose to Atlas

I'm always connecting to the "admin" DB, which is a fixed bug. Using Mongoose 5.0.6 MongoDb 3.6 and trying to connect to Atlas. My question, what driver Mongoose 5.0.6 depend on? How can I find out when Mongoose will have that fix? On a different…
HatzavW
  • 560
  • 1
  • 4
  • 16
19
votes
2 answers

mongodb-nodejs-driver, DeprecationWarning: collection.count is deprecated

I want to get the count of posts documents using: db.collection('posts').count() But, I got a warning: DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or…
Lin Du
  • 88,126
  • 95
  • 281
  • 483
18
votes
1 answer

How to set Auto Reconnect with Unified Topology

After setting useUnifiedTopology=true, the Auto Reconnect stopped working and generates the following ERRORS: DeprecationWarning: The option `reconnectInterval` is incompatible with the unified topology DeprecationWarning: The option…
TBE
  • 1,002
  • 1
  • 11
  • 32
15
votes
3 answers

Connection to Mongodb-Native-Driver in express.js

I am using mongodb-native-driver in express.js app. I have around 6 collections in the database, so I have created 6 js files with each having a collection as a javascript object (e.g function collection(){}) and the prototypes functions handling…
Saransh Mohapatra
  • 9,430
  • 10
  • 39
  • 50
14
votes
6 answers

nextjs import but don't invoke function throws Module not found: Error: Can't resolve 'dns'

My project(NextJS) was working fine and suddenly I am experiencing the issue ModuleNotFoundError. Particularly in the case of dynamic routing of nextJs. Error I see is: Module not found: Error: Can't resolve 'dns' In the pages directory…
Priyabrata
  • 629
  • 1
  • 8
  • 23
14
votes
1 answer

MongoDB native: is there any difference between toString and toHexString methods?

I'm using Node.js v0.12.0 with MongoDB driver v1.4.34. So, is there any difference between converting ObjectID to String with toString and toHexString methods?
JustLogin
  • 1,822
  • 5
  • 28
  • 50
13
votes
2 answers

How to buffer MongoDB inserts during disconnect in node.js?

We do read an XML file (using xml-stream) with about 500k elements and do insert them into MongoDB like this: xml.on(`endElement: product`, writeDataToDb.bind(this, "product")); Insert in writeDataToDb(type, obj) looks like…
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
1
2 3
32 33