0

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,

Community
  • 1
  • 1
Amir Mog
  • 321
  • 1
  • 4
  • 17
  • Possible duplicate of [Mongoose, Select a specific field with find](http://stackoverflow.com/questions/24348437/mongoose-select-a-specific-field-with-find) – styvane Oct 24 '15 at 06:31
  • Thanks but I tried those also, somehow, I'm getting the User instead of 'friends' only. thanks – Amir Mog Oct 26 '15 at 08:26

1 Answers1

0

What you are looking for is the .select method.

Users.find( { "friends.relationships.relationId": someId } ).select({ 'friends': 1, '_id': 0 }).exec( function(err, friendObj) {
    console.log('friendObj: %s', friendObj);
});
styvane
  • 59,869
  • 19
  • 150
  • 156
  • That's not the case because this friendObj is being returned as the entire users obj so the friendObj.friend is undefined. – Amir Mog Oct 24 '15 at 00:39
  • Thanks, bu what does .select({ 'friends': 1, '_id': 0 }) eaxctly mean? does this mean that it would get the first item in friends with _id == 0 ? if that's teh case, it would not work as it's not the case that the friends with relationId = '2' .. could you explain or let me know how to do it please. thanks again – Amir Mog Oct 24 '15 at 01:43
  • No it will get only friends for each document. – styvane Oct 24 '15 at 06:14
  • Thanks but I don't know why it still brings me the entire user obj instead of only a subset of it which is the selected 'friends' obj. Any ideas? thanks – Amir Mog Oct 26 '15 at 08:25