I am newbie to Nodejs and mongodb.
I was trying to get data for the following scenario. Consider 3 schemas as follows Profile schema
var ProfileSchema = new Schema({
username: {type: String, match: /^[a-zA-Z0-9_.-]+$/, unique: true},
name: String});
Posts schema
var PostsSchema = new Schema({
profileid: {type: ObjectId, ref: 'Profile'},
message: {type: String, match: /^.{1,160}$/} });
Follows schema
var FollowSchema = new Schema({
profileid: {type: ObjectId, ref: 'Profile'},
followingid: {type: ObjectId, ref: 'Profile'}});
Which is similar to the twitter hierarchy.
To get all the posts from my followers I tried the following
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);
});
});
But it is not working. Please guide the best way to get this.
Thanks for your support
Are you not getting anything back from the initial query on the Follows collection? You're passing in a string
'500d18823e792d8814000001'
as the profileId, but MongoDB stores strings and ObjectIds differently. Since the profileid field is an ObjectId, you have to convert the string to an ObjectId before executing the query.
Try querying the Follows collection for
'profileid':ObjectId('500d18823e792d8814000001')