Rahal
Rahal

Reputation: 119

mongoose find by ObjectId

I'm defining a mongoose schema like this

var accountPostSchema = new mongoose.Schema({
  account: {
    id: { type: mongoose.Schema.Types.ObjectId, ref: 'Account' }
  },
  post: { 
   id: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' }
  }
});

app.db.model('AccountPost', accountPostSchema);

When a user(account holder) create a post, I save the post in a Post schema and get the 'postId'. Then I save the 'postId' and the 'accountId' in the above accountPostSchema like this

var fieldsToSet = {
  post: {
    id: postId
  },
  account: {
    id: accountId
  }
};

db.models.AccountPost.create(fieldsToSet, function(err, accountPost) {
  if (err) {
    // handle error
  }

  // handle success
});

After entering few postId's and accountId's, I see the following results in the mongo shell

> db.accountposts.find({})
{ "_id" : ObjectId("5835096d63efc04da96eb71e"), "post" : { "id" : ObjectId("5835096d63efc04da96eb71d") }, "account" : { "id" : ObjectId("5833c920c868d7264111da69") }, "__v" : 0 }
{ "_id" : ObjectId("583509e12052c7a2a93c4027"), "post" : { "id" : ObjectId("583509e12052c7a2a93c4026") }, "account" : { "id" : ObjectId("5833c920c868d7264111da69") }, "__v" : 0 }

Now how do I find all the matching 'Posts' given an accountId? (not the postId's)

For example if I the accountId is 583509e12052c7a2a93c4026, I need to find Posts with Post._id=5835096d63efc04da96eb71d and Post._id=583509e12052c7a2a93c4026

What is the query I should run to get the matching Posts?

Upvotes: 5

Views: 20450

Answers (2)

c1moore
c1moore

Reputation: 1867

First, I would suggest changing your Schema to the following

var accountPostSchema = new mongoose.Schema({
  account: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Account'
  },
  post: { 
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Post'
  }
});

This actually makes more sense, especially when you try to populate the subdocuments. Actually, I would say this Schema is useless. Why don't you define your Post schema like the following?

var PostSchema = new mongoose.Schema({
  poster: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Account'
  },
  message: String
});

If you use the latter code, you could execute the following query to get all posts by a particular user:

db.posts.find({poster: accountId}, function(dbErr, userPosts) {
  if(dbErr) {
    // Handle the error.
  }

  // Do something with the posts by accountId in the array userPosts.
});

The advantages of removing the id field from poster becomes clear once you try to populate poster. If you defined poster as an object with the field id and try to populate it, you will need to access data about the poster as such:

posterName = retrievedPost.poster.id.name;

Alternatively, by just making the poster field an ObjectId directly, you can access the populated user more directly:

posterName = retrievedPost.poster.name;

Upvotes: 3

Manish Singh
Manish Singh

Reputation: 538

I think, you should follow this way to get all the posts associated with particular accountid.

db.accountposts.find({'account.id' : accountId})
.populate('post.id')
.exec();

Upvotes: 5

Related Questions