2
回答
MongoDB 查询字段是列表,如何只取列表符合条件的字段输出
华为云实践训练营,热门技术免费实践!>>>   
假如数据库中记录是下面4条:
{
code: "1",
qty: [
{ size: "a", num: 10, color: "blue" },
{ size: "b", num: 100, color: "blue" },
{ size: "c", num: 100, color: "blue" },
]
},
{
code: "2",
qty: [
{ size: "a", num: 10, color: "blue" },
{ size: "e", num: 100, color: "blue" },
{ size: "f", num: 100, color: "blue" },
]
},
{
code: "3",
qty: [
{ size: "h", num: 10, color: "blue" },
{ size: "b", num: 100, color: "blue" },
{ size: "i", num: 100, color: "blue" },
]
},
{
code: "4",
qty: [
{ size: "j", num: 10, color: "blue" },
{ size: "k", num: 100, color: "blue" },
{ size: "m", num: 100, color: "blue" },
]
}
想要查询qty字段对应列表中包含size:“a”  或者包含size:“b” 的结果 期望返回结果是:
{
code: "1",
qty: [
{ size: "a", num: 10, color: "blue" },
{ size: "b", num: 100, color: "blue" },
]
},
{
code: "2",
qty: [
{ size: "a", num: 10, color: "blue" },
]
},
{
code: "3",
qty: [
{ size: "b", num: 10, color: "blue" },
]

},

请问大神 mongodb如何实现?

举报
顶部