【发布时间】: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