Java query from shell script

Hi,

How to translate this shell script to Java ?

db.probability.aggregate(
  [
     {
       $unwind: “ $probability”
     },
    {
      $group: {
        _id: "$experimentId",
        "probabilityArr": { $push: "$probability" }
      }
    },
    {
      $project: {
        "description": 1,
        "results": {
          $reduce: {
            input: "$probabilityArr",
            initialValue: [],
            in: { $concatArray: [ "$$value", "$$this" ] }
          }
        }
      }
    }
  ]
)

I will not answer directly to your question.

Here is what I do with pipelines and queries.

  1. I do not used the builders.
  2. I keep them in a resource file as a json document.
  3. I load them at run time.
  4. I use Document.parse() and then use the usual collection API.

This way

  1. I can change a pipeline or query without recompiling.
  2. I can debug the pipeline with the shell or compass.
  3. Store back the result in my resource file.

Thank you very much for your quick response :slight_smile:
I am very new to MongoDB. I have used POJO to store in the database. Since the document model is a nested one and that there could be thousands list items in the child array field I have a question: wouldn’t it be very slow to use ObjectMapper to convert Document to POJO ? It would also slow down the process to read the stored result from the resource file?

My intention is to build on this block of java code to make the query more efficient(to use unwind and reduce):

          Bson match = Aggregates.match(
                 Filters.eq("country._id", countryId)
         );

        Bson sort = Aggregates.sort(
                Indexes.descending("time", "region._id")
        );

        Bson group = Aggregates.group(
                "region._id",
                Accumulators.max("time", "$time"),
                Accumulators.first("contentList", "$contentList")
        );

        List<Data> dataList = dataMongoCollection.aggregate(
                Arrays.asList(
                        match,
                        group
                )
        ).into(new ArrayList<>());

But I found out that it’s quite slow to run the Java query. Is there anyway to improve my existing Java query ? How can I skip sort but still get the document with highest value of time for a certain region._id ?

Thank you very much in advance :slight_smile:

I did not express the following correctly.

I store back the debugged pipeline, the result of the debug session. The result of running the pipeline is always process the normal way. There is no intermediary step involve to process the aggregation result.

Thank you very much for your answer :slight_smile:

I guess that my very limited knowledge in MongoDB makes it difficult for me to understand everything that you wrote. Do I need to remap the result to my POJO model in Java ? You see, if I use the Java query I get a list of my POJO directly.

Do you have a solution regarding my question about the query in Java ?

You do not change your POJO model at all.

I saw your other post. If I have something I will write it over there.

thank you :slight_smile: I thought that query deserves as an own topic :slight_smile:

1 Like