如何在单个mongoDB查询中两次使用$ match运算符?

问题描述 投票:0回答:1

我有两种型号:

const ClientRequest = new mongoose.Schema({
   sourceLanguage: {
        type: String,
        default: '',
        trim: true
    },
    type: {
        type: String,
        default: '',
        trim: true
    },
    customer: {
        type: Schema.Types.ObjectId, ref: 'Clients'
    }
}

const Client = new mongoose.Schema({
    name: {
        type: String,
        default: '',
        trim: true
    },
    web: {
        type: String,
        default: '',
        trim: true
    },
    country: {
        type: String,
        default: '',
        trim: true
    }
}

并且我需要找到由requestssourceLanguage过滤的所有name。我正在使用此查询:

const requests = await ClientRequest.aggregate([
            {$match: {
                "sourceLanguage.symbol": "EN-GB"}
            },
            {
                $lookup: {
                    from: "clients",
                    localField: "customer",
                    foreignField: "_id",
                    as: "clients"
                }
            },
            {
                $match: {
                    "clients.name": filters.clientFilter,
                }
            }
        ])

但是它返回空数组。如果删除$match之一,它将起作用。但是如何在单个查询中同时使用两个过滤器?

mongodb mongoose
1个回答
0
投票
const requests = await ClientRequest.aggregate([ {$match: { "sourceLanguage": "EN-GB", "customer": ObjectId("5d933c4b8dd2942a17fca425") } }, { $lookup: { from: "clients", localField: "customer", foreignField: "_id", as: "clients" } }, ])
© www.soinside.com 2019 - 2024. All rights reserved.