简体   繁体   English

使用Spring Data将DBObject插入MongoDB

[英]Insert DBObject into MongoDB using Spring Data

I tried to insert the following DBObject into MongoDB using Spring Data: 我尝试使用Spring Data将以下DBObject插入MongoDB:

    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). 其中mongoTemplate是我的Spring模板(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. 我的JSON最终将是动态的。 So any idea how to provide these entity information dynamically ? 那么任何想法如何动态提供这些实体信息? Or is there another way to insert raw JSON into Mongodb through Spring Data ? 或者是否有另一种方法可以通过Spring Data将原始JSON插入Mongodb?

You are confusing spring-data with normal mongo persistence using the java driver. 您使用java驱动程序将spring-data与正常的mongo持久性混淆。

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. 如果你想使用java驱动程序直接将数据持久化到mongoDB,那么你将使用你所展示的BasicDBObject,除了你不会使用mongoTemaplate类来持久化而是使用MongoClient类。 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. 但是如果你试图使用spring-data来保存文档,那么你需要创建一个java类来表示你的文档(例如:Person)并使用@Document(collection =“person”)注释这个类,然后使用mongoTemplate (这是一个特定于spring-data的类来保存这个实体。这与使用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);

Another way to do this is to directly access the DBCollection object via the MongoTemplate : 另一种方法是通过MongoTemplate直接访问DBCollection对象:

DBObject company = new BasicDBObject();
...
DBCollection collection = mongoTemplate.getCollection("company");
collection.insert(company);

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM