1

我是 Nodejs 和 mongodb 的新手。

我试图获取以下场景的数据。考虑以下 3 个模式配置文件模式

var ProfileSchema = new Schema({
username: {type: String, match: /^[a-zA-Z0-9_.-]+$/, unique: true},
name: String});

帖子架构

var PostsSchema = new Schema({
profileid: {type: ObjectId, ref: 'Profile'},
message: {type: String, match: /^.{1,160}$/} });

遵循架构

var FollowSchema = new Schema({
profileid: {type: ObjectId, ref: 'Profile'},
followingid: {type: ObjectId, ref: 'Profile'}});

这类似于 twitter 层次结构。

为了从我的关注者那里获取所有帖子,我尝试了以下方法

Follows.find({'profileid' : '500d18823e792d8814000001'}).select('followingid -_id').limit(20).sort({addedon: 'desc'}).execFind(function (arr,followings) {
    Posts.find({profileid: {$in: followings}}).limit(20).execFind(function (arr,data) {
        console.log(data);
    });
});

但它不起作用。请指导获得此信息的最佳方法。

谢谢你的支持

4

1 回答 1

1

您没有从关注集合的初始查询中得到任何反馈吗?你传入一个字符串

'500d18823e792d8814000001'

作为 profileId,但 MongoDB 以不同的方式存储字符串和 ObjectId。由于 profileid 字段是 ObjectId,因此您必须在执行查询之前将字符串转换为 ObjectId。

尝试查询 Follows 集合

'profileid':ObjectId('500d18823e792d8814000001')
于 2012-08-25T18:13:15.147 回答