mercredi 30 mars 2016

Project with Match in aggregate not working after use substr in mongodb

I have face one use with mongodb.

below is my sample record.

{
    "_id" : ObjectId("56fa21da0be9b4e3328b4567"),
    "us_u_id" : "1459169911J4gPxpYQ7A",
    "us_dealer_u_id" : "1459169911J4gPxpYQ7A",
    "us_corporate_dealer_u_id" : "1459169173rgSdxVeMLa",
    "us_oem_u_id" : "1459169848CK5yOpXito",
    "us_part_number" : "E200026",
    "us_sup_part_number" : "",
    "us_alter_part_number" : "",
    "us_qty" : 0,
    "us_sale_qty" : 2,
    "us_date" : "20160326",
    "us_source_name" : "BOMAG",
    "us_source_address" : "",
    "us_source_city" : "",
    "us_source_state" : "",
    "us_zip_code" : "",
    "us_alternet_source_code" : "",
    "updated_at" : ISODate("2016-03-29T06:34:02.728Z"),
    "created_at" : ISODate("2016-03-29T06:34:02.728Z")
}

I have try to get all recored having unique date

So, I have made below query using aggregate

.aggregate(
            [
                {
                    "$match":{
                            "yearSubstring":"2016",
                            "monthSubstring":"03",
                            "us_dealer_u_id":"1459169911J4gPxpYQ7A"
                            }
                },
                {
                    "$project":
                            {
                            "yearSubstring":{"$substr":["$us_date",0,4]},
                            "monthSubstring":{"$substr":["$us_date",4,2]},
                            "daySubstring":{"$substr":["$us_date",6,2]}
                            }
                },
                {
                    "$group":
                            {
                                "_id":{"monthSubstring":"$monthSubstring",
                                        "yearSubstring":"$yearSubstring",
                                        "daySubstring":"$daySubstring"
                                    },
                                "daySubstring":{"$last":"$daySubstring"}
                            }
                },
                {"$sort":{"us_date":1}}
            ]
        )

I have try both way to pass year and month (as string and as int)

but I have get blank result.

if I'm remove month and year from condition then record came.

mostly I have try all the diff. diff. solution but result is same.

Thank in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire