1

In my Spring boot project have a Document like so:

@Document(collection="AuditTable")
public class AuditTable {

    @Id
    private String id;

    private Map<String, String> properties;

where properties is a dynamic field i.e. it can take in as many different key-value pairs.

I use MongoRepository to store this value:

@Repository
public interface AuditTableRepo extends MongoRepository<AuditTable, String> {
}

Now when I store it in the Collection it looks like this:

enter image description here

whereas I want it to look like this:

"_id": "XYZ" 
"_class": "XYZ" 
"workCaseId":"12" 
"taskName":"AUDIT" 
"owner":"ANSHU" 
"createdDate":"XYZ"

Any idea about how I can fix this without using converters? Or if I do have to use them how should I do it?

I'm new to spring data mongodb as we have recently made the jump to mongo from Oracle.

Anshuman Tripathy
  • 113
  • 1
  • 3
  • 12

1 Answers1

0

If you are using the latest mongo version then you can use $replaceRoot and $mergeObjects (reference from stackoverflow answer)

let pipeline = [
    {
        "$replaceRoot":{
            "newRoot":{
                "$mergeObjects":[
                    {
                        "id":"$id"
                    },
                    "$properties"
                ]
            }
        }
    }
]
db.collection.aggregate(pipeline)
Jaap
  • 641
  • 12
  • 19
Nishant Bhardwaz
  • 924
  • 8
  • 21