Menu
  • HOME
  • TAGS

How do I pass a variable into mongodb callback?

node.js,node-mongodb-native

In our case better use waterfall instead of series, and map, like this function getSettings(target){ async.waterfall([ function (callback) { db.get(target).distinct('version', function (err, versions) { callback(err, versions); }); }, function (versions, callback) { async.map(versions, function (version, next) { db.get(target).distinct('instance', {version: version}, function (err, items) { var result = {}; result[version] =...

How to save a modified object in mongodb using Node.JS Driver

node.js,mongodb,node-mongodb-native

collection.update({'facebook_id':req.params.facebook_id}, {$push: { messages: {'value': message.value, 'date': message.date} } }, function(err) { }); Use the $push operator to add a value to an array directly in the database. http://docs.mongodb.org/manual/reference/operator/update/push/ Note that this is much more efficient than updating the entire object, especially for large objects....

Distinct values from various fields in MongoDB collection

node.js,mongodb,node-mongodb-native

You could aggregate it on the database server as below: Group Individual document, to get the values of each intended field in an array. Project a field named values as the union of all the intended field values, using the $setUnion operator. Unwind values. Group all the records, to get...

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

node.js,mongodb,node-mongodb-native

From the docs https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html Connection pool configuration: maxPoolSize=n: The maximum number of connections in the connection pool Default value is 5 ...

Cannot infer query fields to set error on insert

javascript,mongodb,mongodb-query,node-mongodb-native

It's not the "prettiest" way to handle this, but current restrictions on the selection operators mean you would need to use a JavaScript expression with $where. Substituting your vars for values for ease of example: matches.findAndModify( { "$where": function() { var match = [1,2]; return this.users.filter(function(el) { return match.indexOf(el) !=...

Access and modify extern variable in MongoDB request

node.js,node-mongodb-native

I have no idea what you mean by "affectation fail", but it looks like you're calling res.render too early — callbacks are invoked asynchronously after your current context finishes executing, so when you call res.render(...) after your for loop has finished, your Post.find(...)... operations still haven't finished and their callbacks...

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

node.js,mongoose,node-mongodb-native,gridfs-stream,multer

In my code: var fileId = new ObjectId(); console.log("Creating WriteStream"); var writeStream = gfs.createWriteStream({ _id: fileId, filename: file.originalname, mode: 'w', content_type: file.mimetype, metadata: { id: '123', number: '2', name: "Kenny Erasmuson" } }); I am assigning an ObjectId rather than the string representation of an ObjectId to the _id property...

Changing mongo database

node.js,mongodb,node-mongodb-native

From MongoDB 2.0.0 Driver docs Indirectly Against Another Database In some cases you might have to authenticate against another database than the one you intend to connect to. This is referred to as delegated authentication. Say you wish to connect to the foo database but the user is defined in...

Can't use $text with String

node.js,mongodb,mongoose,node-mongodb-native

Looking at the docs you cannot specify a field for the text search, it will search across all the indexed fields, so in your case it will search over fullAddress and shortAddress returning documents that match in either of these fields. Your query would need to be: self.staticVars.Model .find({$text :...

MongoDB retrieval error

node-mongodb-native

You could just about place money on entry_id is an ObjectId value and what you are passing as a variable is actually just a string. But the other obvious thing is that you are using .find() in the wrong way. The "result" returned is a "cursor". If you want something...

MongoDB native: is there any difference between toString and toHexString methods?

javascript,node.js,mongodb,node-mongodb-native,objectid

toHexString method returns the ObjectID id as a 24 byte hex string representation // Create a new ObjectID var objectId = new ObjectID(); // Verify that the hex string is 24 characters long assert.equal(24, objectId.toHexString().length); you won't need to base64 encode the result of calling toString on an ObjectId as...

Create user, callback

node.js,callback,node-mongodb-native

First of all: do not use sync methods if possible, use callbacks instead. var bcrypt = require('bcrypt-nodejs'); var users = db.collection("users"); this.addUser = function(username, password, email, callback) { "use strict"; // Generate password hash bcrypt.genSalt(function(err, salt) { if (err) { callback(err); return; } bcrypt.hash(password, salt, function(err, password_hash) { if (err)...

MongoDB Bulk Update is slow

javascript,mongodb,bulkinsert,node-mongodb-native

Based on your comment, your find query is not using an index which will prompt a full collection scan. Add an index to your collection that can be used by find(query); use explain() to confirm it's being used....

Can't get MongoDB update query to work in Node.JS

javascript,node.js,mongodb,node-mongodb-native

Your code is all over the place. This should be easily handled with a simple findOneAndUpdate, optionally with upsert if you want to create the player if they don't exist. var MongoClient = require('mongodb').MongoClient; function insertEntry(sPlayerName, sPlayerID, sPlayerScore, sPlayerHealth) { MongoClient.connect('mongodb://127.0.0.1:27017/mdata', function(err, db) { if(err) { throw err; } var...

Bad BSON Document: illegal CString with Node MongoDB driver

node.js,mongodb,node-mongodb-native

Thanks to @wdberkeley for all the help in the above comment, which helped me to track down my problem. It turns out that I did have a single corrupted document in my collection, which was inserted during an unclean shutdown of Mongo. I was unaware how that document would affect...

MongoDB not updating subdocument within double-nested array (using Mongoose FindByIdAndUpdate) - EXACT POSITION KNOWN

node.js,mongodb,mongoose,mongodb-query,node-mongodb-native

I have figured out the issue. Apparently when using the Mixed Schema type, (which is the same as {}) with Mongoose, updating a subfield within the object is a 2 step process. You can't just use findByIdAndUpdate(). You must first use fineById(), grab the document in the callback, run markModified()...

Using Dynamic strings for querying nested mongo doc

mongodb-query,node-mongodb-native

Not tested it, just thoughts. Did you tried this code: var prop = "Car.Make." + "Model" //or ""Year" var query = {}; query[prop] = "1989"; db.carcoll.find(query) ...