我是 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);
});
});
但是它不起作用。 请指导获得此信息的最佳方法。
感谢您的支持
最佳答案
您没有从对 Follows 集合的初始查询中得到任何返回值吗?您正在传递一个字符串
'500d18823e792d8814000001'
作为profileId,但是MongoDB以不同的方式存储字符串和ObjectId。由于profileid字段是ObjectId,因此在执行查询之前必须将字符串转换为ObjectId。
尝试查询关注集合
'profileid':ObjectId('500d18823e792d8814000001')
关于json - Mongoose:将 $in 运算符与 JSON 对象一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093802/