Struggling with updating data to MongoDB using Express. Despite receiving a status 200 response from my API, I can't seem to update the values in Mongoose when using Mongoskin.
var mongo = require('mongoskin');
var db = mongo.db(config.connectionString, {
native_parser: true
});
var ObjectId = require('mongodb').ObjectID;
db.bind('users');
db.bind('dashboard')
function update(_id, userParam) {
var deferred = Q.defer();
for (var i = 0; i < Object.keys(userParam).length; i++) {
var set = {
file: userParam[i].file,
result: userParam[i].result,
user_id: userParam[i].user_id
};
db.dashboard.update({
_id: mongo.helper.toObjectID(_id)
}, {
$set: set
},
function(err, doc) {
if (err) deferred.reject(err);
deferred.resolve();
});
};
return deferred.promise;
}
This is how my API looks:
{
"0": {
"_id": "57396e49a6c36801024021a1",
"file": "app_static/audio/SampleAudio_0.4mb.mp3",
"result": "FM",
"user_id": "57396ded0aef5ee405320dbe"
},
"1": {
"_id": "57396e5ca6c36801024021a2",
"file": "app_static/audio/SampleAudio_0.7mb.mp3",
"user_id": "57396ded0aef5ee405320dbe"
}
}
I'm utilizing a for loop to ensure the entire API is processed and pushed to MongoDB.
If you have any suggestions for an alternative way to push an entire JSON to MongoDB, please share them.
Your assistance is greatly appreciated!
Thank you in advance.