I am trying to insert a whole Java object into a MongoDB Collection using Java. I am getting following error:
Error :
Exception in t
Just use "insertOne" method, not save.
MongoCollection collection;
String collectionName = "somename";
String jsonObject = "{}";
if (!mongoTemplate.collectionExists(collectionName)) {
collection = mongoTemplate.createCollection(collectionName);
logger.info("Collection %s was successfully created", collectionName);
} else {
collection = mongoTemplate.getCollection(collectionName);
}
collection.insertOne(Document.parse(jsonObject));