It is quite interesting and thought that I would share. I just figured out a solution for this using the BatScream comment above:
You must create an object and paste it into your MongoDB:
Animal animal = new Animal(); animal.setName(name); animal.setCat(cat); mongoTemplate.insert(animal);
Your animal class will look using getters and settings for all fields:
public class Animal { @Id @JsonProperty private String id; @JsonProperty private String name; @JsonProperty private String cat; public String getId() { return id; } }
AFTER you have nested a tab in mongoTemplate.insert(animal); , you can actually call the animal.getId() method, and it will return the ObjectId that was created.
Simon
source share