【问题标题】:Mongoose use array in find().where()猫鼬在 find().where() 中使用数组
【发布时间】:2015-12-27 14:34:47
【问题描述】:

我有一个字符串数组,我想将其用作在我的 mongodb 中查找文档的参数。

基本上我想做的是

// Using query builder
Person.
  find().
  where('name.last').equals('Ghost' || 'ETC' || 'ETC')....

但我不想使用一堆 or 语句,我只想传递我的字符串数组。

这可能吗?

【问题讨论】:

    标签: arrays node.js mongodb mongoose


    【解决方案1】:

    只需使用.in 方法,使用$in 运算符即可:

    Person.
      find().
      where('name.last').in(['Ghost', 'Foo', 'Bar'])
    

    引用the MongoDB docs:

    $in 运算符选择字段值所在的文档 等于指定数组中的任何值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 2019-11-11
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多