You are misleading spring-data with the normal preservation of mangoes using the java driver.
If you want to transfer data to mongoDB directly using the java driver, you should use BasicDBObject, as you showed, except that you will not use the mongoTemaplate class to save, but rather the MongoClient class. So it will look like this:
MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); DB db = mongoClient.getDB( "mydb" ); BasicDBObject o = new BasicDBObject(); o.set...... coll.insert(o);
But if you are trying to save the document using spring-data, you need to create a java class to represent your document (for example: Person) and annotate this class with @Document (collection = "person") and then use mongoTemplate (which is the spring class -data to save this object This is very similar to using JPA / hibernate.
So, it will look something like this.
@Document(collection="person") public class Person { private String fisrtName; .... Relevant getters and setters }
And then perseverance
Person p = new Person(); p.setFirstName("foo"); p.setLastName("bar"); .... mongoTemplate.save(p);
Clinton bosch
source share