Convert following into native query

db.col1.aggregate([
  {
    "$unionWith": 
      {coll: "col2"}
  },
  {
    "$group": {
      "_id": {
        "Name": "$Name",
        "startTime": {
          "$arrayElemAt": [
            {
              $split: [
                "$startTime",
                "T",
                
              ]
            },
            1
          ]
        },
        "endTime": "$endTime"
      },
      "count": {
        "$sum": 1
      },
      "doc": {
        "$first": "$ROOT"
      }
    }
  },
  {
    "$match": {
      "$expr": {
        "$gt": [
          "$count",
          1
        ]
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$doc"
    }
  }
  
])

First, please read Formatting code and log snippets in posts and edit you aggregation so it is easier to read.

Second, what do you mean by native query? As it is, for me, the aggregation is json and is as native as can be. Other forms would be driver and language specific.

Thanks steevej …I will edit the question.

Actually …I want to use this in springboot…as I know to use mongodb query , we need. to use native query.

Hi @Vartika_Malguri,

I believe “native query” in the Spring context usually refers to a SQL query rather than a higher level abstraction.

Queries using the MongoDB API provide access to the full range of features and there is no direct equivalent of writing native SQL statements.

If your reference to native is meant to describe Spring Data syntax, I would refer to the Spring Data documentation for examples: Aggregation Support.

If that isn’t the information you’re looking for, please provide an example of what a native query might look like.

Regards,
Stennie

Thanks @Stennie.
You just cleared my concept of native query.
I was just looking at the syntax of spring data, where. I can write the above query.
The documentation you provided above is really very helpfull.
The only problem is, I’m new to this and I’m unable to fully convert the above query into spring data syntax, Can you please help me with that.
I will be highly obliged.
Thanks again for your support.
Regards
Vartika Malguri

Hi @Vartika_Malguri,

Spring Data has syntax helpers for aggregation stages and operators. It looks like $unionWith has been supported by UnionWithOperation since Spring Data MongoDB 3.1 although this is not currently documented in the list of Spring Data’s Supported Aggregation Operators. That seems to be an oversight and I have reported as spring-data-mongodb #3938 in the GitHub issue queue.

I currently don’t have a Spring Data environment set up to test the exact translation of your query (or time to do so), but the Spring documentation includes some very helpful Aggregation Framework Examples which would be a good starting point.

If you get stuck trying to create your Spring Data query, please post a code snippet with any compilation/error message received in the community forums and someone with more experience may be able to help out.

Regards,
Stennie

2 Likes