【问题标题】:Query in Sub document if main Document is not null如果主文档不为空,则在子文档中查询
【发布时间】:2020-08-20 22:17:17
【问题描述】:

我有这样的架构

product: {
    _id: mongoose.Schema.Types.ObjectId,
    client_name: { type: String, required: [true, "Client Name is Required"]},
    invoice: {
        type:{
            professional_fee: { type: Number, required: [true, "Professional Fee is Required"]},
            approved_expense: { type: Number, default: 0},
            document_expense: { type: Number, default: 0},
            other_expense: { type: Number, default: 0},
            invoice_url: {type: String, default: null},
            created_on: {type: Date, default: Date.now()},
            modified_on: {type: Date, default: Date.now()}
        },
        default: null
    },
    created_on: {type: Date, default: Date.now()},
    modified_on: {type: Date, default: Date.now()}
}

我想查找 invoice.invoice_url == null 的所有文档。

我用过这个 invoice.invoice_url = null

但它会给出一个错误。因为在某些情况下 invoice 属性为 null。

所以我只想在 invoice 属性不为 null 且 invoice_url 为 null 时从 mongo 中查找

【问题讨论】:

  • 你能在你的问题中添加你尝试过的查询吗?

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

试试下面的查询。

db.product.find(
    {"$and":[{"invoice":{"$ne":null}},
    {"invoice.invoice_url":{"$eq":null}}]}
)

【讨论】:

  • sorry it didn't work..此查询将首先查找invoice不为空的记录,然后查找invoice.invoice_url..
  • 然后在您的问题中指定您的集合、查询和预期输出。
猜你喜欢
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 2021-02-21
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多