0

I am new in mongo and node and I am facing a problem in filtering.

I have a customer schema and wallet schema. When I am inserting a new customer it is populating a wallet for that customer. Schema of this two model is below.

Customer.model.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);

I want to get customer details on basis of reason. So, the code is below.

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: [] })
        }

    })

It is not returning the wallet, But if I remove the match property it is working fine.It will be very helpful if I get a solution. Thanks in advance.

Sandip Nag
  • 968
  • 2
  • 18
  • 34
  • Your match condition should be `match: { "transaction_log.reason": { $in: [ "service charge" ] } }` – Ashh Nov 30 '18 at 07:39
  • Thanks @AnthonyWinzlet. it is working now but all the data is returning, I mean the data whose reason is not "service charge " is also coming. – Sandip Nag Nov 30 '18 at 07:44
  • Because `match` inside populate only restricts the referenced document not the ROOT document you have to use some aggregation here. Check [this](https://stackoverflow.com/questions/11303294/querying-after-populate-in-mongoose) – Ashh Nov 30 '18 at 07:49
  • can you change "service charge" to "\"service charge\"" – Shiva Nov 30 '18 at 08:09
  • Does not work "\"service charge\"" @secretsuperstar. no data is coming. – Sandip Nag Nov 30 '18 at 08:12
  • You should start with `wallet` model and find then find the `users`. Just as I explained [here](https://stackoverflow.com/questions/53551680/how-to-use-regex-search-in-referenced-field-in-mongodb/53551874#53551874) – Ashh Nov 30 '18 at 17:02
  • Please let me know if you need any help – Ashh Dec 03 '18 at 02:45
  • I have created a model of users where hobbies is an array of documents and each an every document has "name" field. If i want to how the hobby of a particular value "sports" then the code below works:- db.getCollection('users').aggregate([ {$match: {'hobbies.name': 'Sports'}}, {$project: { my_hobbies: {$filter: { input: '$hobbies', as: 'myhobbies', cond: {$eq: ['$$myhobbies.name', 'Sports']} }}, _id: 1, name:1 }} ]) But is this possible to make aggregate on populated data? – Sandip Nag Dec 03 '18 at 05:56
  • Edit your question and explain what do you want. And use `@` in comments to notify me. Else I would not be notified. – Ashh Dec 03 '18 at 19:05

0 Answers0