0

I want to find a sub-document using Mongoose hook without 'id';

const child = await parent.child.id(id);

This code is pretty good but i want to find without id (ex: I want to find child match name) Edit:

My code:

const parent = await Parent.findOne({name: 'Parent'});
const child = await parent.child.id('5b0068b89f41693d2a71866d');

I got child.value // =1

My data:

{  
   "_id":"5b0068b89f41693d2a718664",
   "name":"Parent",
   "child": [  
      {  
         "_id":"5b0068b89f41693d2a71866d",
         "name":"foo",
         "value": 1,
      },
      {  
         "_id":"5b0068b89f41693d2a71866d",
         "name":"bar",
         "value": 2,
      },
      {  
         "_id":"5b0068b89f41693d2a71866d",
         "name":"foobar",
         "value": 3,
      }
   ]
}

But I want to get value of foo. -> using name not _id

Thank you!

Trọng Nguyễn Công
  • 1,207
  • 1
  • 10
  • 23

0 Answers0