$match in mongoose aggregate query

Preethi

I have a Schema defined as

const mongoose = require('mongoose');

const urlSchema = new mongoose.Schema({
    longURL : {
        type : String,
        required : true,
    },
    shortUrl: {
        type : String
    },
    clicks : {
        type : Number,
        default : 0
    },
    date: {type: Date, default: new Date()},

    year : {
        type : Number
    }
    
})

module.exports = mongoose.model('Url',urlSchema)

When I give default value for year in $match for the below code, I'm getting desired output.

let result = await Url.aggregate(
            [
                 {$match:{"year":2020}}, 
                
                {
                        $project:
                        {
                            month: { $month: "$date" }
                        }
        
                },
                {
                    $group : {
                        _id : "$month",
                        count : {$sum : 1}
                    }
                }
            ]
        )

output : 

[
    {
        "_id": 1,   // 1 - represent Jan
        "count": 8
    },
    {
         "_id: : 7,  // 7 - represent Jul
         "count: : 5
    }
]

But if I pass dynamic values, I'm getting empty array as output

let year = 2020;
let result = await Url.aggregate(
            [
                 {$match:{"year":year}}, 
                
                {
                        $project:
                        {
                            month: { $month: "$date" }
                        }
        
                },
                {
                    $group : {
                        _id : "$month",
                        count : {$sum : 1}
                    }
                }
            ]
        )

output: 
[]

Just started on working with mongoose and I don't find any reference why I'm getting this kind of weird result? I need count values based on year and month. Can anyone help me out of this?

Thanks for the help in advance!

Murat Colyaran

I think the problem is with the integer/string confussion.

Try this:

{$match:{"year": parseInt(year) }},

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Modify date in object for aggregate $match query in mongoose

Mongoose aggregate if _id match

mongoose aggregate $match does not match

nodejs + mongoose - query aggregate

An aggregate query with subdocuments with Mongoose

Aggregate query runs in mongodb and not in mongoose

Mongoose aggregate returns pipeline query

aggregate query in mongoose mongodb nodejs

Match Two different fields in Mongoose, Aggregate?

Mongoose aggregate match and addfield with provided list index

Mongoose aggregate query working as a mongodb query but not able to convert to mongoose

Mongodb aggregate match query with priority on full match

mongoose aggregate query to filter and sort data

Scala MongoDB aggregate group and match query

Mongo Aggregate Conditional query within $match

Match with OR clause after Unwind in MongoDB aggregate query

$merge, $match and $update in one aggregate query

MongoDB aggregate $match stage with conditional query

NodeJs, Mongoose find and populate works but aggregate, match and lookup doesn't

Mongoose Aggregate match if an Array contains any value of another Array

Mongoose aggregate

Mongoose aggregate with $in

Mongoose query populate match id of find elements

MongoDB aggregate query return document based on match query and priority values

Dynamically building an Mongoose aggregate query in Node.js

how to skip query exection if input array is empty or not exit in mongoose aggregate

How to get data from a mongoose aggregate query in nodejs

Query to show json responses which are public true inside mongoose aggregate

How can I filter query subdocuments using aggregate in Mongoose?

TOP Ranking

HotTag

Archive