0

I tried to get embedded documents in the table using mongodb but its not working.

dev table

[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true},{"id":3,"name":false},{"id":1,"name":true}]
}]

Query

db.dev,find({data.name:true})

Excepted output

[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true}]
}]

I got Output

[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true},{"id":3,"name":"false"},{"id":1,"name":true}]
}]

How to write the query to match the expected output. can give sample code

Jesús Ros
  • 480
  • 2
  • 6
  • 19
hari prasanth
  • 716
  • 1
  • 15
  • 35
  • @ Jesús Ros I tried this query db.test.find( {"data.name": true}, {_id: 0, data: {$elemMatch: {name: true}}});. BUt I got only single record ouput : [{ id:1 data:[{ "id":1, "name": true }] }], I need all true condition output [{ "id":1, "data":[{"id":1,"name":true},{"id":2,"name":true},,{"id":1,"name":true}] }]. How to acheive it – hari prasanth Nov 14 '19 at 05:05

1 Answers1

-1

Try out this solution

let whereClause = { "data.name":true };
    await db.dev.find(whereClause);
zshan4444
  • 380
  • 3
  • 7