Mongodb 我可以使用mongoose通过多个参数查询子文档数组吗?

Mongodb 我可以使用mongoose通过多个参数查询子文档数组吗?,mongodb,mongoose,Mongodb,Mongoose,我正在尝试使用服务类型和帐户id通过他们的社会服务查找用户,但我不知道如何将其作为Mongoose查询 例如,如果我有“twitter”和“789DEF”,我如何使用Mongoose找到这个用户 { "email":"me@domain.com", "social": [ { "service": "linkedin", "username": "Test User", "url": "https://www

我正在尝试使用服务类型和帐户id通过他们的社会服务查找用户,但我不知道如何将其作为Mongoose查询

例如,如果我有“twitter”和“789DEF”,我如何使用Mongoose找到这个用户

{
"email":"me@domain.com",
"social": [
        {
            "service": "linkedin",
            "username": "Test User",
            "url": "https://www.linkedin.com/in/testuser",
            "accountId": "ABC12356"
        },
        {
            "service": "twitter",
            "username": "TestUser",
            "url": "https://www.twitter.com/testuser",
            "accountId": "789DEF"
        }
    ]
}
您需要查询运算符。假设您已经声明了一个mongoose模型(比方说
用户
),那么类似的东西应该可以工作:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}, function (err, doc) {
    // doc contains your user document
});
如果只需要相关的
社交
子文档,则还应使用投影操作符:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}).select('social.$').exec(function (err, doc) {
    // ...
});
您需要查询运算符。假设您已经声明了一个mongoose模型(比方说
用户
),那么类似的东西应该可以工作:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}, function (err, doc) {
    // doc contains your user document
});
如果只需要相关的
社交
子文档,则还应使用投影操作符:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}).select('social.$').exec(function (err, doc) {
    // ...
});
您需要查询运算符。假设您已经声明了一个mongoose模型(比方说
用户
),那么类似的东西应该可以工作:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}, function (err, doc) {
    // doc contains your user document
});
如果只需要相关的
社交
子文档,则还应使用投影操作符:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}).select('social.$').exec(function (err, doc) {
    // ...
});
您需要查询运算符。假设您已经声明了一个mongoose模型(比方说
用户
),那么类似的东西应该可以工作:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}, function (err, doc) {
    // doc contains your user document
});
如果只需要相关的
社交
子文档,则还应使用投影操作符:

Users.findOne({
    social: {
        $elemMatch: {
            service: 'twitter',
            accountId: '789DEF'
        }
    }
}).select('social.$').exec(function (err, doc) {
    // ...
});