Home > OS >  How to find document in mongoose?
How to find document in mongoose?

Time:12-07

How i can find certain userId in mongoose? I have this code:

{
   _id: String,
   tagName: 'git',
   users: [
       {userId: 1, count: 1}
       {userId: 234232342, count: 1}
   ]
}

I tried to find with

const checkUser = await Tag.findOne({
        tagName: params.tag],
        users: { $in: { userId: params.userId } },
      });

CodePudding user response:

Assuming params.userId holds a single value, you can simply do:

const checkUser = await Tag.findOne({
   "users.userId": params.userId
});

I've omitted tagName as you did not include it in your schema ..

  • Related