1

I have an array inside of document.

{
    "id" : "id_1",
    "name" : "name_1";
    "additionalData" : [
        {
             "additionalDataId" : "id_1_1",
             "additionalDataName" : "name_1_1",
             "longText" : "A long story about..."  
        },
        {
             "additionalDataId" : "id_1_2",
             "additionalDataName" : "name_1_2",
             "longText" : "A longer story about danger..."  
        },
        {
             "additionalDataId" : "id_1_3",
             "additionalDataName" : "name_1_3",
             "longText" : "A longer story about danger and courage"  
        },
    ]       
}

To retrieve element of array with name "name_1_2", I use mongo query.

db.collection.find( { name: "name_1"},
        { _id: 0, additionalData: { $elemMatch: { "additionalDataName": "name_1_2" } } 
    })

How to do the same using mongoTemplate?

I have tried

Aggregation aggregation = Aggregation.newAggregation(
    Aggregation.match(
            Criteria.where("name").is("name_1").and("additionalData.additionalDataName").is("name_1_2")
        ),
    Aggregation.project("additionalData"),

); mongoTemplate.aggregate(aggregation, "CustmObjects", Object.class);

And I also tried to use ArrayOperators.Filter.filter, I used this answer.

Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(Criteria.where("name").is("name_1")),
        Aggregation.project("additionalData").and(
                ArrayOperators.Filter.filter("additionalData").as("item")
                        .by(ComparisonOperators.valueOf("item.additionalDataName").equalTo("name_1_2"))
        )
    );
mongoTemplate.aggregate(aggregation, "CustmObjects", Object.class);

https://stackoverflow.com/a/46769125/4587961

Anyway, the result I got, all elements of the array. Please, help!

4
  • 1
    $elemMatch projection is not available in aggregate(). Use a .find variant instead, or $filter just as demonstrated in the answers you link to. Commented Mar 5, 2019 at 23:47
  • I will write in 9 hours. Not at work now. It did not work with Aggregate and ArrayOperators.Filter Commented Mar 5, 2019 at 23:51
  • Okay, better to actually show what you tried so we can point out what exactly you did wrong anyway. Though I thought I did previously link you to a spring-mongo implementation of exactly that $filter usage. So a bit unsure what part you did not get right. Either that or I linked the wrong example :( Commented Mar 5, 2019 at 23:54
  • @NeilLunn I updated. Commented Mar 6, 2019 at 9:01

1 Answer 1

1

If I understood the question correctly, this will give the desired results.

Query query = new Query(new Criteria().andOperator(
    Criteria.where("name").is("name_1"),
    Criteria.where("additionalData.additionalDataName").is("name_1_2")
));
query.fields().include("additionalData").exclude("_id");

List<Document> results = template.find(query, collectionName, org.bson.Document.class);
Sign up to request clarification or add additional context in comments.

1 Comment

This is giving a list but question is to fetch this object - { "additionalDataId" : "id_1_2", "additionalDataName" : "name_1_2", "longText" : "A longer story about danger..." }

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.