1

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

3
  • 1
    For starters, you're querying the Follows collection on the 'userid' field, but that doesn't appear to be in your schema. Is the FollowsSchema you display in the question the entire Schema? Commented Aug 23, 2012 at 14:48
  • @shelman My bad, it is profileId in the query not userID. Commented Aug 24, 2012 at 4:46
  • What is select('followingid -_id'), I think it should be select('followingid') Commented Sep 27, 2013 at 8:40

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.