查询对象 mongoose 的嵌套数组

Ind*_*j26 5 mongoose mongodb mongodb-query

我想在嵌套对象游乐场中使用 Alexa 查找名称:https://mongoplayground.net/p/rqYQtf0liaX

[
  {
    item: "journal",
    instock: [
      {
        warehouse: "A",
        qty: 5,
        items: null
      },
      {
        warehouse: "C",
        qty: 15,
        items: [
          {
            name: "alexa",
            age: 26
          },
          {
            name: "Shawn",
            age: 26
          }
        ]
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

到目前为止我已经尝试过,这返回没有找到文档

db.collection.find({
  "instock.items": {
    $elemMatch: {
      name: "Alexa"
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

Gib*_*bbs 10

Mongo 区分大小写。

db.collection.find({
  "instock.items": {
    $elemMatch: {
      name: "alexa"
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

如果您想不区分大小写,请使用regex$text。有了$regex,就可以用了$elemMatch

db.collection.find({
  "instock.items": {
    $elemMatch: {
      name: {
        $regex: "alexa",
        $options: "i"
      }
    }
  }
})
Run Code Online (Sandbox Code Playgroud)