首页 > 解决方案 > 填充后的条件查询mongo db不返回数据

问题描述

我是 mongo 和 node 的新手,我在过滤时遇到了问题。

我有一个客户架构和钱包架构。当我插入一个新客户时,它正在为该客户填充一个钱包。这两个模型的架构如下。

客户模型.js

var Schema = mongoose.Schema;
    const Genders = Object.freeze({
        Male: 'male',
        Female: 'female',
        Other: 'other',
    });


var CustomerSchema = new Schema({
        reg_date: { type: Date, default: Date.now },
        first_name: String,
        last_name: String,
        gender: {
            type: String,
            enum: Object.values(Genders),
        },
        wallet_balance: { type: Number, default: 0 },
        status:{type:Boolean,default:true},
        wallet:{type:mongoose.Schema.Types.ObjectId,ref:'Wallet'},
        customer_rank: String
});
module.exports = mongoose.model('Customer', CustomerSchema);

Wallet.model.js

var Schema = mongoose.Schema;
var TransactionSchema = new Schema({
    reason: String,
    deposit_by: Number,
    transaction_type: String,
    transacted_balnace:Number
})
var WalletSchema = new Schema({
    user_id:String,
    transaction_log: [TransactionSchema],
    balance: { type: Number, default: 0 },
    created_at: { type: Date, default: Date.now },
    updated_at: { type: Date, default: Date.now }
});
WalletSchema.plugin(uniqueValidator);
module.exports = mongoose.model('Wallet', WalletSchema);

我想根据原因获取客户详细信息。所以,代码如下。

CustomerModel.find({}, { "password": 0 }).populate({
        path: 'wallet',
        match: { reason: { $in: [ "service charge" ] } },
        select: 'transaction_log'
    }).exec().then(data => {
        if (data) {
            res.status(200).send({ status: true, data: data })
        }
        else {
            res.send({ status: false, data: [] })
        }

    })

它没有归还钱包,但是如果我删除匹配属性它工作正常。如果我得到解决方案将非常有帮助。提前致谢。

标签: mongodbmongodb-querymongoose-schema

解决方案


推荐阅读