Re: Mongoose 'find'
I'm using Mongoose.js and I need to get all the friend obj which has the relationId == '2'.
Here is my data set:
var someId = '2';
var users = [
{
username: 'robert',
email: 'robert@robert.com',
friends: [
{
relationships:[
{
relationId: '1',
description: ''
}
]
}
]
},
{
username: 'john',
email: 'john@john.com',
friends: [
{
relationships:[
{
relationId: '2',
description: ''
}
]
}
]
}
];
and this is my query: User = mongoose.model('users', userSchema);
Users.find( { "friends.relationships.relationId": someId }, function(err, friendObj) {
console.log('friendObj: ', friendObj); // this friendObj is not correct as it's the user obj instead of user.friend obj
} );
// The above code returns all users which is:
[
{
username: 'robert',
email: 'robert@robert.com',
friends: [
{
relationships:[
{
relationId: '1',
description: ''
}
]
}
]
},
{
username: 'john',
email: 'john@john.com',
friends: [
{
relationships:[
{
relationId: '2',
description: ''
}
]
}
]
}
]
// where as I want it to return:
friends: [
{
relationships:[
{
relationId: '2',
description: ''
}
]
}
]
Any ideas as how I can get the friends obj with relationId = '2' ?
Thanks,