mongoose query .where/.and after .populate - javascript

I need to test if a user has the given id, a project with a specified id and a role with a given name.
var UserSchema = new Schema({
roles: [{
project: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Project',
},
role: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Role',
}
}]
});
and
var RoleSchema = new Schema({
name: {
type: String
}
});
I tryed to .populate and then apply .where, but .where does nothing.
Using an .and after populate does not work either.
How to solve this in mongodb/mongoose?
Thank you!
//EDIT
Right now I have something like that, which does not work (.where does nothing) and it is really not beautiful:
User.findById(userId)
.populate({
path: 'roles.role',
match: { 'name': roleName}
})
.where('roles.project').equals(projectId)
.exec(function(err, data){
data.roles = data.roles.filter(function(f){
return f.role;
})
if(!err){
if(data){
if(data.roles.length == 1) {
return true;
}
}
}
return false;
});
When I do what Kevin B said:
Role.findOne({name: roleName}, function(err, data){
if(!err){
if(data){
User.findById(userId)
.and([
{'roles.project': projectId},
{'roles.role': data._id}
])
.exec(function(err2, data2){
if(!err2){
if(data2){
console.log(data2);
}
}
});
}
}
});
The .and query just does nothing here...

Now I'm just doing comparisons in the program instead of the database.
User.findById(userId)
.populate('roles.role')
.exec(function(err, data){
if(!err){
if(data){
if(data.roles.find(function(element, index, array){
return element.project == projectId && element.role.name == roleName;
}))
return callback(true);
}
}
return callback(false);
});

Related

How to filter data from mongo collection subarray with subarray data of other collection

