node-mongodb-native

MongoDB NodeJS Native Driver(mongodb) vs Mongo Shell Performance

一世执手 提交于 2021-01-28 11:49:31
问题 I have 10000 records in MongoDB in table1. Data is as below: "_id" : ObjectId("5d5e500cb89312272cfe51fc"), "cities" : [ { "cityid" : "5d5d2205cdd42d1cf0a92b33", "value" : "XYZ" }, { "cityid" : "5d5d2214cdd42d1cf0a92b34", "value" : "Rowcliffe" }, ], Query is as below: { $unwind: "$cities" }, { "$addFields": { "cities.cityid": { "$toObjectId": "$cities.cityid" } } }, { $lookup: { from: "cities", localField: "cities.cityid", foreignField: "_id", as: "docs" } }, So, here i lookup cityid in

MongoError: there are no users authenticated

↘锁芯ラ 提交于 2020-06-25 09:38:06
问题 I'm trying to write a script to add an admin user and a generic user to the MongoDB database using mongodb NodeJS driver - version 3.0.1 I'm able to create the admin user, but not general user for a database. I'm always getting MongoError: there are no users authenticated . Going through the documentation, the only way to authenticate a user is via URL. I've deleted the database completely from the specified path, tried multiple times, still, I'm stuck. Here is what I've got so far, const

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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-11 06:31:27
问题 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

node-mongodb-native: How can I share the db api object of the connection callback through my application

混江龙づ霸主 提交于 2020-01-02 09:11:47
问题 I am currently rolling back from mongoose to node-mongodb-native. So I am quite new at this topic. However my issue currently is that want to create a database collection on server start which I then can use through the application. Unfortunately I only find examples in the repository where you only can do database actions directly in the callback of the connect function. docs: var mongodb = require("mongodb"), mongoServer = new mongodb.Server('localhost', 27017), dbConnector = new mongodb.Db

node-mongodb-native: How can I share the db api object of the connection callback through my application

匆匆过客 提交于 2020-01-02 09:11:30
问题 I am currently rolling back from mongoose to node-mongodb-native. So I am quite new at this topic. However my issue currently is that want to create a database collection on server start which I then can use through the application. Unfortunately I only find examples in the repository where you only can do database actions directly in the callback of the connect function. docs: var mongodb = require("mongodb"), mongoServer = new mongodb.Server('localhost', 27017), dbConnector = new mongodb.Db

What does an example MongoDB error look like on the NodeJS native driver?

大城市里の小女人 提交于 2020-01-02 04:02:13
问题 I can't seem to find any examples of MongoDB error objects in their documentation or on the internet. What does an example MongoDB error object look like? I'd like to "handle" the error and/or reformat it for my own purposes, depending on what the error is. 回答1: As of MongoDB 2.4.8 with the mongodb 1.3.23 driver, they look like this: { "name":"MongoError", "err":"E11000 duplicate key error index: test.test.$country_1 dup key: { : \"XYZ\" }", "code":11000, "n":0, "connectionId":10706, "ok":1 }

ensureIndex causes error

牧云@^-^@ 提交于 2019-12-25 07:39:47
问题 This is my code: this._db = db; this._collection = this._db.collection("Topics"); this._collection.ensureIndex( { slug: 1 }, { unique: true }, function(error) { if (error) { console.error(error); }}); When I use an insert: self._collection.insert(topic, function (err, docs) { if (err) { topic.slug = shortId.generate() + "-" + topic.slug; self._collection.insert(topic, function (err, docs) { return done(err, docs); }); } return done(err, docs); }); I get this error inside the "docs" response,

Why my NodeJS program opens multiple connections to Mongo (using native driver)

无人久伴 提交于 2019-12-24 03:26:05
问题 From my NodeJS program I connect to Mongodb with native driver. I start mongod and see server waiting for connections. When my program connects I can see however 5 connections instead of one. I don't understand why is happening as I do nothing unusual it seems. Mesage from mongod is below: 2015-05-02T15:35:17.635+1000 [initandlisten] waiting for connections on port 27017 2015-05-02T15:36:17.638+1000 [clientcursormon] mem (MB) res:51 virt:508 2015-05-02T15:36:17.639+1000 [clientcursormon]

Cannot loop MongoDB Collection Functions

邮差的信 提交于 2019-12-23 19:20:10
问题 For some reason, I cannot loop collection.count . It keeps printing filename[5] 5 times instead of starting at 1 and going to 5. This makes no sense because I can manually copy and paste this function 5 times and it will work, but when in a for loop, it doesn't. for( var k =0; k<(filename.length)-2;k++) { collection.count( { "display.Name": filename[k] } , function(err, count) { console.log("Filename: " +filename[k]); console.log(" Trips: " + count); }); } NOTE: If I put the console.log

$geoNear aggregation ignoring maxDistance

旧巷老猫 提交于 2019-12-23 05:00:09
问题 The following query should return cities within a distance of lng and lat. The city collection has an 2dsphere index on its gps field. City.native(function(err, collection) { collection.aggregate([{ $geoNear: { near: { type: 'Point', coordinates: [lng, lat] }, distanceField: 'dist', limit: limit, maxDistance: distance, distanceMultiplier: 1, spherical: true } }], function(err, result) { return res.send(result); } }); }); My problem is that when I set distance to 0 the query still returns