Do I work with MongoDB?

Wrote a few simple functions for writing and changing files in the database. Here is an example of one of them:
let mongodb = require('mongodb'),
 MongoClient = mongodb.MongoClient,
 url = 'mongodb://localhost:27017/PlantsBot',

function addNewUser(MongoClient, collection, url, user) {
 MongoClient.connect(url, function(err, db) {
 if (err) throw err;
 let users = db.collection(collection);
 users.insert(user, function(err, docs) {
 if (err) throw err;
 console.log('User successfully added');
db.close();
});
});
};


Other functions are the same. Again, I call the connect() method of MongoClient object, and in the end cause db.close(). Only instead of insert (), I call find() or remove().

Is it normal that I constantly open and close the connection MongoClient? Whether beats it in performance?
July 8th 19 at 12:06
5 answers
July 8th 19 at 12:08
I got to bring home 3 packets of food from the store. I get out, close the door, go to the store, buy a package, take it home, open the door, I bring the package in and close the door. Then repeat it twice.
Immediately bring packages it's against my religion, because I work with Mongo.
the hint is clear, thank you - brannon_Christians commented on July 8th 19 at 12:11
can you tell me at least roughly, how can I implement a separate delete function, and write so as not to connect to the database many times? - brannon_Christians commented on July 8th 19 at 12:14
just once have an open connection - amparo_Hickle commented on July 8th 19 at 12:17
July 8th 19 at 12:10
Whether beats it in performance?

Beats
can you tell me at least roughly, how can I implement a separate delete function, and write so as not to connect to the database many times? - brannon_Christians commented on July 8th 19 at 12:13
: Make a global connection at the start, and then use it everywhere - brannon_Christians commented on July 8th 19 at 12:16
so the problem is that any action with the database is executed as a callback when calling the connect method. If you believe the official documentation, there is not shown how to first connect, and then perform the desired action, not the sounds of the callback

mongodb.github.io/node-mongodb-native/2.1/quick start - amparo_Hickle commented on July 8th 19 at 12:19
: you can try to save the link (db) in a global variable and then juzat. - brannon_Christians commented on July 8th 19 at 12:22
July 8th 19 at 12:12
Use Mongoose already and find happiness ))
for a project I need only 3 functions to work with databases. Write, modify and read. Really for the sake of this module is a separate plug in? - brannon_Christians commented on July 8th 19 at 12:15
All on your discretion, I just offered - brannon_Christians commented on July 8th 19 at 12:18
Support Alexis. - amparo_Hickle commented on July 8th 19 at 12:21
July 8th 19 at 12:14
I think something like this:

var mongodb = require('mongodb'),
 MongoClient = mongodb.MongoClient,
db;

 MongoClient.connect('mongodb://localhost:27017/PlantsBot', function(err, database) {
 if(err) throw err;

 db = database;

 // Start if the database is booted

});

 db.collection('mycollection').find({}, function(err, docs) {
...
 });
not working. The db variable after the following code is undefined - brannon_Christians commented on July 8th 19 at 12:17
And you sure the app is run after db = database;? - brannon_Christians commented on July 8th 19 at 12:20
const MongoClient = require('mongodb').MongoClient,
url = 'mongodb://localhost:27017/PlantsBot';

let db;

MongoClient.connect(url, function(err, database) {
if (err) throw err;
db=database;
console.log(db); //prints base
});

console.log(db); //undefined

Yes. I have code like this - amparo_Hickle commented on July 8th 19 at 12:23
: To express for example should be like this:
var mongodb = require('mongodb'),
 express = require('express'),
 MongoClient = mongodb.MongoClient,
 app = express(), db;

 MongoClient.connect('mongodb://localhost:27017/PlantsBot', function(err, database) {
 if(err) throw err;

 db = database;

 // Start if the database is booted
app.listen(3000);

});

 app.get('/', function(req, res) {

 db.collection('mycollection').find({}, function(err, docs) {
...
});

 });
- brannon_Christians commented on July 8th 19 at 12:26
July 8th 19 at 12:16
can you tell me at least roughly, how can I implement a separate delete function, and write so as not to connect to the database many times?


"Pattern Repository"

Find more questions by tags Node.jsMongoDB