node-mongodb-native

Node Mongo Native - how to tell when a cursor is exhausted?

梦想与她 提交于 2019-12-12 09:42:28
问题 The documentation for the node-mongo-native collection.find() function says that it creates a cursor object which lazily returns the matching documents. Furthermore: The basic operation on a cursor is the nextObject method that fetches the next matching document from the database. The convenience methods each and toArray call nextObject until the cursor is exhausted. Unfortunately, the documentation provides no indication of how to tell when the cursor is actually exhausted. You could use the

How to connect with username/password to mongodb using native node.js driver

我的未来我决定 提交于 2019-12-12 08:09:00
问题 I'm using native mongo driver in Joyent cloud, the node.js application runs fine locally but in Joyent when i run with usrname/pswd that they provided it fails to connect. following is the code used to connect: var db = new MongoDB(dbName, new Server('localhost', 27017 , {auto_reconnect: true}), {w: 1}); db.open(function(e, db){ if (e) { console.log(e); } else{ console.log('connected to database :: ' + dbName); //db.admin().authenticate('admin', '+(uihghjk', function(de , db){ // if(e){ //

node-mongodb-native manually aborting running queries

夙愿已清 提交于 2019-12-12 02:05:05
问题 I'm wondering what the most efficient way to manually abort a running query with the official node client is. In my current code I am creating a new DB connection for each remote client and then simply closing those connections if the client disconnects early, which works well but seems like it might be a wasteful way of doing things (edit: indeed it is; detailed here) . I imagine if it's possible to directly access the mongodb client connection pool and close specific connections rather than

MongoDB filter by array property only if it exists

时间秒杀一切 提交于 2019-12-11 18:15:12
问题 My database object has an optional property named tags which is a string array. I want to write a query that returns objects if they match one of these conditions: They don't have a tags property. They have a tags property that has at least one item included in another array called queryTags From reading the documentation I came up with the following but it doesn't work: let query = { tags: { '$or': [{'$exists': false}, {'$in': queryTags}]} } 回答1: $or is a top-level operator, so your query

auto increment in node-mongodb-native using counters collection

我的梦境 提交于 2019-12-11 12:08:56
问题 Is there any way of implementing a counters collection as described in the docs with node-mongodb-native? I'm trying to avoid doing this by nesting over 9000 callbacks (which IMHO sounds untidy and not-elegant) but the good people who wrote this native driver refused to implement synchronous/blocking calls. Does the native driver provide a someway to call user defined functions and use their return values during queries? Or could there be an alternate way of extracting a sequential count...

How to keep date from client saved in MongoDB as date?

随声附和 提交于 2019-12-11 04:12:29
问题 I'm using Node.js and Angular.js for a web project. I understand that date is saved as date if it's created on server using new Date() (e.g. 2015-04-08 04:15:18.712Z shown in Robomongo as Date type). However, if the date is created on client using new Date(), it is then saved as a string (e.g. 2015-04-07T04:58:12.771Z shown in Robomongo as String type), because it becomes a string through node API. How to make it save as a Date instead of String? UPDATE: This is what I got based on Jason Cust

How to check node-mongodb-native driver version?

☆樱花仙子☆ 提交于 2019-12-11 03:43:43
问题 I am using official node-mongodb-native driver in my project. Which was installed by doing npm install mongodb. However I want to check its version but not sure how to do so. There is nothing in the READ.ME file of that npm repo. Please help. Thanks. 回答1: According to the npm documentation you can get a list of npm packages and their corresponding versions using npm ls or npm ls -g (for global) 回答2: The version is exported in index.js: exports.version = require('../../package').version; So

How do I enable profiling in node-mongodb-native?

廉价感情. 提交于 2019-12-11 03:24:26
问题 I want to enable profiling on one of my MongoDB databases, via the node-mongodb-native driver. However there doesn't seem to be a Db.setProfilingLevel() method (apart from on the Admin DB). I've tried using db.command({setProfilingLevel: 2}) but get no such cmd: setProfilingLevel . Works fine through the mongo shell with db.setProfilingLevel(2) 回答1: I see what you mean about the methods, but I think the issue with the db.command attempt is that you are trying to run a shell helper as a

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

六月ゝ 毕业季﹏ 提交于 2019-12-09 19:14:11
问题 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 = require('path') , http = require('http') , mongo = require('mongodb'); // Configure Express app var app = express(); app.configure(function () { app.set('port', process.env.PORT || 3000); app.use(express.logger('dev')); /* 'default', 'short', 'tiny',

Node mongodb: Error: connection closed due to parseError

隐身守侯 提交于 2019-12-09 05:31:25
问题 Using the native 'mongodb' npm package, I'm receiving Error: connection closed due to parseError When making a very basic query: collections.myCollection.findOne({id: someID}, function (err, repo) { ... }) The weird thing is, the exact same query has run before. Types are identical for each query, etc. 回答1: Answering my own question to hopefully stop the next person from tearing their hair out: As noted here, this error message is useless and doesn't relate to the actual problem. The