NodeJS + Mongo native – check if collection exists before query - node.js

I've got a function, trying to get a specific value from settings collection in MongoDB. The marker for settings object, containing settings values, in settings collection is {'settings':'settings'}. The schema is:
collection:setting
|--object
|--{'settings':'settings'}
|--{'valueA':'valueA'}
|--...
The problem is when I first time query settings object, the collection 'settings' simply does not exists. So,
exports.getInstruments = function (callback) {
db.collection("settings", function(error, settings) {
settings.find({ "settings" : "settings" }), (function(err, doc) {
callback(doc.instruments);
});
]);
}
just hangs and callback is not invoked. If collection does not exist, I should return "" or undefined, else - doc.instrumens.

There's an exists() function that you could use to determine whether or not to execute the code that hangs.
> db.getCollection('hello').exists()
null
> db.getCollection('world').exists()
{ "name" : "testdb.world" }

You could potentially take advantage of db.createCollection which explicitly creates a collection:
> db.createCollection("asd")
{ "ok" : 1 }
> db.createCollection("asd")
{ "errmsg" : "collection already exists", "ok" : 0 }
Just check if the command succeeded based on the ok field.

You shouldn't need to specially handle the new collection case, I think the problem is with your code.
Aside from some syntax problems, the main problem is that find passes a Cursor to your callback function, not the first matching document. If you're expecting just one doc, you should use findOne instead.
This should work:
exports.getInstruments = function (callback) {
db.collection("settings", function(error, settings) {
settings.findOne({ "settings" : "settings" }, function(err, doc) {
callback(doc && doc.instruments);
});
});
};

Related

Pass arguments in couchdb-nano view function

I am using nodejs's nano npm module and couchdb to get the total number of documents based on a status message that I should be able to pass in a couchdb view.
The view looks like the following.
{
_id: "_design/docCount",
views: {
fileCountByStatus: {
reduce: "_count",
map:
'function (doc) {\n if(doc.status === "COMPLETE") {\n emit(doc._id, 1);\n }\n}'
},
},
language: "javascript"
}
I am accessing the above view using nano's view function.
My question is, is there anyway I can pass the doc status using the view function instead of hardcoding it (COMPLETE in the above case)
Answering my own question.
The general case is to create an index on status with a map function like this:
function(doc) {
emit(doc.status, 1)
}
Then you can query the view for any value of status
db.view('mydesigndoc', 'myview', { key: 'complete' })
// or
db.view('mydesigndoc', 'myview', { key: 'new' })

findOneAndUpdate with Upsert always inserting a new user

