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
3
votes
1 answer

How to get a instance of db from node-mongo native driver?

Consider, I have MongoDB connection opened in the main app.js file itself and the following code fall in it's call back: mongodb.connect('MongoDBUrlGoesHere', function (err, db) { app.listen(app.get('port'), function AppListnCB() { …
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
3
votes
2 answers

Is there a way to represent ISODate and ObjectID fields in JSON that MongoDB will recognize?

I am trying to import a JSON file into MongoDB inside node. Here is what I am trying to do: db.open(function(err, db) { if(!err) { db.collection('orgs', {safe: true}, function(err, collection) { if (err) { console.log("The…
Scott Switzer
  • 1,064
  • 1
  • 15
  • 25
3
votes
2 answers

Passing reference to DB into routes is not working for my Node / Express project

I am using node + express to create a simple REST API, and am trying to separate out routing logic from db logic. I am having a problem getting access to the DB from the routes. Here is my server.js code: var express = require('express') , path =…
Scott Switzer
  • 1,064
  • 1
  • 15
  • 25
3
votes
2 answers

[Node.js]loop insert 1000000 documents to mongodb via native driver, why node takes up a lot of memory?

I use the following code to loop insert 1000000 documents to mongodb,but i found node process takes up a lot of memory,my client are dead. db.collection("batch_insert", function (err, collection) { if (!err) { var count = 0; for…
Steve
  • 281
  • 1
  • 3
  • 14
2
votes
0 answers

How to update a sub-document in MongoDB using Mongoose, without overriding existing fields?

I have an Organization schema with an address sub-document defined as AddressSchema. Whenever I update a single field of the address sub-document from the parent Organization model, it replaces the whole address sub-document with the updated field.…
2
votes
1 answer

mongodb node driver connect() ignores connectTimeoutMS (and socketTimeoutMS) settings

I'm using mongodb node driver version 4.7.0 (latest at the moment of writing this). I have the following fragment of code to connect to DB: var url = ""; var client = require('mongodb').MongoClient; client.connect( url, { …
fgalan
  • 11,732
  • 9
  • 46
  • 89
2
votes
0 answers

How to access fields in MongoDB insert error with NodeJS?

Well I want to do some basic error handling and return the _id of the bad insert. When I am in mongo console and try to insert a duplicate document, then this error is thrown: > db.users.insertOne({ firstname: "", lastname: ""…
Baesm
  • 83
  • 9
2
votes
1 answer

Node Mongodb Driver: different result on aggregate

how you doing? I have a trouble making a aggregation in my project, my aggregation result is different in Robo3T and Node. db.getCollection('companies').aggregate([ { '$match': { _id: { '$eq': ObjectId("5e30a4fe11e6e80d7fb544a4")} } }, {…
2
votes
0 answers

Loopback Connector MongoDB - connection pooling

We are using loopback-connector-mongodb@4.2.0 version to connect to mongo db . Our database admins are reporting that our application is using more connections and it is constantly growing. When they report issues , we just restart our applications…
2
votes
3 answers

how to use passport.js local strategy without creating a schema,or database model

I was trying to understand how to use the passport.js in Mongodb native driver,Express now the problem is that all the reference or tutorials are showing the LOCAL-STRATEGY by using mongoose which creates a schema or model........so now iam STUCK
Varun M
  • 21
  • 1
2
votes
0 answers

MongoError: user not allowed to create 2d index

I am creating a program to list nearby objects based on the user's current location. For this I am using the old [longitude, latitude] system. I will store these coordinates in the location field. The following error shows up when I try to create a…
2
votes
2 answers

How do I gracefully shutdown mongoose's connection pool?

If I receive a SIGINT/SIGTERM (e.g. ctrl+c) then I must gracefully stop my app and close all connections to the mongodb server. Most documentation/tutorials state that to stop a connection (or pool of connections), I must use mongoose.disconnect()…
lonix
  • 14,255
  • 23
  • 85
  • 176
2
votes
2 answers

How to mock toArray() of mocked find()?

For this method content.js const content = await Content.findOne({ _id: articleId }) I do the mock like: content.test.js Content.findOne = jest.fn(() => Promise.resolve({ some: 'content' })) But how do I mock a find.toArray() method which is used…
user3142695
  • 15,844
  • 47
  • 176
  • 332
2
votes
2 answers

How to handle MongoDB Client connection error after it has been cached Mongo shuts down ?

I have created a mongodb native connection and saved it and then using findOne to query a document. const Promise = require("bluebird"); const MongoClient = require('mongodb').MongoClient; let mongoDB = undefined; const getCollection = (collName)…
vashishatashu
  • 7,720
  • 7
  • 29
  • 34
2
votes
0 answers

Change collection with node-mongodb-native

I want change collections with another query's result. MongoClient.connect(mongo_url, function (err, db) { db.collection('col1').find({}).limit(1).toArray(function (err, docs) { if(_.isEmpty(docs)) { …