Update multiple records using mongoose? - mongodb

I am writing a service class (mongo_service.js) in order to keep my CRUD operations seperate for a project.
Below is the code to update multiple items.
updateMultipleCollection(Model, searchObject, collectionNewToUpdate) {
return new Promise(function (resolve, reject) {
Model.update( searchObject, collectionNewToUpdate, {new:true, multi:true}, function (err, data) {
if (err) {
reject(err);
}
resolve (data);
console.info("Multiple updates successful");
});
});
};
This works fine but the promise won't return the updated objects and I was wondering whether there was any other way to overcome this??
NOTE: In mongoose documentation they have mentioned that .update() will not return the updated objects whereas .findOneAndUpdate() does.

you could do a .find() and that will return an array.
Then do a forEach over the array and do .save on each doc. The new doc should be in the callback of .save

Related

Mongoose findOneAndUpdate return not updated model

I've 1 little issue. I'm trying update model by findOneAndUpdate method. And this method works unexpected - method update model in DB but return old model (before update)
try {
const updatedLanding = await Landing.findOneAndUpdate({key: req.body.key}, {
$set: {
name: req.body.name,
}
},
).exec((err, result) => {
if (err) {
res.status(422).send({error: err});
return
}
res.send({response: result})
});
}
catch (e) {
res.status(400).send(e)
}
in mongoose query, findOneAndUpdate returns the old record that has been updated, not the updated record, the record has actually been updated, but you can not get the updated result as the query returns the old one by default, if you want to see the updated record you have to issue another query to find the record and get its updated data.
If you update document using findOneAndUpdate() hook, you'll get the old document unless you specify
{ new: true }

WaterlineJs find() with no criteria and fields/select provided does not work

I am trying to fetch all the records but with selected fields, I have tried the following ways but none works:
Post.find(
{
where: {},
select: ['title']
}
);
Post.find(
{},
{
fields: {
title: 1
}
}
);
As this answer points out, the fields param "WILL work as long as you pass other params with it such as limit or order."
Alternatively, if you want this throughout your application, you could define a custom toJSON function for your model, under attributes. If not, you could still define it under some other (e.g. filter) and use map to return the custom objects instead of the default model. Remember to take care of the control flow while using map though. Use async/promises/raw logic to avoid returning before all objects are processed.
The issue has been resolved in sails-mongo latest version:
https://github.com/balderdashy/waterline/issues/1098
Thanks
I've played with trying to get above answer to use limit or order to kick in the projection to no avail.
I did see this in the docs located here:
http://sailsjs.org/documentation/reference/waterline-orm/models/native
With an out of the box solution for exactly what you're doing (pasted here for ease of use).
Pet.native(function(err, collection) {
if (err) return res.serverError(err);
collection.find({}, {
name: true
}).toArray(function (err, results) {
if (err) return res.serverError(err);
return res.ok(results);
});
});
Swap out the response base things and change Pet to Post and, this ought to work in the sails console:
Post.native(function(err, collection) {
if (err) throw new Error(err);
collection.find({}, {
title: true
}).toArray(function (err, results) {
if (err) throw new Error(err);
console.log(results);
});
});
You'll still get the _id field, and if you don't want that then hit the Mongo docs on not getting those hint(title: true, _id: false)hint
Hope this helps!

Update MongoDB object field with _assign/_merge/_extend

I am running into a question when to use which one, the following is update function for mongoose, it works fine.
// Updates an existing form in the DB.
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Form.findById(req.params.id, function (err, form) {
if (err) { return handleError(res, err); }
if(!form) { return res.send(404); }
var updated = _.assign(form, req.body);
updated.formContent = req.body.formContent;
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, form);
});
});
};
Tried the following to replace the form data.
_.assign(form, req.body); // Works (update database)
_.merge(form, req.body); // Not Work (database not updating, remain the same)
_.extend(form, req.body); // Works (update database)
The above result show merge doesn't work when there is object within the post data.
Could some please explain why one is not working the others is ok. I have read the following question
Lodash - difference between .extend() / .assign() and .merge()
but i am curious to understanding which one won't update the database, but when applied with assign and extend it's working.

Is there a way to query MongoDB Rest API with a list or array of IDs

I'm using a MEAN stack and with Mongoose. Is there a way to query MongoDB with multiple ids to only return those specific IDs in one query e.g. /api/products/5001,5002,5003
Is this possible or would I need to query each product individually or add an additional attribute to the products and query by that.
Update: To clarify as suggested below I've managed to get it partially working using {'_id': { $in: [5001,5002,5003]} however I'm having problems figuring out how to pass the list from the api url to the find function.
Using Express.js for router
router.get('/list/:ids', controller.showByIDs);
exports.showByIDs = function(req, res) {
Product.find({'_id': { $in: [req.params.ids]}}, function (err, product) {
if(err) { return handleError(res, err); }
if(!product) { return res.send(404); }
return res.json(product);
})
};
Then trying /api/products/list/5001 works however /api/products/list/5001,5002 doesn't. I'm not sure if it's a syntax problem in the url or my router code that needs to change or the controller.
You can use the $in operator to query for multiple values at once:
Products.find({_id: {$in: [5001, 5002, 5003]}}, function (err, products) { ... });
On the Express side, you need to use a format for the ids parameter that lets you split it into an array of id values, like you had in your first example:
/api/products/5001,5002,5003
Then in your route handler, you can call the split function on the req.params.ids string to turn it into an array of id values that you can use with $in:
exports.showByIDs = function(req, res) {
var ids = req.params.ids.split(',');
Product.find({'_id': { $in: ids}}, function (err, product) {
if(err) { return handleError(res, err); }
if(!product) { return res.send(404); }
return res.json(product);
})
};

concurrency issues while upserting and then reading the data from mongodb using mongoose

Hi I am trying to build an application which upserts data and fetches from the mongodb baser on the userid.This approach works fine for a single user.But when i try hitting for multiple users say 25 the data fetched seems to be null. Below is my upsert code
collection.update({'USER_ID': passVal.ID},
{'RESPONSE': Data}, { upsert: true }, function (err) {
if (err) {
console.log("Error in saving data");
}
var query = collection.findOne({'USER_ID': passVal.ID});
query.select('RESPONSE');
query.exec(function (err, data) {
if (err) return handleError(err);
console.log(data.RESPONSE);
});
})
I always get an error insome cases as data is null.I have written the read code in the call back of upsert only.I am stuck here any help regarding this will be much helpful.