node-mongodb-native

Alternatives to MongoDB cursor.toArray() in node.js

馋奶兔 提交于 2019-12-01 18:53:02
I am currently using MongoDB cursor's toArray() function to convert the database results into an array: run = true; count = 0; var start = process.hrtime(); db.collection.find({}, {limit: 2000}).toArray(function(err, docs){ var diff = process.hrtime(start); run = false; socket.emit('result', { result: docs, time: diff[0] * 1000 + diff[1] / 1000000, ticks: count }); if(err) console.log(err); }); This operation takes about 7ms on my computer. If I remove the .toArray() function then the operation takes about 0.15ms. Of course this won't work because I need to forward the data, but I'm wondering

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

本小妞迷上赌 提交于 2019-12-01 08:08:07
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() { console.log("Server listening on port " + app.get('port')); }); }); This is all done to have only one db instance across the application. Now, If we are in another external.js file and need a same db object which is aleady has connected. This can be done very easily if we are using mongoskin or mongoose Can someone help me to find how this can be done with native

MongoError when uploading a file using mongoose, gridfs-stream and multer

不问归期 提交于 2019-11-30 10:22:27
I am running express 4 using multer , gridfs-stream and mongoose with mongodb and I am attempting to upload a file and stream it to gridfs. The express route that does this is defined as: app.post('/uploadfile', function (req, res) { console.dir(req.files); // The mongodb instance created when the mongoose.connection is opened var db = mongoose.connection.db; // The native mongo driver which is used by mongoose var mongoDriver = mongoose.mongo; // Create a gridfs-stream var gfs = new Gridfs(db, mongoDriver); var file = req.files.myFile; var fileId = new ObjectId(); console.log("Creating

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

旧城冷巷雨未停 提交于 2019-11-30 07:53:39
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 working properly before connecting my actual app. Here is my simple app: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var timeSchema = new Schema({ timestamp: String }); var Time = mongoose.model('Time', timeSchema); mongoose.connect('mongodb://localhost/mydb'); var db = mongoose.connection; db.on('error', console.error.bind

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

a 夏天 提交于 2019-11-30 05:02:55
问题 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 create a new connection.) var express = require('express'); var app = express(); var MongoClient = require('mongodb').MongoClient; var assert = require('assert'); // Connection URL var url = '[connectionString]]'; // start server on port 3000 app

MongoError when uploading a file using mongoose, gridfs-stream and multer

可紊 提交于 2019-11-29 15:35:08
问题 I am running express 4 using multer, gridfs-stream and mongoose with mongodb and I am attempting to upload a file and stream it to gridfs. The express route that does this is defined as: app.post('/uploadfile', function (req, res) { console.dir(req.files); // The mongodb instance created when the mongoose.connection is opened var db = mongoose.connection.db; // The native mongo driver which is used by mongoose var mongoDriver = mongoose.mongo; // Create a gridfs-stream var gfs = new Gridfs(db

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

守給你的承諾、 提交于 2019-11-29 10:41:28
问题 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 working properly before connecting my actual app. Here is my simple app: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var timeSchema = new Schema({ timestamp: String }); var Time = mongoose.model('Time', timeSchema); mongoose

Handling MongoDB disconnect/reconnects from Node

…衆ロ難τιáo~ 提交于 2019-11-29 00:30:17
When my MongoDB connection is idle for a few minutes, the next request ends in error. From the mongo command line client, it looks like this: > db.users.find() Sat Jan 12 23:42:35 Socket recv() errno:54 Connection reset by peer 107.22.25.25:47207 Sat Jan 12 23:42:35 SocketException: remote: 107.22.25.25:47207 error: 9001 socket exception [1] server [107.22.25.25:47207] Sat Jan 12 23:42:35 DBClientCursor::init call() failed Sat Jan 12 23:42:35 query failed : chowology.users {} to: ds047207.mongolab.com:47207 Error: error doing query: failed Sat Jan 12 23:42:35 trying reconnect to ds047207

Bluebird Promisfy.each, with for-loops and if-statements?

此生再无相见时 提交于 2019-11-28 23:36:54
Right now, the parent for-loop ( m < repliesIDsArray.length ) completes before the first findOne fires, so this all only loops through the last element of the repliesIDsArray..asynchronous.. What's the proper syntax for a promisified version of this codeset? Am new to promisification, and wondering how to start this promisify + loop through arrays + account for if-statements.. Bluebird is required, and Promise.promisifyAll(require("mongoose")); is called. for(var m=0; m<repliesIDsArray.length; m++){ objectID = repliesIDsArray[m]; Models.Message.findOne({ "_id": req.params.message_id}, function

MongoDB cannot use the part to traverse element

心不动则不痛 提交于 2019-11-28 11:09:27
问题 I have in my db a Project's schema like this : Project: { _id: ObjectID(), // some data dashboard_group: [ 0: { _id: ObjectID(), dgr_nom: String, dgr_enable }, // others dashboards ] } For every project, it can have only one dashboard enable. So, before I add a new dashboard (enabled) in the 'dashboard_group' array, I have to set all the others dashboards (dgr_enable) on false. To not have to retrieve the index of the enabled dashboard, I use the identifier $[] to set all dashboards to false.