猫鼬查找检查数组中嵌套对象的空合并

Mongoose find check null coalesce on nested object in an array

我有这样一个模型:

var organization = new Schema({
  things:[
    {a:{
      b:string
      }
    }
  ]
})

我正在尝试 findOne 像这样使用 Mongoose:

let organization = await Organization.findOne({
  "things.a.b": "uniquestring",
});

但问题是a可能为空。

我想做这样的事情:

let organization = await Organization.findOne({
  "things.a?.b": "uniquestring",
});

Is there a way to check for null on this query?

我不确定这是否能回答您的问题,但您不能做这样的事情吗?如果您认为您唯一的问题是可能有一些 null 个对象

假设您的对象看起来像这样

[
  {
    "things": [
      {
        "a": {
          "b": "test"
        }
      },
      {
        "a": null
      },
      {
        "a": {
          "b": "abc"
        }
      }
    ]
  }
]

那么你可以运行这个查询

db.collection.find({
  things: {
    "$elemMatch": {
      "a.b": "test"
    }
  }
},
{
  "things.$": 1
})

你可以查看MongoDB游乐场here