8

I tried to insert the following DBObject into MongoDB using Spring Data:

    BasicDBObject document = new BasicDBObject();
    document.put("country", "us");
    document.put("city", "NY");
    mongoTemplate.insert(document);

where mongoTemplate is my Spring template (org.springframework.data.mongodb.core.MongoTemplate).

When executing, I get:

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: No Persitent Entity information found for the class com.mongodb.BasicDBObject
at org.springframework.data.mongodb.core.MongoTemplate.determineCollectionName(MongoTemplate.java:1747)
at org.springframework.data.mongodb.core.MongoTemplate.determineEntityCollectionName(MongoTemplate.java:1732)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:658)

My JSON would be dynamic at the end. So any idea how to provide these entity information dynamically ? Or is there another way to insert raw JSON into Mongodb through Spring Data ?

Begoodpy
  • 1,018
  • 3
  • 8
  • 20

3 Answers3

14

You are confusing spring-data with normal mongo persistence using the java driver.

If you want to persist data to mongoDB directly using the java driver then you would use the BasicDBObject like you have shown except that you would not use the mongoTemaplate class to persist but rather the MongoClient class. So it would 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 persist a document using spring-data, then you need to create a java class to represent your document (eg: Person) and annotate this class with @Document(collection="person") and then use the mongoTemplate (which is a spring-data specific class to persist this entity. This is very similar to using JPA/hibernate.

So it would look something like this

@Document(collection="person")
public class Person {
    private String fisrtName;
    ....

    Relevant getters and setters

}

And then the persistence

Person p = new Person();
p.setFirstName("foo");
p.setLastName("bar");
....
mongoTemplate.save(p);
Clinton Bosch
  • 2,497
  • 4
  • 32
  • 46
  • 1
    Thanks Clinton! Then is there a way to insert data via Spring data without defining a domain object ? (especially raw json) – Begoodpy Dec 10 '13 at 16:12
  • It looks like there is a very recent feature request for this: https://jira.springsource.org/browse/DATAMONGO-771 and https://github.com/spring-projects/spring-data-mongodb/pull/79 but I am not sure when this might make it's way into production. Also worth noting that if you are using the Ubunut repo as described above then this is a version or 2 behind the latest available version. – Clinton Bosch Dec 11 '13 at 11:42
  • Thanks! so it means i cannot persist string like `mongoTemplate.insert("{\"name\":\"helloworld\"}");` since it didn't mention about the collection name? rit? – Kanagavelu Sugumar Jan 12 '17 at 18:30
4

Another way to do this is to directly access the DBCollection object via the MongoTemplate:

DBObject company = new BasicDBObject();
...
DBCollection collection = mongoTemplate.getCollection("company");
collection.insert(company);
helmy
  • 9,068
  • 3
  • 32
  • 31
1

Another way to do it

Import statements

import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;

Member Variables

  @Autowired MongoTemplate mongoTemplate;

  MongoCollection<Document> collection;
  @PostConstruct
  public void init(){
    collection = mongoTemplate.getCollection("company");
  }

And then, the method

public void insertRawData(){
    Document company = new Document(new HashMap()); // If you have to insert a hashMap 
// otherwise you can add one-by-one using company.put("foo","bar")
    collection.insertOne(company);
}
so-random-dude
  • 15,277
  • 10
  • 68
  • 113