未知的顶级运算符:$eq
unknown top level operator: $eq
我的数据库示例:
{
name: 'The Perks of Being a Wallflower'
genres: ['Drama', 'Romance']
rating: 8
},
{
name: 'The Edge of Seventeen'
genres: ['Drama', 'Comedy']
rating: 7.3
},
{
name: 'Little Women',
genres: ['Drama', 'Romance']
rating: 7.8
}
如果流派数组的第一个元素是 Comedy
或 Romance
.
,我想投影它
我试过了:
db.shows.find(
{},
{ genres: { $elemMatch: { $or: [{ $eq: 'Drama' }, {$eq: 'Comedy'}] } } }
);
但它给我这个错误“未知的顶级运算符:$eq”。
使用$in
The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:
演示 - https://mongoplayground.net/p/MAuWUeP9GIE
db.collection.find({
genres: {
$elemMatch: {
"$in": [ "Drama", "Comedy" ]
}
}
})
演示 - https://mongoplayground.net/p/JJJkoHuz_DZ
db.collection.find({},
{
genres: {
$elemMatch: {
"$in": [ "Drama", "Comedy" ]
}
}
})
我的数据库示例:
{
name: 'The Perks of Being a Wallflower'
genres: ['Drama', 'Romance']
rating: 8
},
{
name: 'The Edge of Seventeen'
genres: ['Drama', 'Comedy']
rating: 7.3
},
{
name: 'Little Women',
genres: ['Drama', 'Romance']
rating: 7.8
}
如果流派数组的第一个元素是 Comedy
或 Romance
.
,我想投影它
我试过了:
db.shows.find(
{},
{ genres: { $elemMatch: { $or: [{ $eq: 'Drama' }, {$eq: 'Comedy'}] } } }
);
但它给我这个错误“未知的顶级运算符:$eq”。
使用$in
The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:
演示 - https://mongoplayground.net/p/MAuWUeP9GIE
db.collection.find({
genres: {
$elemMatch: {
"$in": [ "Drama", "Comedy" ]
}
}
})
演示 - https://mongoplayground.net/p/JJJkoHuz_DZ
db.collection.find({},
{
genres: {
$elemMatch: {
"$in": [ "Drama", "Comedy" ]
}
}
})