未知的顶级运算符:$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

$in 运算符选择字段值等于指定数组中任何值的文档。要指定 $in 表达式,请使用以下原型:

演示 - 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" ]
    }
  }
})

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...