Using findOne then save() to replace a document, mongoose - node.js

I want to use the validation in my schema. Therefore i can't use findOneAndUpdate (?). I must use save.
Problem is, if I use findOne, then replaces the object with the one I'm going to replace it with, it will no longer have the save function.
mongoose.model('calculations').findOne({calcId:req.params['calcId']}, function(err, calculation){
if(err) {errHandler.serverErr(err, res, 'Something went wrong when trying to update a calculation'); return;}
calculation = calculationToReplace;
calculation.save(function(err, calc){ //No longer exists
if(err) {errHandler.serverErr(err, res, 'Something went wrong when trying to update a calculation'); return;}
res.send(200);
});
});
This must be a common task but I can't find any solution. How do I fix this?

There is a simple solution to your (by now really old) question.
In my case I had to have a findOneAndUpdate upsert that returned more information on what happened. So my solution was to step through the process to update the object with a for loop.
(Think the reason why you can't just copy is that the doc object contains a bunch of "extras" like version information and save function and other "bits"); So here is my solution.
exports.postData = function(req,res) {
console.log("will create " + req.body.alias);
console.log("It is level " + req.body.level); //OK, all this have to be changed to members of the data! req.body contains all the data sent from the user at this time
var query = { 'fulltext' : req.body.fulltext};
console.log("Checkking if " + req.body.fulltext + " exists")
Skill.findOne(query, function (err,doc){
if(err) return res.status(500).send(err)
if (!doc){
console.log(req.body.fulltext + " not found!")
var newdoc = new Skill(req.body);
newdoc.save(function(err){
if(err) return res.status(500).send(err)
console.log(newdoc.fulltext + " created as " + newdoc._id);
return res.status(200).send({_id: newdoc._id, alias: newdoc.alias})
})
return res.status(200).send('blal')
} else {
console.log(req.body.fulltext + " found!")
for (var id in req.body ){
doc[id]= req.body[id];
}
doc.save( function(err){
if(err) return res.status(500).send(err)
return res.status(200).send({_id: doc._id, alias: doc.alias})
})
//return res.status(200).send({_id: doc._id, alias: doc.alias})
}

I have not tested the following, so I am not sure if this works properly but it should probably be fine:
Swap this:
calculation = calculationToReplace;
with this:
for (var key in calculationToReplace)
if(typeof calculation[key] !== 'function')
calculation[key] = calculationToReplace[key];

Yes there is a way. You can read the mongoose documentation here. Take a look at the following code.
Tank.findById(id, function (err, tank) {
if (err) return handleError(err);
tank.size = 'large';
tank.save(function (err) {
if (err) return handleError(err);
res.send(tank);
});
});
This approach involves first retreiving the document from Mongo, then issuing an update command (triggered by calling save).

Related

native js how to find a field name of a collection where another field equal to certain value

I know that on robomongo if I want to find _id of user where username = test2 I can use
db.getCollection('user').find({},{_id:1},{"username":"test2"})
now, on visual studio code, I want to find value of field "disabled" from user collection where field "username" value equal to variable "tempusername" value. I tried:
colUser = mongoDb.collection("user");
var status = colUser.find({},
{ disabled:1},{ username:tempusername},function (err, doc) {
console.log(doc);
});
but it shows value of status is "undefined". What is the write code for this?
I think it's something you're looking for.
const url = 'mongodb://localhost:27017'
MongoClient.connect(url, (err, db) => {
const dbo = db.db('mydb')
dbo.collection('user').find({disabled:'1',username:tempusername}).toArray((err, doc) => {
if(err){
console.log(err)
}
console.log(doc)
db.close()
})
})
I found the answer, basically the way it work is the result will be return inside the function, so I have to put it like this:
var statusbuffer;
colUser.findOne({ username:tempusername},{ _id:0,disabled:1},function (err, userstatus){
// User result only available inside of this function!
if (err) {
next("Failed to update records");
} else {
console.log("disabled status here:",userstatus.disabled) // => yields your user results
statusbuffer = userstatus.disabled;
next();
}
});
thanks all for your comments!

mongoose finds but doesn't remove

I am finding any document that matches my condition and I want to remove those docs.
MyModle.find({writer: req.body.id}, function(err, docs){
if (err){ return console.log(err)}
if (!docs || !Array.isArray(docs) || docs.length === 0){
return console.log('no docs found')}
docs.forEach( function (doc) {
doc.remove();
console.log("Removed book with ID ",doc._id, "by writer ", req.body.id);
});
});
My console is printing the message as if the document was removed, but it is still in the collection.
What is wrong here?
As stated in the documentation for remove, the deletion is performed only if you either:
pass a callback function: doc.remove(function() { console.log('removed!'); });
or call exec: doc.remove().exec()
See also this question
To fix your code, you can replace:
doc.remove();
console.log("Removed book with ID ",doc._id, "by writer ", req.body.id)
with
doc.remove(function() {
console.log("Removed book with ID ",doc._id, "by writer ", req.body.id)
});

Show entire MongoDB contents in Node.js API

First off, don't worry, it's a tiny data set - I realise it wouldn't be wise to dump an entire production DB to a single screen via an API... I just need to get a JSON dump of entire (small) DB to return via an API endpoint in a Node.js application.
My application does successfully return single records with this code:
MongoClient.connect("mongodb://localhost:27017/search", function (err, db) {
if(err) throw err;
db.collection('results', function(err, collection) {
// search for match that "begins with" searchterm
collection.findOne({'string':new RegExp('^' + searchterm, 'i')}, function(err, items){
// get result
var result;
if (items == null || items.result == null){
result = "";
}
else {
result = items.result;
}
// return result
res.send(result);
});
});
});
So I know Node is talking to Mongo successfully, but how can I tweak this query/code to basically return what you get when you execute the following on the MongoDB command line:
$ db.results.find()
This is snippet.
model.find({}).exec(function (err, result) {
if (err) {console.error(err); return;}
else return result;
});
First use your predefined model and call find. the logic is to place a empty object {} essentially rendering . select all from this model.
Make sense?
Exactly as you've described it.
collection.find({}).exec((err, result) => {
if (err) {
console.log(err);
return;
}
if (result.length > 0) {
// We check that the length is > 0 because using .find() will always
// return an array, even an empty one. So just checking if it exists
// will yield a false positive
res.send(result);
// Could also just use `return result;`
});
Thanks guys, I appreciate your answers pointing me in the right direction, in terms of using {} as the query. Here is the code that eventually worked for me:
db.collection('results', function(err, collection) {
collection.find({}).toArray(function(err, docs) {
res.send(docs);
});
});
The crucial element being the toArray(...) part.

mongoose update a value in a document using model.update

What am i doing wrong here, I want to update the score?
the problem is it executes the code without errors but doesn't update the score.
Team.findOne({name: req.body.team}, function(err, teamData){
if(teamData) {
var a = teamData.score + 1;
Team.update({name: req.body.team},{$set: {score : a}});
}
else {
console.log(err);
}
});
As Muhammad Ali said, update requires a callback function, that is probably your problem. But there is also an easier way to do this:
Team.findOne({name: req.body.team}, function(err, teamData){
if(teamData){
teamData.score += 1
teamData.save(function(err) {
if (err) // do something
});
}else{
console.log(err);
}
});

NodeJS + Mongoose: Updating all fields on a Mongoose model

I'm building out an api using Node, MongoDB and Mongoose. One thing that is bugging me is that you can't seem to set multiple fields at once:
app.put('/record/:id', function(req, res) {
Record.findById(req.params.id, function(err, doc) {
if (!err) {
doc.update(req.params);
doc.save();
...
However, it seems that you have to work out the update query and run it on the Model object rather than on the document object. Unless you want to assign individual properties and run save() at the end.
Is there any way of accomplishing this without having to write a Mongo query?
jsaak's answer is good but doesn't work for nested objects. I elaborated on his answer by searching and setting nested objects.
I added these functions to a utility.js file
var _ = require('underscore');
exports.updateDocument = function(doc, SchemaTarget, data) {
for (var field in SchemaTarget.schema.paths) {
if ((field !== '_id') && (field !== '__v')) {
var newValue = getObjValue(field, data);
console.log('data[' + field + '] = ' + newValue);
if (newValue !== undefined) {
setObjValue(field, doc, newValue);
}
}
}
return doc;
};
function getObjValue(field, data) {
return _.reduce(field.split("."), function(obj, f) {
if(obj) return obj[f];
}, data);
}
function setObjValue(field, data, value) {
var fieldArr = field.split('.');
return _.reduce(fieldArr, function(o, f, i) {
if(i == fieldArr.length-1) {
o[f] = value;
} else {
if(!o[f]) o[f] = {};
}
return o[f];
}, data);
}
implement as:
var util = require('./utility');
app.put('/record/:id', function(req, res) {
Record.findById(req.params.id, function(err, doc) {
if (!err) {
utils.updateDocument(doc, Record, req.params);
doc.save();
...
Maybe this has changed since this question was first asked, but you can update multiple paths in Mongoose with the set method ike:
// object
doc.set({
path : value,
path2 : {
path : value
}
});
doc.save();
References
http://mongoosejs.com/docs/api.html#document_Document-set
direct updating is not recommended according to this document:
http://mongoosejs.com/docs/2.7.x/docs/updating-documents.html
i solved it like this:
Book.findOne({isbn: req.params.isbn}, function (err, book){
if (err) {
res.send(422,'update failed');
} else {
//update fields
for (var field in Book.schema.paths) {
if ((field !== '_id') && (field !== '__v')) {
if (req.body[field] !== undefined) {
book[field] = req.body[field];
}
}
}
book.save();
}
});
If you want to update the entire document , you can delete the document based on its id and store the entire object again.
That object must contain data for each and every fields of the mongo document.
Here is an example.
mongoDBCollectionObject.findOneAndRemove({ // -- it will delete the entire document
_id: req.body.fieldsdata._id // here fiedsdata is exact copy with modification of previous data
}, function(err, data) {
var newFieldsData = new mongoDBCollectionObject(fieldsdata); //-- fieldsdata updated data
newFieldsData.save(function(err, data) { // save document to that collection with updated data
if (err) {
console.log(err);
} else
res.json({
success: true
});
});
})
To clarify the question, it looks like you are taking the Request parameters and using those to find and update the given document.
Is there any way of accomplishing this without having to write a Mongo query?
The obvious answer is to update the Model object with the value from the Request. Which is what you suggest...
Unless you want to assign individual properties and run save() at the end.
But it seems like you don't want to do this? It sounds like you want to update the Model object directly from the Request object?
You can do this if you really want. You just loop through req.params and set the doc values where appropriate.
for(var i in req.params) {
if(req.params[i] != doc[i]){
doc[i] = req.params[i];
}
}
It should be as simple as this. However, you only want to do this if you have a whole bunch of validation code on the Model objects. The whole point to the Model is that you don't want to get random data in the DB. The line above will generically "set" the correct values, but you'll definitely need to include code for authentication, authorization and validation around that simple for loop.
try to updating the collection without the find, like this
Record.update({_id:req.params.id}, {$set: { field: request.field }}, {upsert: true}, function(err{...})
The option upsert create the document if not exist.
In case you have a new object and want to update whole object in the database, you can update multiple fields at once like this:
find the object
get all schema paths (fields)
save the new object.
SomeModel.findOne({ 'id': 'yourid' },function (err, oldObject) {
if (err) return handleError(err);
// get all schema paths (fields)
SomeModel.schema.eachPath(function(path) {
// leave __id and __v alone
if (path != '_id' && path != '__v') {
// update the data from new object
oldObject[path] = newObject[path];
}
})
oldObject.save(function(err) {
if (err)
console.log(err)
});
})
A neat and clean approach would be using async await and findOneAndRemove along with create Here is the sample code
try {
let resp = await this.findOneAndRemove({ _id: req.body._id });
let entry = await this.create(req.body);
} catch (err) {
}
Don't Forget to mark this whole function as async

Resources