node-mongodb-native

mongodb nodejs each vs toArray

和自甴很熟 提交于 2019-12-03 17:24:44
I've had a quick look around and not found anything that's satisfied me with an answer but basically I've started to use node.js with express and mongodb to create a webapi rather than the usual .Net MVC Web API route. One thing I've noted though is in order to return a collection of results I'm doing it in a rather bulky way, or that's how it feels at least. app.get('/property', function (req, res) { var propArray = []; MongoClient.connect(settings.connection, function (err, db) { if (err) throw err; var properties = db.collection("PROPERTIES"); var searchParams = { Active: true, Deleted:

Changing mongo database

烈酒焚心 提交于 2019-12-03 16:43:24
I want to query a collection in my replica set using the native 2.0 mongodb driver for node. I can connect and authenticated against the admin database but how do I switch databases to query the collection I'm interested in? var mongodb = require('mongodb'); var MongoClient = mongodb.MongoClient; var url = "mongodb://user:pass@db1,db2,db3/admin"; MongoClient.connect(url, function(err, db) { console.log("Connected correctly to server"); console.log("Current database", db.databaseName); // switch context to database foo // foo.bar.findOne(); db.close(); }); From MongoDB 2.0.0 Driver docs

What aggregation cursor methods are supported by Nodejs drivers?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 16:31:04
As you know from 2.6 on Mongodb aggregate() operation returns a cursor, however the behavior is a bit different than the normal cursor which returns from a find() . I am using native mongodb nodejs driver, and could not find a proper documentation on available aggregate cursor methods. For example, one cannot run a count() on an aggregation cursor however there are two methods such cursor.objsLeftInBatch() and cursor.itcount() n mongo shell. I could not find any of them in the source code of nodejs native driver. What aggregation cursor methods are supported by Nodejs native driver or Mongoose

Check if document exists in mongodb using es7 async/await

时光毁灭记忆、已成空白 提交于 2019-12-03 09:17:17
问题 I'm trying to check if the user with the email provided exists in the collection users , but my function keeps returning undefined for every call. I use es6 and async/await in order to get rid of lots of callbacks. Here's my function (it is inside of a class): async userExistsInDB(email) { let userExists; await MongoClient.connect('mongodb://127.0.0.1:27017/notificator', (err, db) => { if (err) throw err; let collection = db.collection('users'); userExists = collection.find({email: email})

Check if document exists in mongodb using es7 async/await

浪子不回头ぞ 提交于 2019-12-02 23:29:21
I'm trying to check if the user with the email provided exists in the collection users , but my function keeps returning undefined for every call. I use es6 and async/await in order to get rid of lots of callbacks. Here's my function (it is inside of a class): async userExistsInDB(email) { let userExists; await MongoClient.connect('mongodb://127.0.0.1:27017/notificator', (err, db) => { if (err) throw err; let collection = db.collection('users'); userExists = collection.find({email: email}).count() > 0; console.log(userExists); db.close(); }); console.log(userExists); return userExists; } So,

Multiple node-mongodb-native connections

a 夏天 提交于 2019-12-02 03:18:06
When I run this Node.js code: var mongodb = require('mongodb'), MongoClient = mongodb.MongoClient; MongoClient.connect('mongodb://localhost:27017/mydb', function(error, db) { if (error) { throw (error); } console.log('Connected!'); }); The mongo logs show 5 connections open: sudo mongod mongod --help for help and startup options 2014-11-04T21:03:23.107-0700 [initandlisten] MongoDB starting : pid=27572 port=27017 dbpath=/data/db 64-bit host=mylaptop 2014-11-04T21:03:23.107-0700 [initandlisten] db version v2.6.2 2014-11-04T21:03:23.107-0700 [initandlisten] git version: nogitversion 2014-11-04T21

Express.js - Filter a mongodb id in the URL

早过忘川 提交于 2019-12-02 00:53:23
问题 This question inspired by this post but in my case I need to filter MongoId. Is it possible to make filtering easily that the below because I need use it in each route? app.post('/:mongoId(^[0-9a-fA-F]{24}$)', function(req, res){ // Send query based on mongoId } 回答1: You're almost there, just don't add the ^ and $ anchors. And the uppercase A-F range isn't even necessary since Express seems to match case-insensitive: app.post('/:mongoId([0-9a-f]{24})', function(req, res){ var id = req.param(

Node.js, MongoDB - Inserting/updating multiple documents and sending a single response

狂风中的少年 提交于 2019-12-01 23:17:10
问题 I'm trying to develop a synchronization server (think: SVN like) that accepts one or more documents (JSON string) from the client in one request (JSON stringified array of JS objects), inserts/updates them into mongodb and sends one response - which is a JSON string containing the insert/update status (and some more info like _id in mongo) of each document. If it was one document, i could have done one insert/update and in its callback, i could have sent the response. collection.insert(_data,

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

浪尽此生 提交于 2019-12-01 22:23:13
问题 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

Express.js - Filter a mongodb id in the URL

℡╲_俬逩灬. 提交于 2019-12-01 21:29:35
This question inspired by this post but in my case I need to filter MongoId. Is it possible to make filtering easily that the below because I need use it in each route? app.post('/:mongoId(^[0-9a-fA-F]{24}$)', function(req, res){ // Send query based on mongoId } You're almost there, just don't add the ^ and $ anchors. And the uppercase A-F range isn't even necessary since Express seems to match case-insensitive: app.post('/:mongoId([0-9a-f]{24})', function(req, res){ var id = req.param('mongoId'); ... }); According to the Express API documentation , yes, you can use a regular expression as a