Mongoose:在一个JSON对象中使用$ in操作符
我是Nodejs和mongodb的新手。
我正在尝试为以下情况获取数据。 考虑3个模式如下Profile模式
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层次结构类似。
要获得我的追随者的所有post,我尝试了以下
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); }); });
但它不工作。 请指导最好的方式来得到这个。
感谢您的支持
Follows集合上的初始查询是否没有得到任何回复? 你正在传递一个string
'500d18823e792d8814000001'
作为profileId,但MongoDB以不同的方式存储string和ObjectIds。 由于profileid字段是ObjectId,因此必须在执行查询之前将string转换为ObjectId。
尝试查询Follows集合
'profileid':ObjectId('500d18823e792d8814000001')