简体   繁体   中英

How to execute a complex MongoDB native query from Java Springboot

I have a bit of a complex query of view creation using 3 collections. The query is written in the native level. I need that query to be executed from Java and is there any way that I can execute these types of queries from Java level. Maybe a function that takes a MongoDB native query as a string and executes that on the database level

db.createView('TARGET_COLLECTION', 'SOURCE_COLLECTION_1', [
  {
    $facet: {
      SOURCE_COLLECTION_1: [
        {$match: {}},
        { $project: { "sourceId":  {$toString: "$_id"}, "name": 1, "image": "$logo" }}
      ],
      SOURCE_COLLECTION_2: [
        {$limit: 1},
        {
          $lookup: {
            from: 'SOURCE_COLLECTION_2',
            localField: '__unexistingfield',
            foreignField: '__unexistingfield',
            as: '__col2'
          }
        },
        {$unwind: '$__col2'},
        {$replaceRoot: {newRoot: '$__col2'}},
        { $project: { "sourceId": {$toString: "$_id"}, "name": 1, "image": 1 }}
      ],
      SOURCE_COLLECTION_3: [
        {$limit: 1},
        {
          $lookup: {
            from: 'SOURCE_COLLECTION_3',
            localField: '__unexistingfield',
            foreignField: '__unexistingfield',
            as: '__col2'
          }
        },
        {$unwind: '$__col2'},
        {$replaceRoot: {newRoot: '$__col2'}},
        { $project: { "sourceId":  {$toString: "$_id"}, "name": 1, "image": "$logo" }}
      ]
    },
  },
  {$project: {data: {$concatArrays: ['$SOURCE_COLLECTION_1', '$SOURCE_COLLECTION_2', '$SOURCE_COLLECTION_3']}}},
  {$unwind: '$data'},
  {$replaceRoot: {newRoot: '$data'}}
])

An example:

Consider a document in a collection:

{ _id: 1234, name: "J. Doe", colors: [ "red", "black" ] }

And the following aggregation from the mongo shell:

db.collection.agregate( [
  { $project: { _id: 0, colors: 1 } }
] )

This returns: { "colors": [ "red", "black" ] }

This can also be run with the following command:

db.runCommand( { 
  aggregate: "collection", 
  pipeline: [ { $project: { _id: 0, colors: 1 } } ],  
  cursor: { } 
} )

And, its translation using Spring Data's MongoTemplate :

String jsonCommand = "{ aggregate: 'collection', pipeline: [ { $project: { _id: 0, colors: 1 } } ], cursor: { } }";
Document resultDoc = mongoTemplate.executeCommand(jsonCommand);

The output document resultDoc has a format like the following:

{
        "cursor" : {
                "firstBatch" : [
                        {
                                "colors" : [
                                        "red",
                                        "black"
                                ]
                        }
                ],
                "id" : NumberLong(0),
                "ns" : "test.colors"
        },
        "ok" : 1
}

To know more about the db.runCommand(...) method see MongoDB documentation at: Database Commands and Database Command Aggregate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM