Mongoose:对JSON对象使用$in运算符
我是Nodejs和mongodb的新手 我试图获取以下场景的数据。考虑以下3种模式 配置文件模式Mongoose:对JSON对象使用$in运算符,json,node.js,mongodb,mongoose,Json,Node.js,Mongodb,Mongoose,我是Nodejs和mongodb的新手 我试图获取以下场景的数据。考虑以下3种模式 配置文件模式 var ProfileSchema = new Schema({ username: {type: String, match: /^[a-zA-Z0-9_.-]+$/, unique: true}, name: String}); Posts模式 var PostsSchema = new Schema({ profileid: {type: ObjectId, ref: 'Profile'},
var ProfileSchema = new Schema({
username: {type: String, match: /^[a-zA-Z0-9_.-]+$/, unique: true},
name: String});
Posts模式
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);
});
});
但它不起作用。
请指导获取此信息的最佳方法
感谢您的支持您是否没有从以下集合的初始查询中获得任何信息?你在传递一个字符串
'500d18823e792d8814000001'
作为profileId,但是MongoDB以不同的方式存储字符串和objectid。因为profileid字段是ObjectId,所以在执行查询之前必须将字符串转换为ObjectId
尝试查询以下集合以获取
'profileid':ObjectId('500d18823e792d8814000001')
您是否没有从以下集合的初始查询中获得任何信息?你在传递一个字符串
'500d18823e792d8814000001'
作为profileId,但是MongoDB以不同的方式存储字符串和objectid。因为profileid字段是ObjectId,所以在执行查询之前必须将字符串转换为ObjectId
尝试查询以下集合以获取
'profileid':ObjectId('500d18823e792d8814000001')
首先,您在“userid”字段上查询followers集合,但它似乎不在您的模式中。您在问题中显示的FollowsSchema是整个模式吗?@shelman My bad,它是查询中的profileId而不是userID。什么是
select('followsId-\u id')
,我认为它应该是select('followsId')
,首先,您在“userID”字段上查询followsed集合,但这似乎不在您的模式中。您在问题中显示的FollowsSchema是整个架构吗?@shelman My bad,它是查询中的profileId而不是userID。什么是select('followsId-\u id')
,我认为应该是select('followsId')