I want to do is update a record and insert it if it doesn't exist with mongoose. Here is my code:
module.exports.upsertUser = function(user) {
var options = {userName : 'Ricardo'}
Users.findOneAndUpdate({email: user.email}, options, {upsert:true}).exec();
}
And:
var promise = Users.upsertUser(user);
promise
.then(function(results){
...
}
.catch(function(err){
...
}
When I execute the promise, each time a new user is created with the same email.
I'm not sure if I'm performing the update incorrectly. I've tried it in the same way but with update and it does not work either.
Can you help me? Thanks!
You need to put the return without the exec:
module.exports.upsertUser = function(user) {
var options = {userName : 'Ricardo'}
return Users.findOneAndUpdate({email: user.email}, options, {upsert:true, new: true});
}
var promise = Users.upsertUser(user);
promise.then(function(results){
if(results) {
console.log(results);
} else {
console.log('!results');
}
}.catch(function(err){
...
}
FYI:
The default is to return the unaltered document. If you want the new, updated document to be returned you have to pass an additional argument: {new:true} with the upsert option.
according to your coding what findOneAndUpdate() does is that it finds this document/user's document that you are looking for to edit which in this case what you are using to search for this user's document that you want to edit it's document is with his email. Now your second argument modifies that part of the document that you wanted to modify, but this option has to be attached with some mongodb operators (pay a visit to this link for a list of them: https://docs.mongodb.com/manual/reference/operator/update/) but in your case what you need is the $set operator.so i would modify your code like this:
module.exports.upsertUser = function(user) {
var options =
Users.findOneAndUpdate({email: user.email}, {$set:{
userName : 'Ricardo'
}}, {
returnOriginal: true}).exec();
};
so try the above modification let's see how it works

Mongoose - Modle.update() updates wrong document - Cast Error

I need some help to clear some things up.
I have a Model:
var Event = new Schema({
event_code: String
, segments: [Segment]
});
The creation of new documents work very well like perfect. When it comes to update certain documents I ran into some troubles.
When I do this (code below): = it only updates the first document, even if the id does not match
function edit_event (id, new_name, callback) {
Event.update(id, {$set:{event_code: new_name}}, function(err, doc) {
if (err) throw err;
callback();
});
}
When I do this (code below): = it gives me an Error (see below)
function edit_event (id, new_name, callback) {
Event.findByIdAndUpdate(id, {$set:{event_code: new_name}}, function(err, doc) {
if (err) throw err;
callback();
});
}
Error when using findByIdAndUpdate: Cast to ObjectId failed for value ""58fdbde31bff83141b376508"" at path "_id" for model "Event"
Please, i'm desperate :! :/
UPDATE
I figured out that the id that i'm trying to pass get stored with "" around it, so when i am looking for document with matching ID it puts an extra pair of "" around it so it ends up like ""id""
UPDATE 2
When I am listing all my documents, it returns:
{ _id: 58fdbde31bff83141b376508,
event_code: 'TestABC',
__v: 0,
segments: [] }
Then when i store the id in an HTML form it adds extra pair of "" around it ... that's not the case with event_code. Why is that ?
Looks like you performed unneeded JSON.stringify and that's why you get quotes around the id. Calling JSON.parse should solve the issue:
Event.findByIdAndUpdate(JSON.parse(id), {$set:{event_code: new_name}}, ...

Why doesn't jasmine-node mongoose test wait, as expected?

I am writing a simple app that saves and looks up locations. I'm using mongoose and jasmine-node.
User CRUD test work as expected. However, I created the users individually to test different custom validations. I also start the tests by clearing the collection and re-loading all the users as a way to be sure all test data is good before launching into save/update/etc tests.
For locations, I am doing the same but I have several dozen locations and I wanted to load them using an array...and test the load along the way to be sure that it works fine.
If I only do one location, it works fine. More than one and they fail.
I know I'm missing some async related step here, but I'm either searching for the wrong terms or I'm too close to it now to see the fundamentally simple mistake I'm making here.
Versions:
mongoose: 3.6.16
jasmine-node: 1.11.0
mongodb: 2.4.5
Details
The test...
it("creating location succeeds", function(done){
for(var locIndex in testLocations) {
locations.create(testLocations[locIndex], function(err, location){
// console.log says location is undefined
// unless there is only one location, then it works.
expect(err ).toBeNull();
expect(location.name ).toBe(testLocations[locIndex].name);
done();
});
}
});
...and the create function from a separate file holding location related functions...
exports.create = function(location, cb){
var newLocation = new Location(location);
// console.log says we make it into here...
newLocation.save(function(err, newLocation){
// ...but we never make it in here, except when there's only one location
if (err) {
cb(err, null);
} else {
cb(null, newLocation);
}
});
};
...and some test locations...
var testLocations = [
{
"name" : "A Great Noodle Place",
"street" : "1234 Elm Street",
"city" : "Springfield",
"phone" : "(123) 456-7890",
"website" : "n00dlesrus.com",
"district" : "Downtown"
},
{
"name" : "Perfect Pizza Palace",
"street" : "1234 Professor Ave",
"city" : "Springfield"
"phone" : "(321) 654-0987",
"website" : "cheesegalore.com",
"district" : "Uptown"
}
]
Thanks!
You're calling done() inside a loop. So it gets called in the first iteration. That's why it works when there's only 1. You could try using async, which will iterate over a list and call a final callback when finished:
it("creating location succeeds", function(done){
async.each(Object.keys(testLocation), function(key, callback){
locations.create(testLocations[key], function(err, location){
expect(err).toBeNull();
expect(location.name).toBe(testLocations[key].name);
callback();
});
}, function(err) {
done();
});
});

MongoDB Upsert add to array

I'm trying to insert/update an array of strings in a mongodb document using some typescript code running in NodeJS.
The following code is typescript but I guess JS developers will get it w/o any problems:
export function addEvents(entityId: string,
events: string[] ,
callback: () => void) {
db.collection('events', function(error, eventCollection) {
if(error) {
console.error(error); return;
}
eventCollection.update({ _id: entityId }, { "$pushAll ":
{ events: events }},
function(error, result) {
if(error) {
console.error(error); return;
}
callback();
});
});
}
the document have the following structure:
{
_id : string
events : ["array","of","strings"]
}
I simply want to append an array strings at the end of the existing array for a specific _id.
I don't quite get if I should use update,save, $push ,$pushall etc.
Can someone explain?
If I understood correctly the problem is that pushAll does nothing or update returns error? Maybe copy-paste mistake in your example but I think you have typo here.
{ "$pushAll ": { events: events }}
It should be
{ $pushAll: { events: events }}
Your combination of update and $pushAll looks like the best choice for what you're doing here -- it's for appending an array to an existing array. $push is for adding an element to an array. save would involve getting the existing events array, appending to it, then saving the document.
The extra space in "$pushAll " needs to be removed. It may have quotes: "$pushAll".
Found the problem, I needed to pass "{ upsert = true }" as a third argument to the update function.
To achieve 'upsert' semantics in this case, you'd need to use $addToSet. If you have an array of values to add, you'd need to throw in the $each modifier. From mongo shell:
db.events.update(
{ _id: entityId },
{ $addToSet: { $each: events } }
)

Resources