【发布时间】:2019-12-17 21:17:39
【问题描述】:
我是 MongoDB 新手。
我想查找与整个数组匹配的文档。
让我们向您展示集合架构。 这里是组合收集数据
{
_id: 741258,
product_id: 258,
attributes: [ { attr_id: 123, val: '26' },
{ attr_id: 456, val: 'Blue' },
{ attr_id: 789, val: 'Slim' } ],
},
{
_id: 745896,
product_id: 258,
attributes: [ { attr_id: 124, val: '28' },
{ attr_id: 484, val: 'Red' },
{ attr_id: 852, val: 'Small' } ],
},
{
_id: 985632,
product_id: 258,
attributes: [ { attr_id: 123, val: '26' },
{ attr_id: 456, val: 'Blue' } ],
},
{
_id: 456855,
product_id: 258,
attributes: [ { attr_id: 123, val: '26' } ],
}
查询:
db.combination.find( { product_id: "258" },
{ attributes: { $elemMatch: [ { attr_id: 123, val: '26' },
{ attr_id: 456, val: 'Blue' }
] } } )
错误:
(node:4460) UnhandledPromiseRejectionWarning: MongoError: $elemMatch needs an Object
预期结果:
{
_id: 985632,
product_id: 258,
attributes: [ { attr_id: 123, val: '26' },
{ attr_id: 456, val: 'Blue' } ],
},
请帮助减缓这个问题。提前致谢
【问题讨论】: