1

I have the following objects array and was wondering if there is a way to filter results as return only QtyIn records or return only QtyOut records? Any hint is highly appreciated. Thanks for your help

{
    warehouseID: "1234",
    transactions : [ 
        {
            "qtyIn" : "10",
            "transDateTime" : ISODate("2019-09-10T18:54:41.983Z")
        }, 
        {
            "qtyOut" : "11",
            "transDateTime" : ISODate("2019-08-10T18:54:41.983Z")
        },
        {
            "qtyOut" : "200",
            "transDateTime" : ISODate("2019-02-10T11:54:41.983Z")
        }    
    ],
}
1
  • Within transactions ? Or as root document? Commented Apr 30, 2020 at 22:22

1 Answer 1

1

You can compare qTyIn with undefined within $filter:

db.collection.aggregate([
    {
        $addFields: {
            transactions: {
                $filter: {
                    input: "$transactions",
                    cond: {
                        $ne: [ "$$this.qtyIn", null ]
                    }
                }
            }
        }
    }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.