我有与此类似的对象集合(为了这个问题的目的而简化):
_id: '6509e5f504613ddc1d92c62f',
matchingHistory: [
{
color: 'red',
shape: 'square',
size: 'large',
_id: '1509e5f504613ddc1d92c62g',
updatedAt: '2023-09-21T16:16:00.000Z'
},
{
color: 'red',
shape: 'square',
size: 'small',
_id: '2509e5f504613ddc1d92c62h',
updatedAt: '2023-09-21T16:10:00.000Z'
},
]
我想从find
查询中排除matchingHistory
中没有对象red
、square
和large
的所有文档,但所有且只有所有这些条件必须匹配.话虽如此,另外,我想忽略搜索中的特定字段,例如_id
和updatedAt
.matchingHistory
最初未设置,稍后重置时它将变为空数组,因此这些文档应包含在结果中.TLDR;我只想排除对象数组中包含确切对象(红色+正方形+大)的文档.
我try 了$not
、$nor
的多种组合,但都没有产生预期的效果.
我的最后一次try 是在下面,但问题是,它实际上也排除了"red+square+small"(因为它似乎关心"至少"一个属性匹配:
$or: [
{
matchingHistory: {
$exists: true,
$eq: [],
},
},
{
matchingHistory: {
$exists: false,
},
},
{
$and: [
{
matchingHistory.color: {
$ne: 'red',
},
},
{
matchingHistory.shape: {
$ne: 'square',
},
},
{
matchingHistory.size: {
$ne: 'large',
},
},
],
}
]