简体   繁体   中英

Query in Sub document if main Document is not null

I have a schema like this

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()}
}

I want to find all documents where invoice.invoice_url == null.

i have used this invoice.invoice_url = null

but it will give an error. because in some cases invoice property is null.

So I want to find from mongo only when invoice property is not null and then invoice_url is null

Try the below query.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM