0

new in mongoose here, I have a mongoose model that has property which is an array of ids, so I want to retreive a specific id in that array. My code is something like this:

async (idToRetreive, contextUserId) => {
const retreivetodoId = await UserModel.findOne({ _id: contextUserId })
                                      .select('todos -_id')
                                      .where('todos')
                                      .equals(idToRetreive);
//todos is an array of ids

const retreiveTodoObject = await TodoModel.findOne({ _id: retreivetodoId });

return retreiveTodoObject;
}

but I have no success. help?

gpbaculio
  • 5,693
  • 13
  • 60
  • 102
  • No idea without actually showing your models and preferably some sample documents and an expected result. But I cannot imagine you are actually asking anything different from [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/q/3985214/2313887) or [Querying after populate in Mongoose](https://stackoverflow.com/q/11303294/2313887). Which are both common questions with well established answers. – Neil Lunn Jul 30 '17 at 05:58
  • @NeilLunn My User Model has todos field which is an array, I want to retreive only the todoId(from the argument) in it, so I can find it in my TodoModel. sorry I am not so clear – gpbaculio Jul 30 '17 at 06:00
  • The point of the comment is to highlight that "words are failing to describe". So instead of giving us more words, please "show us the code that implements your models". Simple yes? But please actually look at the linked answers first. I cannot yet see any reason why one of those would not in fact answer your question. – Neil Lunn Jul 30 '17 at 06:03

0 Answers0