10 examples of 'connect local mongodb to node js' in JavaScript

Every line of 'connect local mongodb to node js' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
58async connect() {
59 this.client = await MongoClient.connect(this.config.url, this.config.options);
60 this.db = this.client.db(this.config.dbName);
61}
16connect() {
17 return new Promise((resolve, reject) => {
18 Mongoose.connect(`mongodb://localhost:${this.port}/${this.dbName}`);
19 Mongoose.connection.on('connected', () => {
20 this.logger(`Connect to database name: ${this.dbName} on port: ${this.port}`);
21 resolve(Mongoose);
22 });
23
24 Mongoose.connection.on('error', (err) => {
25 this.logger(`Mongoose default connection error: ${err}`);
26 reject('Connection failed.');
27 });
28 });
29}
5export default async function connectToMongo(port) {
6 const mongoUrl = `mongodb://localhost:${port}/database`;
7
8 // This prebuilt guy is pretty jankeriffic, we can't await because it doesn't
9 // call the callback when it succeeds
10 denodeify(mongoPrebuilt.start_server.bind(mongoPrebuilt))({
11 args: {
12 port,
13 dbpath: './db',
14 },
15 });
16
17 return await MongoClient.connect(mongoUrl);
18};
45export async function connect(
46 url: string,
47 dbName: string
48): Promise {
49 const dbClient = await connectToMongoDb(url, { useNewUrlParser: true });
50 return new PhenylMongoDbConnection({ dbClient, dbName });
51}
33connect() {
34 return Promise.resolve()
35 .then(() => {
36 this.connections++;
37
38 if(this.db) {
39 return;
40 }
41
42 return MongoClient.connect(this.url)
43 .then(db => {
44 log.debug(`connected to ${ this.url }`);
45 this.db = db;
46 })
47 .catch(err => {
48 log.error(`Failed to connect to ${ this.url } for ${err.message}`);
49 throw new Error(err);
50 });
51 });
52}
4function connect (remote, db_name) {
5 var db = new pouchdb(path_join('data', db_name)),
6 opts = {
7 continuous: true
8 };
9
10 db.replicate.to([remote, db_name].join('/'), opts);
11 db.replicate.from([remote, db_name].join('/'), opts);
12
13 return db;
14}
233function openMongo(callback) {
234 /*
235 if (noInsert) {
236 console.log('skip to connect mongodb!');
237 callback(null);
238 return
239 }
240 */
241
242 var mongoConf = nconf.get('mongo'),
243 mongoServer = new mongo.Server(mongoConf.host, mongoConf.port, {}),
244 mongoConn = new mongo.Db(mongoConf.database, mongoServer);
245
246 mongoConn.open(function (err) {
247 if (err) { console.trace(); processError(err); }
248
249 if (!!mongoConf.user && !!mongoConf.password) {
250 mongoConn.authenticate(mongoConf.user, mongoConf.password, function(err) {
251 processError(err);
252
253 console.log('mysql connection is opened with authentication!');
254 callback(mongoConn);
255 });
256 } else {
257 console.log('mysql connection is opened without authentication!');
258 callback(mongoConn);
259 }
260 });
261}
14function connect () {
15 return MongoClient
16 .connect(process.env.MONGO_URL, { promiseLibrary: Promise })
17 .then(build)
18}
51function connect(){
52 metalog.minor('cube_testdb', { state: 'connecting to db', options: options });
53 test_db.options = options;
54 test_db.client = new mongodb.Server(options["mongo-host"], options["mongo-port"], {auto_reconnect: true});
55 test_db.db = new mongodb.Db(options["mongo-database"], test_db.client, {});
56}
43private static async connect(): Promise {
44 const config = Config.get().database;
45 console.log(config.uri);
46
47 // eslint-disable-next-line no-constant-condition
48 while (true) {
49 try {
50 return await MongoClient.connect(config.uri, {
51 useNewUrlParser: true,
52 useUnifiedTopology: true,
53 });
54 break;
55 } catch (err) {
56 console.warn('Failed to connect to database, retrying in 10 seconds', err.message);
57 await new Promise(resolve => setTimeout(resolve, 10000));
58 }
59 }
60}

Related snippets