0

I have this aggregation that I want to transform into a Spring MongoDb aggregation. A document has several named data in a list. But I want to project only 2 datas for a couple of documents with a given ID as follow.

How to put a filter in the spring aggregation ?

db.getCollection("data").aggregate(
    [
        { 
            "$match" : { 
                "_id" : { 
                    "$in" : [
                        1, 
                        2
                    ]
                }
            }
        }, 
        { 
            "$project" : { 
                "datas" : { 
                    "$filter" : { 
                        "input" : "$datas", 
                        "as" : "item", 
                        "cond" : { 
                            "$or" : [
                                { 
                                    "$eq" : [
                                        "$$item.name", 
                                        "data1"
                                    ]
                                }, 
                                { 
                                    "$eq" : [
                                        "$$item.name", 
                                        "data2"
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        }
    ]
);
  • Could you please share some sample data and write the expected result? – Valijon Jul 25 '22 at 13:57
  • Post with using `$filter` with Spring Data MongoDB: https://stackoverflow.com/questions/64007682/how-to-use-mongotemplate-cond-and-filter-in-java-code/64009214#64009214 – prasad_ Jul 25 '22 at 13:59

0 Answers0