Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MogoDB subdocument filteration

  {
        "_id" : ObjectId("5a4e43edb85ed11cd4dcba45"),
        "email" : "[email protected]",
        "username" : "alpesh",
        "subscriptions" : [ 
            {
                "sub_id" : "5a4df654b9799b79147f9361",
                "activation_date" : ISODate("2017-12-19T18:30:00.000Z"),
                "expiry_date" : ISODate("2018-01-19T18:30:00.000Z")
            }, 
            {
                "sub_id" : "5a4df654b9799b79147f9361",
                "activation_date" : ISODate("2018-01-19T18:30:00.000Z"),
                "expiry_date" : ISODate("2018-02-19T18:30:00.000Z")
            }, 
            {
                "sub_id" : "5a51a925ddc5003b68cc38b3",
                "activation_date" : ISODate("2018-02-19T18:30:00.000Z"),
                "expiry_date" : ISODate("2018-03-22T18:30:00.000Z")
            }
        ]
    }

i have tried this.. db.find({"subscriptions.sub_id" : "5a4df654b9799b79147f9361" }); it returns ..

{
    "_id" : ObjectId("5a4e43edb85ed11cd4dcba45"),
    "email" : "[email protected]",
    "username" : "alpesh",
    "subscriptions" : [ 
        {
            "sub_id" : "5a4df654b9799b79147f9361",
            "activation_date" : ISODate("2017-12-19T18:30:00.000Z"),
            "expiry_date" : ISODate("2018-01-19T18:30:00.000Z")
        }, 
        {
            "sub_id" : "5a4df654b9799b79147f9361",
            "activation_date" : ISODate("2018-01-19T18:30:00.000Z"),
            "expiry_date" : ISODate("2018-02-19T18:30:00.000Z")
        }, 
        {
            "sub_id" : "5a51a925ddc5003b68cc38b3",
            "activation_date" : ISODate("2018-02-19T18:30:00.000Z"),
            "expiry_date" : ISODate("2018-03-22T18:30:00.000Z")
        }
    ]
}

i have also tried $aggregate , $unwind , $filter , $projection and many ways but none of them returns as expected... i want all the matching subdocuments...like this....

   {
        "_id" : ObjectId("5a4e43edb85ed11cd4dcba45"),
        "email" : "[email protected]",
        "username" : "alpesh",
        "subscriptions" : [ 
            {
                "sub_id" : "5a4df654b9799b79147f9361",
                "activation_date" : ISODate("2017-12-19T18:30:00.000Z"),
                "expiry_date" : ISODate("2018-01-19T18:30:00.000Z")
            }, 
            {
                "sub_id" : "5a4df654b9799b79147f9361",
                "activation_date" : ISODate("2018-01-19T18:30:00.000Z"),
                "expiry_date" : ISODate("2018-02-19T18:30:00.000Z")
            }
        ]
    }
like image 779
AlpeshVasani Avatar asked Dec 04 '25 14:12

AlpeshVasani


1 Answers

db.collection('gyms').aggregate([
    {
        $match: {
            subscriptions: { 
                $elemMatch: { sub_id: "5a4df654b9799b79147f9361" }
            }
        }
    },
    {
        $redact: {
            $cond: {
                if: {
                    $or: [
                        { $eq: ["$sub_id", "5a4df654b9799b79147f9361" ] },
                        { $not: "$sub_id" }
                    ]
                },
                then: "$$DESCEND",
                else: "$$PRUNE"
            }
        }
    }
])

this is generating as expected within a single doucment without unnecessary sub documents.

{
    "email": "[email protected]",
    "username": "alpesh",
    "subscriptions": [
        {
            "sub_id": "5a4df654b9799b79147f9361",
            "activation_date": "2017-12-19T18:30:00.000Z",
            "expiry_date": "2018-01-19T18:30:00.000Z"
        },
        {
            "sub_id": "5a4df654b9799b79147f9361",
            "activation_date": "2018-01-19T18:30:00.000Z",
            "expiry_date": "2018-02-19T18:30:00.000Z"
        }
    ]
}
like image 119
AlpeshVasani Avatar answered Dec 07 '25 09:12

AlpeshVasani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!