get Selected fields in .populate() waterline-postgresql .populate('fieldName',{select:[]}) - node.js

select query is not working in .populate() of waterline-postgresql.
Model.find(query).populate(assoc.alias,{select:['field1','field2']});
This is not working in waterline-postgresql adapter.
Is this not supported or Am I making any mistake?

select is not supported in .populate(). You can see this github issue. In populate select is not working currently.
This is feature request and it is open issue. hope in next release
waterline team will introduce this feature.

Since there's no official way to do that I did some changes in order to have that. Maybe it's not the best way but it works.
User.find({ belongs_to: user.id })
.populate('person_id')
.exec(function findCB (err, usersFound)
{
console.log(usersFound[0].toJSON())
if (err)
{
return res.json(401, {error: err});
}
if (usersFound.length == 0)
{
return res.json(200, {message: 'No user asigned'});
}
else
{
// An array to store all the final user objects
var asignedArray = [];
for (index in usersFound)
{
var myObj = {},
key,
value;
// Object in format {key: value}
myObj['id'] = usersFound[index].id;
myObj['fullname'] = usersFound[index].person_id.first_name + ' ' +
usersFound[index].person_id.second_name + ' ' +
usersFound[index].person_id.last_name;
myObj['email'] = usersFound[index].email;
myObj['job'] = usersFound[index].person_id.job;
myObj['permission_level'] = usersFound[index].permission_level;
// Adding the object to the main array
asignedArray.push(myObj);
}
return res.json(200, {
users: asignedArray
});
}
});
I hope it could be useful for you.

Related

How to check if a document contains a property in Cloud Firestore?

I want to know if there is a way to check if a property is present in a Cloud Firestore document. Something like document.contains("property_name") or if there is a document property exist.
To solve this, you can simply check the DocumentSnapshot object for nullity like this:
var yourRef = db.collection('yourCollection').doc('yourDocument');
var getDoc = yourRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
if(doc.get('yourPropertyName') != null) {
console.log('Document data:', doc.data());
} else {
console.log('yourPropertyName does not exist!');
}
}
})
.catch(err => {
console.log('Error getting document', err);
});
You could use the in operator like in the snippet bellow
const ref = admin.firestore().collection('yourCollectionName').doc('yourDocName')
try {
const res = await ref.get()
const data = res.data()
if (!res.exists) {
if (!("yourPropertyName" in data)) {
// Do your thing
}
} else {
// Do your other thing
}
} catch (err) {
res.send({err: 'Something went terribly wrong'})
}
I think you refer to making a query.
Still there is no way to check if some field is present or not in the Firestore. But you can add another field with value true/false
val query = refUsersCollection
.whereEqualTo("hasLocation", true)
query.get().addOnSuccessListener {
// use the result
}
check out this links for more
https://firebase.google.com/docs/firestore/query-data/queries
How do I get documents where a specific field exists/does not exists in Firebase Cloud Firestore?

Find data with no key value

So I want to search for items in my database using mongoose .find() function.
In my router I have this code to get certain items from the url.
For example;
mydomainname.com/market?type=1&name=hi&stats=123
...?type=123&name=&...
var type = req.query.type;
var name= req.query.name;
var stats= req.query.stats;
Model.find({type: type, name: name, stats: stats})
.exec(function(err, model){
if(err){
console.log("error")
}else{
res.render('*jade*', {models: JSON.stringify(model)})
}})
This works fine, but when there is no query value in the url(as seen above) the query value will be set to ''. Which then sorts away every item I have on my database because there is none with exmaple name = '';
I have search around but I have not find any help so if any would be able to give me tip I would be grateful!
You could create your find() query object based on the value of the request query parameters. The following example checks for the name field if it has an empty string value, remove the property from the query and then use the final query object as your find() filter:
var q = req.query;
if (q.name === '') {
delete q.name;
}
// carry out further checks
Model.find(q)
.exec(function(err, model){
if(err){
console.log("error");
}else{
res.render('*jade*', {models: JSON.stringify(model)});
}
})
Try this
var q = req.query;
var data ={};
if (q.name !=null) {
data.name = q.name;
}
else if (q.type !=null){
data.type = q.type;
}
else if (q.stats !=null){
data.stats = q.stats;
}
else{
data={};
}
Model.find(data)
.exec(function(err, model){
if(err){
console.log("error");
}else{
res.render('*jade*', {models: JSON.stringify(model)});
}
})

Using findOne then save() to replace a document, mongoose

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).

Scope in exec callback

I am attempting to break out of a function based on an expression, however am having trouble with the scoping. Here is a snippet of the code:
function createService(dict, res) {
// Ensure no duplicate
var serviceExists = Service.find({name: dict['name']}).count().exec(function(err, doc) {
return (doc !== 0);
});
console.log(serviceExists);
if(serviceExists){
res.send(500, "Duplicate document. Try updating existing entry or chooseing a different name");
return;
}
//Insert the document
service = new Service(dict);
service.save(function(err) {
if(!err) {
res.send("Service saved");
}
});
}
The output of the console.log():
{ emitted: {},
_events: { err: [Function], complete: [Function] } }
The end goal here is that the code will not reach the "Insert the document" portion if doc !== 0. Please let me know the correct way of doing this (Maybe using exceptions? That is the only idea I have left). Thanks
Service.find is asynchronous. the callback in exec doesn't execute immediately. This causes problem 1. (If Service....exec(...) returned a value, your console.log would have already excuted, before the callback.)
Problem 2 is also pretty common. return in exec() doesn't return a value you can assign to a variable. (exec() does not return the return value of your anonymous function.)
Here is a fix for your code:
function createService(dict, res) {
// Ensure no duplicate
Service.findOne({name: dict['name']}).count().exec(function(err, doc) {
var serviceExists = (doc !== 0);
console.log(serviceExists);
if(serviceExists){
res.send(500, "Duplicate document. Try updating existing entry or chooseing a different name");
return;
}
//Insert the document
service = new Service(dict);
service.save(function(err) {
if(!err) {
res.send("Service saved");
}
});
});
}
I also changed find to findOne, otherwise you'll get an array instead of a doc.

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