Baiscally making a node.js, mongodb add friends functionality where having the option of list user to add in friends list, sent friends request, accept friends request, delete friends request, block friends request.
Register Collection
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let Register = new Schema(
First_Name:{
type: String,
required: true
},
Last_Name: {
type: String
},
Email: {
type: String,
unique: true,
lowercase: true,
required: true
},
Friends:[{type: String}],
});
module.exports = mongoose.model('Register', Register);
Friends Collection
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var ObjectId = require('mongodb').ObjectID;
let Friends = new Schema({
Requester: {
type: ObjectId,
required: true
},
Recipients: [{Recipient:{type:ObjectId},Status:{type:Number}}],
});
module.exports = mongoose.model('Friends', Friends);
Inside Node.js Post API
var Register = require('../models/register.model');
var Friends =require('../models/friends.model');
router.post('/getdata',function(req,res)
{
let Email="example#example.com";
Register.findOne({ Email : Emails }, function(err, user) {
Friends.findOne({ Requester :user._id }, function(err, user1) {
Register.find({$and:[{Friends:{$nin:[user._id]}},{_id:{$ne:user1.Recipients.Recipient}}]},function(err, user2) {
console.log("user2",user2);
//Here User2 data is not coming
//How to get data so can able to list user that is not added yet in FriendList
//Mainly user1.Recipients.Recipient this is not working because //Recipients is array so how can match all data with array, if i am //using loop then find return data scope ends on inside find closing //braces only.
//Any suggestion
});
});
});
So if I have it correct, you want to do the following:
Find a registration based on a given email
Find the friends related to this user
Find registrations that are not yet in the friend list of the user
Also, given what you've typed, I'm assuming A can be the friend of B, but that doesn't mean B is the friend of A.
While the data structure you currently have may not be optimal for this, I'll show you the proper queries for this:
var Register = require('../models/register.model');
var Friends =require('../models/friends.model');
router.post('/getdata',function(req,res) {
const email = "example#example.com";
Register.findOne({ Email: email }, function(err, user) {
if (err) {
console.error(err);
return;
}
Friends.findOne({ Requester: user._id }, function(err, friend) {
if (err) {
console.error(err);
return;
}
const reciptientIds = friend.Recipients.map(function (recipient) {
return recipient.Recipient.toString();
});
Register.find({Friends: { $ne: user._id }, {_id: { $nin: recipientIds }}, function(err, notFriendedUsers) {
if (err) {
console.error(err);
return;
}
console.log(notFriendedUsers);
});
});
});
});
P.S. This "callback hell" can be easily reduced using promises or await/defer
Finally able to solve it, below is the solution
var Register = require('../models/register.model');
var Friends =require('../models/friends.model');
router.post('/getdata',function(req,res)
{
let Emails="example#example.com";
Register.findOne({$and:[{ Email : Emails}] }, function(err, user) {
if (err) {
console.error(err);
return;
}
Friends
.findOne({ Requester: user._id },
{ _id: 0} )
.sort({ Recipients: 1 })
.select( 'Recipients' )
.exec(function(err, docs){
docs = docs.Recipients.map(function(doc) {
return doc.Recipient; });
if(err){
res.json(err)
} else {
console.log(docs,"docs");
Register.find({$and:[{Friends: { $ne: user._id }},{_id: { $nin: docs }},{_id:{$ne:user._id}}]}, function(err, notFriendedUsers) {
if (err) {
console.error(err);
return;
}
console.log(notFriendedUsers);
});
}
})
});

How to retrieve value from another mongoose collection

I'm attempting to find() in one collection and concatenate that value to the correct object returned by subsequent find(). However I cannot guarantee the value retrieved in the first find() will match the index . How do I ensure the first value (Boolean) is attached to the correct instance of the second find()?
What I have so far is to use the indexes of the first condition but it may not match if an instance has been removed
My Model:
let Instance= new Schema({
imgName: String,
offer: String,
brand: String,
desc: String,
keywords: Array,
loc: String,
location: {
type: {
type: String, // Don't do `{ location: { type: String } }`
enum: ['Point'], // 'location.type' must be 'Point'
required: true
},
coordinates: {
type: [Number],
required: true
}
},
categories: Array,
userId: String,
active: Boolean,
stockPic: String,
startTime: Number,
endTime: Number,
range: Boolean
});
mongoose.model('Beams2', Instance);
let LikesSchema = new Schema({
userId: String,
likeId: String,
categories: Array,
public: Boolean
});
mongoose.model('Likes', LikesSchema);
//My query:
exports.findAllLikes = function(req, res){
Likes.find({'userId': req.body.userId}, function(err, results) {
let likeIds = results.map((currentValue, index, array) => {
return currentValue.likeId;
});
let statusArr = results.map((currentValue, index, array) => {
return currentValue.public;
});
Instances.find({'_id': { $in: likeIds }}, function(err, favs){
if (err) return console.log(err);
let newArr = [];
favs.forEach(function(element, i) {
//console.log(statusArr[i]);
let post = element.toObject();
post.public = statusArr[i]; //need to guarantee correct value
newArr.push(post);
console.log(post);
});
return res.send(newArr);
});
});
};
I think, this is how you could achieve what you are looking for:
exports.findAllLikes = function(req, res) {
Likes.find({
'userId': req.body.userId
}, (err, results) => {
if (err) {
console.log('Error while fetching likes: ', err);
return res.status(500).json({
Error: 'Error while fetching likes.'
});
}
const likeIds = results.map(({
likeId
}) => likeId);
Instances.find({
'_id': {
$in: likeIds
}
}, (err, favs) => {
if (err) {
console.log('Error while fetching likes: ', err);
return res.status(500).json({
Error: 'Error while fetching Instances.'
});
}
let newArr = [];
favs.forEach((elem) => {
let post = elem.toObject();
const likeDoc = results.find(({
likeId
}) => likeId === post._id.toString());
post.public = (likeDoc && likeDoc.public) ? likeDoc.public : '';
newArr.push(post);
});
console.log(post);
return res.status(200).send(newArr);
});
});
};
You shouldn't ever play with indexes for mapping stuff, until and unless you are sure that you have same length arrays.
Hope this helps :)

Populate Query Options with Async Waterfall

I'm trying mongoose populate query options but i don't know why the query options doesn't work.
I have user schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema(
{
username: { type: String, required: true },
email: { type: String },
name: { type: String },
address: { type: String }
},
{ timestamps: true }
);
module.exports = mongoose.model('User', UserSchema);
and feed schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const FeedSchema = new Schema(
{
user: { type: Schema.ObjectId, ref: 'User' },
notes: { type: String, required: true },
trx_date: { type: Date },
status: { type: Boolean, Default: true }
},
{ timestamps: true }
);
FeedSchema.set('toObject', { getters: true });
module.exports = mongoose.model('Feed', FeedSchema);
I want to find all feed by user id, i used async waterfall like the following code:
async.waterfall([
function(callback) {
User
.findOne({ 'username': username })
.exec((err, result) => {
if (result) {
callback(null, result);
} else {
callback(err);
}
});
},
function(userid, callback) {
// find user's feed
Feed
.find({})
// .populate('user', {_id: userid._id}) <== this one also doesn't work
.populate({
path: 'user',
match: { '_id': { $in: userid._id } }
})
.exec(callback);
}
], function(err, docs) {
if (err) {
return next(err);
}
console.log(docs);
});
With above code, i got all feeds, and it seems like the query option do not work at all, did i doing it wrong ?
Any help would be appreciate.
Not sure why you are looking to match "after" population when the value of _id is what is already stored in the "user" property "before" you even populate.
As such it's really just a simple "query" condition to .find() instead:
async.waterfall([
(callback) =>
User.findOne({ 'username': username }).exec(callback),
(user, callback) => {
if (!user) callback(new Error('not found')); // throw here if not found
// find user's feed
Feed
.find({ user: user._id })
.populate('user')
.exec(callback);
}
], function(err, docs) {
if (err) {
return next(err);
}
console.log(docs);
});
Keeping in mind of course that the .findOne() is returning the whole document, so you just want the _id property in the new query. Also note that the "juggling" in the initial waterfall function is not necessary. If there is an error then it will "fast fail" to the end callback, or otherwise pass through the result where it is not. Delate "not found" to the next method instead.
Of course this really is not necessary since "Promises" have been around for some time and you really should be using them:
User.findOne({ "username": username })
.then( user => Feed.find({ "user": user._id }).populate('user') )
.then( feeds => /* do something */ )
.catch(err => /* do something with any error */)
Or indeed using $lookup where you MongoDB supports it:
User.aggregate([
{ "$match": { "username": username } },
{ "$lookup": {
"from": Feed.collection.name,
"localField": "_id",
"foreignField": "user",
"as": "feeds"
}}
]).then( user => /* User with feeds in array /* )
Which is a bit different in output, and you could actually change it to look the same with a bit of manipulation, but this should give you the general idea.
Importantly is generally better to let the server do the join rather than issue multiple requests, which increases latency at the very least.

Node js aggregation and findone concept

I need to find the history of person1 deals..so in my getHistory module i have filtered all the deals in that person1's dealSchema , in acceptreject function i have used aggregation concept to filter the deals which have person1's _d in the accepted field. But the callback function acceptreject does not fetch the result it always results in []..anyone please findout the mistake i have done in this code..Thanks in advance
var acceptreject = function (userId, callback){//callback function for finding accepted deal
console.log("Aggregate:" +userId);
if(userId == null && userId == 'undefined'){
console.log("error fetching id");
res.send(new Response.respondWithData('failed','Invalid userId'));
return next();
}
User.aggregate([
{
$unwind: "$deals"
},
{
"$match":
{
"deals.accepted": userId
//"deals.rejected": id
}
},
{
$project:{
"shopName":"$deals.shopName",
"deal":"$deals.deal",
"price":"$deals.price",
"name":"$name"
}
}
],function (err, user){
console.log(user);
if (err){
callback (err);
}
console.log("Your accepted deal:"+ user);
// res.send(200,user);
callback(null, user);
})
}
exports.getHistory = function (req, res, next) {
var incomingUser = req.user;
var id = incomingUser._id;
User.findById(id,function (err, user){
console.log(user);
if (err) {
return next(new Response.respondWithData('failed','Cant find theuser'));
}
var dealObj = _.filter(user.deals);
acceptreject({
userId : id
},function(err, users){
if(err){
res.send(new restify.InternaError(err.message));
return next();
}
else{
console.log("final");
var final = {
accepteddeal: users,//it returns [] in result but person1 accepted the deal
mydeal: dealObj
}
next(res.send(200, final));
}
});
});
}
And my schema is
var dealSchema = new mongoose.Schema({
shopName: {type: String,required: true},
deal: {type: String,required: true},
price:{type: Number,required: true},
start:{type: Date,default: Date.now},
end:{type: Date},
expiry:{type: Date},
comments:{type: String},
accepted: {type:mongoose.Schema.Types.ObjectId, ref:'user'},//person1 _id
rejected: {type:mongoose.Schema.Types.ObjectId, ref: 'user'}
});
var userSchema = new mongoose.Schema({
name: { type: String,required: true},
phone: { type: Number, required: true,unique: true},
email:{type: String},
password: {type: String},
deals:[dealSchema]
}, {collection: 'user'});
mongoose.model('Deal', dealSchema);
mongoose.model('user', userSchema);
I have done a mistake in callback function where the userId parameter of that function does not pass the id
var accept= function (userId, callback){
console.log("Aggregate:" +userId);
if(userId == null && userId == 'undefined'){
res.send(new Response.respondWithData('failed','Invalid userId'));
return next();
}
var id = new Object(userId);
console.log(id);
User.aggregate([
{
$unwind: "$deals"
},
{
"$match": {
"deals.accepted" : id
}
},
{
$project:{
"shopName":"$deals.shopName",
"deal":"$deals.deal",
"price":"$deals.price",
"name":"$name"
}
}
],function (err, user){
console.log(user);
if (err){
callback(err);
}
console.log("Your accepted deal:"+ user);
//res.send(200,user);
callback(null, user);
})
}

Can't push a json object into array using javascript and mongodb

I have a problem pushing into my Student model data and its schema looks as below:
var StudentSchema = new Schema({
firstName: {
type: String,
trim: true,
default: ''
//validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: ''
//validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
worksnap: {
user: {
type: Object
},
timeEntries : [],
},
timeEntries : []
});
While my javascript code for pushing items looks like this:
Student.findOne({
'worksnap.user.user_id': item.user_id[0]
})
.populate('user')
.exec(function (err, student) {
if (err) {
throw err;
}
//student.timeEntries.push(item); // this works
student.worksnap.timeEntries.push(item); // this does not work
student.save(function (err) {
if (err) {
//return res.status(400).send({
// message: errorHandler.getErrorMessage(err)
//});
} else {
console.log('item inserted...');
}
});
});
As you can see, if I use timeEntries array outside the worksnap object it works fine, it inserts the item as object into that array... I just don't know why it is not working the same being inside worksnap object.
Is there any other option that I can add json objects into an array type in mongo
Thanks
Use .lean()
Documents returned from queries with the lean option enabled are plain JavaScript objects, not MongooseDocuments. They have no save method, getters/setters or other Mongoose magic applied.
Student.findOne({
'worksnap.user.user_id': item.user_id[0]
})
.populate('user')
.lean()//-----Added!
.exec(function(err, student) {
if (err) {
throw err;
}
//student.timeEntries.push(item); // this works
student.worksnap.timeEntries.push(item); // this does not work
student.save(function(err) {
if (err) {
//return res.status(400).send({
// message: errorHandler.getErrorMessage(err)
//});
} else {
console.log('item inserted...');
}
});
});

Categories

Resources