Mongoose findOneAndUpdate is not accepting switch case values - node.js

I was trying to create a single function to update different types of options in Mongoose model and encountered this strange behavior.
Here's what I was trying to do.
module.exports.updateUser = function(id, action, status, callback) {
const query = {
_id: id
};
let field = '';
switch (action) {
case 'download':
field = 'download_permission';
break;
case 'upload':
field = 'upload_permission';
break;
case 'view':
field = 'view_permission';
break;
case 'edit':
field = 'edit_permission';
break;
}
User.findOneAndUpdate(query, {
$set: {
field: status,
last_updated: moment().format('llll')
}
}, callback);
};
Now, if I try something like :
User.findOneAndUpdate(query, {
$set: {
'edit_permission': status,
last_updated: moment().format('llll')
}
}, callback);
};
It actually updates the document in the mongodb.
Can someone explain me why second works and not the first (doesn't update the document). Thanks.

In ES6, you can define computed keys using bracket notation.
User.findOneAndUpdate(query, {
$set: {
[field]: status,
last_updated: moment().format('llll')
}
}, callback);

User.findOneAndUpdate(query, {
$set: {
problem ------->>>>field<<<<<: status,
last_updated: moment().format('llll')
}
}, callback);
The field object param will not resolve to the value of your variable field.
This should fix it:
const setStatement = {
last_updated: moment().format('llll')
};
setStatement[field] = status;
User.findOneAndUpdate(query, {
$set: setStatement
}, callback);

I dont think you can have a variable as a key... If you put it that way... the variable itself becomes a string and becomes the key... the value of the variable wont appear in key's place

Related

MongoDB find an object in DB and update its field

Problem
I have a NodeJS app connecting to a MongoDB. I am tracking how many times something occurred. So, what I want is:
Check if my constructed object is in the database (excluding field with number of occurrences)
If so, update its occurrences +=1
If not, set occurrences = 1 and insert it
I have a working code:
const isInDb = await collection.findOne({
// match all other fields except for the occurrences field
});
if(!isInDb) {
parsedElement.occurrences = 1;
await collection.insertOne(parsedElement);
} else {
await collection.updateOne(isInDb, { $inc: { "occurrences": 1 } });
}
My question
Isn't there a better way? Ideally, it'd be something like collection.findAndUpdate or with upsert or something similar. What I wrote is functional, but seems inefficient to me, since I first have to query the DB for a look-up, and then query it for update.
updateOne takes a third parameter for options. Set upsert: true.
collection.updateOne({ /* match properties */ }, { $inc: { "occurrences": 1 } }, { upsert: true })
collection.updateOne({ /* match properties */ }, {
$set: parsedElement,
$inc: {
"occurrences": 1
}
}, {
upsert: true
})

Nodejs and MongoDB : Unable to return value from a function

var config = require('config.json');
var mongo = require('mongoskin');
var db = mongo.db(config.connectionString, { native_parser: true });
module.exports.getNextSequence = function (name) {
var temp;
db.collection("counters").findAndModify(
{ _id: name }, // query
[], // represents a sort order if multiple matches
{ $inc: { seq: 1 } }, // update statement
{ new: true }, // options - new to return the modified document
function (err, doc) {
temp = doc.value.seq;
console.log(temp); // <-- here the temp is getting printed correctly
}
);
return temp;
}
Using the above code, I am not able to return the value of doc.value.seq. When doing console.log(obj.getNextSequence) it prints undefined.
I want the function to return the value of doc.value.seq.
I'm not familiar with mongoskin so I'm not positive this is correct, but a database query is typically asynchronous, so you need to access the queried value via a callback.
I'm guessing your "getNextSequence" function is returning the "temp" variable before the database query completes (i.e. before the "temp = doc.value.seq" statement).
Try something like this:
module.exports.getNextSequence = function (name, callback) {
var temp;
db.collection("counters").findAndModify(
{ _id: name }, // query
[], // represents a sort order if multiple matches
{ $inc: { seq: 1 } }, // update statement
{ new: true }, // options - new to return the modified document
function (err, doc) {
temp = doc.value.seq;
callback(temp);
}
);
}
Then access "temp" from within the callback passed to getNextSequence.
findAndModify is an asynchronous function. Your console.log line will run after you return temp, which will therefore be undefined. In order to get this to work, you'll want to use an asynchronous approach of your own. There are two available approaches in your situation.
Callbacks:
You're already using a callback, which you provide as the final argument to findAndModify. You could extend this approach and feed this into a callback of your own, as follows:
module.exports.getNextSequence = function (name, callback) {
db.collection("counters").findAndModify(
{ _id: name },
[],
{ $inc: { seq: 1 } },
{ new: true },
function (err, doc) {
if (err) {
return callback(err);
}
callback(null, doc.value.seq);
}
);
}
Of course, this will require you to pass a callback into getNextSequence and follow the callback pattern upstream. You might also want to handle the error from mongoskin and do some handling of your own.
Promises:
If you don't provide a callback to findAndModify, it will return a promise, which you can chain on to, as follows:
module.exports.getNextSequence = function (name) {
return db.collection("counters").findAndModify(
{ _id: name },
[],
{ $inc: { seq: 1 } },
{ new: true }
).then(function (doc) {
return doc.value.seq;
});
}
Again, this will require you to follow the promise pattern upstream. You'll want to read up on promises if you choose this approach, so that you can correctly handle errors, which I have not addressed in the example above.

how to let updatemany validate mongoose schema on every modified document ?

I 've got an issue with mongoose schema validation, while trying to validate the schema of documents modified inside a Model::updateMany (or update + mutli:true) request.
I've got the schema below:
var BusinessesSchema = new Schema({
label: {
type: String,
required: true
},
openingDate: {
type: Date,
required: true
},
endingDate: {
type: Date,
validate: function(value) {
if (this.constructor.name === 'Query') {
// Looks like this is a validation for update request
var doc = null;
switch (this.op) {
case 'update':
case 'updateMany': {
doc = this._update.$set;
break;
}
case 'findOneAndUpdate': {
doc = this._update;
break;
}
default:
// keep null, will throw an error
}
return doc.openingDate < value;
}
else {
return this.openingDate < value;
}
}
}
});
I would like to access modified documents value ("this") inside endingDate::validate function to make sure that for each modified document ending Date is greater than beginning one .
Even, when using pre-hook (for update & updateMany, as below), I still do not find any way to access the modified documents value to perform my check when multi is set (or when calling updateMany).
BusinessesSchema.pre('update', function(next) {
this.options.runValidators = true;
this.options.context = 'query';
next();
});
BusinessesSchema.pre('updateMany', function(next) {
this.options.runValidators = true;
this.options.context = 'query';
next();
});
I probably missed something, and would really appreciate help here.
Can't do that. Best you can do with updateMany is to capture the query context, from this, and analyze the update. Something like this:
Schema.pre('updateMany', function (next) {
const update = this.getUpdate();
if (update.$set && update.$set && !validateUpdate(update.$set)) {
throw new Error('Invalid Update');
}
next();
});
If you wanna do validation on the resulting document before the update using the save hook, you can use a cursor:
Schema.pre('save', function(next) {
if (!validDoc(this)) {
throw new Error('Invalid Doc');
}
next();
}
Schema.statics.updateManyWithValidation = async function(criteria, update) {
const cursor = Model.find(criteria).cursor();
let doc = null;
do {
doc = await cursor.next();
Object.assign(doc, update);
await doc.save();
} while(doc != null);
}
Now, bear in mind this is a much more expensive operation since you're fetching the documents, applying the changes, and then saving them individually.

How to apply function for all Collection

I want to update my collection in server.js by using a function.
When I change one field I need to change multiple collections.
My question is how can I use a parameter as a Collection name. Is there any way for it or I must write a function for each Collection?
update: function(personID,option) {
return Personel.update(
{ id: personID },
{ $set: option },
{ multi: true }
);
},
I want to apply this logic for separate collections.
There is a trickier workaround for this problem. you need to actually bind all of your collection in a single object.
CollectionList = {};
CollectionList.Personel = new Mongo.Collection('personel');
CollectionList.secondCollection = new Mongo.Collection('second');
after that you pass as your collection name as a string into the method.
update: function(collectionName,personID,option){
return CollectionList[collectionName].update(
//..rest of your code
);
You can try this approach:
var Personel = new Mongo.Collection('personel');
var Items = new Mongo.Collection('items');
var SomeOtherCollection = new Mongo.Collection('someOtherCollection');
....
update: function(personID, option, collectionName) {
// Choose collection by given name
var Collection = {
Personel: Personel,
Items: Items,
SomeOtherCollection: SomeOtherCollection
}[collectionName];
return Collection.update(
{ id: personID },
{ $set: option },
{ multi: true }
);
},

FindOneAndUpdate not updating nested field with passed in parameters

I am trying to create a service that can be used to update nested fields in a Mongoose model. In the following example I am trying to set the field 'meta.status' to the value 2. This is the service:
angular.module('rooms').factory('UpdateSvc',['$http', function($http)
{
return function(model, id, args)
{
var url = '/roomieUpdate/' + id;
$http.put(url, args).then(function(response)
{
response = response.data;
console.log(response.data);
});
}
}]);
This is how it is called in the controller:
var newStatus = {'meta.$.status' : 2};
var update = UpdateSvc("roomie", sessionStorage.getItem('userID'), newStatus);
And this is the model:
var RoomieSchema = new Schema(
{
meta:
{
created:
{
type: Date,
default: Date.now
},
status:
{
type: Number,
default: '1',
}
}
}
And this is the route:
app.put('/roomieUpdate/:id', function(req,res)
{
var id = req.params.id;
Roomie.findOneAndUpdate(
{_id: mongoose.Types.ObjectId(id)},
req.body,
{ new : true },
function(err, doc)
{
if(err)
{
console.log(err);
}
res.json(doc);
console.log(doc);
});
});
The argument is received correctly, but I can't seem to get this to work. I am not even getting an error message. console.log(doc) simply prints out the object and the field meta.status remains '1'. I have done a direct Mongo search on the target object to make sure that I wasn't just reading the old document. I've tried a great many things like separating the key and value of req.body and use {$set:{key:value}}, but result is the same.
findOneAndUpdate() by default will return the old document, not the new (updated) document.
For that, you need to set the new option:
Roomie.findOneAndUpdate({
_id : mongoose.Types.ObjectId(id)
}, req.body, { new : true }, function(err, doc) {
...
});
As it turns out, var newStatus = {'meta.$.status' : 2}; should have been var newStatus = {'meta.status' : 2}; The document now updates correctly.
The reason the $ was there in the first place was probably based on this thread:
findOneAndUpdate - Update the first object in array that has specific attribute
or another of the many threads I read through about this issue. I had tried several solutions with and without it, but couldn't get anything to go right.

Resources