1

I'm forced to use the aggregation framework and the project operation of Spring Data MongoDb.

What I'd like to do is creating an array of object as a result of a project operation.

For example, considering this intermediate aggregation result:

[
  {
    "content": {
      "processes": [
        {
          "id": "101a",
          "title": "delivery"
        },
        {
          "id": "101b",
          "title": "feedback"
        }
      ]
    }
  }
]

What I want to obtain is this:

[  
  {
    "results":
    {
      "titles": [
        {
          "id": "101a",
          "value": "delivery"
        },
        {
          "id": "101b",
          "value": "feedback"
        }
      ]
    }
  }
]

This was just an example, I don't want to simply "rename" some fields. What I want is the possibility to create an array of objects.

If I try something like this:

   projectionOperation
     .and("$content.processes.id").as("results.titles.id")
     .and("$content.processes.title").as("results.titles.value");

I obtain this:

[  
  {
    "results":
    {
      "titles": {
          "id": ["101a", "101b"]
          "value": ["delivery", "feedback"]
        }
      }
    }
  }
]

With this projection the array is created, but not "in the proper position".

However, If I use the nested operator, I haven't figure out a way to specify that I want to create an array instead of an object. With this projection:

projectionOperation.and("results.titles")
         .nested(
              bind("id", "process.id")
                 .and("value", "process.title")
         );

I can create a proper nested object but not into an array:

"results.titles": {
            "id": "101b",
            "value": "feedback"
        }
4
  • 1
    Can you show us the java code that you have tried ? Commented Aug 17, 2018 at 13:18
  • @Veeram I've updated the question Commented Aug 20, 2018 at 9:30
  • I've tried to use arrayOf(), projectOperation.and(arrayOf("results.titles")).nested(bind(...)), without any luck Commented Aug 20, 2018 at 9:43
  • Hi @Veeram , I've added a similar question. I would appreciate a lot if you could take a look. It's stackoverflow.com/questions/52492829/… Commented Sep 25, 2018 at 15:18

1 Answer 1

2
+50

You can try below aggregation code.

ProjectionOperation po = Aggregation.project().and(
   VariableOperators.mapItemsOf("content.processes").as("rt")
    .andApply(
      new AggregationExpression() {
       @Override
       public Document toDocument(AggregationOperationContext aggregationOperationContext) {
             return new Document("id", "$$rt.id").append("value", "$$rt.title");
       }
    } 
  )
).as("result");
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.