How to get embedded document in mongodb? [duplicate]

我与影子孤独终老i 提交于 2020-02-06 08:01:31

问题


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


回答1:


Try out this solution

let whereClause = { "data.name":true };
    await db.dev.find(whereClause);


来源:https://stackoverflow.com/questions/58815958/how-to-get-embedded-document-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!