tushar
tushar

Reputation: 323

Mongoose query on array returning the complete document

I am making a node.js application where I am trying to query an array using mongoose and I am being returned the complete unfiltered document. The collection stores a single document with the following Data (This is also the data that I am receiving after executing the query):

{
    "_id": {
        "$oid": "60c0900f619f89f6943efbfd"
    },
    "milestones": [{
        "id": "1",
        "name": "5 Friends",
        "description": "refer 5 friends to get 100 GroCash!",
        "isActive": true,
        "reward": 100,
        "condition": 5
    }, {
        "id": "2",
        "name": "7 Friends",
        "description": "refer 7 friends to get 100 GroCash!",
        "isActive": true,
        "reward": 150,
        "condition": 7
    }, {
        "id": "3",
        "name": "5 Friends",
        "description": "refer 5 friends to get 100 GroCash!",
        "isActive": true,
        "reward": 550,
        "condition": 5
    }],
    "newUserReward": 50,
    "refreeReward": 50
}

The query that I am running is : incentiveModel.find({'milestones.isActive':true,'milestones.condition':5}) I am expecting it to return me the following data :

{
        "_id": {
            "$oid": "60c0900f619f89f6943efbfd"
        },
        "milestones": [{
            "id": "1",
            "name": "5 Friends",
            "description": "refer 5 friends to get 100 GroCash!",
            "isActive": true,
            "reward": 100,
            "condition": 5
        }, {
            "id": "3",
            "name": "5 Friends",
            "description": "refer 5 friends to get 100 GroCash!",
            "isActive": true,
            "reward": 550,
            "condition": 5
        }],
        "newUserReward": 50,
        "refreeReward": 50
}

I have tried $project but still no help.
The mongoose schema is :

const milestone = new Schema({
    id:{
        type:String,
        required:true,
        default:mongoose.Types.ObjectId()
    },
    name:{
        type:String,
        required:true,
        default:''
    },
    description:{
        type:String,
        required:true,
        default:''
    },
    isActive:{
        type:Boolean,
        required:true,
        default:false
    },
    condition:{
        type:Number,
        required:true,
        default:null
    },
    reward:{
        type:Number,
        required:true,
        default:0
    }
})

export const incentiveScema = new Schema({
    milestones:{
        type:[milestone]
    },
    newUserReward:{
        type:Number,
        default:0
    },
    refreeReward:{
        type:Number,
        default:0
    }

})

Thanks in advance!

Upvotes: 0

Views: 320

Answers (3)

bhaginath
bhaginath

Reputation: 456

Probably you could use aggregate with $addFields on which you can filter out the data. Below is the query for the same. for more reference please go through the this link.

check the sample example

db.collection.aggregate([
  {
    $addFields: {
      "milestones": {
        $filter: {
          input: "$milestones",
          as: "milestones",
          cond: {
            $and: [
              {
                $eq: [
                  "$$milestones.isActive",
                  true
                ]
              },
              {
                $eq: [
                  "$$milestones.condition",
                  5
                ]
              }
            ]
          }
        }
      }
    }
  }
])

Upvotes: 1

saqsham
saqsham

Reputation: 69

refer to this example below..

https://mongoplayground.net/p/Xrrby8yN0IS

Upvotes: 1

Naeem
Naeem

Reputation: 183

await  incentiveModel.aggregate([
       {
$unwind:'$milestones'
      }
{
    $match:{
       $and:[{'milestones.isActive':true},{'milestones.condition':5}]
    }

    ])

Upvotes: 1

Related Questions