【问题标题】:How to get the objects inside an array of objects using Mongoose如何使用 Mongoose 获取对象数组中的对象
【发布时间】:2016-01-23 14:06:24
【问题描述】:

回复:猫鼬“找到”

我正在使用 Mongoose.js,我需要获取所有具有 relationId == '2' 的朋友 obj。

这是我的数据集:

   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: ''
                        }
                    ]
                }
             ]
        }
     ];

这是我的查询: 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: ''
                        }
                    ]
                }
             ]

有什么想法可以让我用 relativeId = '2' 获得朋友 obj 吗?

谢谢,

【问题讨论】:

标签: node.js mongodb mongoose nosql


【解决方案1】:

您正在寻找的是.select 方法。

Users.find( { "friends.relationships.relationId": someId } ).select({ 'friends': 1, '_id': 0 }).exec( function(err, friendObj) {
    console.log('friendObj: %s', friendObj);
});

【讨论】:

  • 不是这样,因为这个friendObj是作为整个用户obj返回的,所以friendObj.friend是未定义的。
  • 谢谢, .select({ 'friends': 1, '_id': 0 }) 是什么意思?这是否意味着它将在 _id == 0 的朋友中获得第一项?如果是这样的话,它就行不通了,因为没有关系 ID = '2' 的朋友......你能解释一下或让我知道该怎么做吗?再次感谢
  • 不,它只会为每个文档获取朋友。
  • 谢谢,但我不知道为什么它仍然给我带来了整个用户 obj 而不仅仅是它的一个子集,即选定的“朋友”obj。有任何想法吗?谢谢
猜你喜欢
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2023-02-26
  • 2015-07-29
  • 1970-01-01
  • 2017-04-18
  • 2016-09-01
相关资源
最近更新 更多