2016-04-04 7 views
2

У меня есть UserКак вставить объект в документ MongoDB 3.2?

public class User { 
    private String name; 
    private String email; 

    public User() { } 

    public User(String name) { 
     this.name = name; 
    } 

    public User(String name, String email) { 
     this(name); 
     this.email = email; 
    } 
    // getters and setters 
} 

Также у меня есть простой POJO Comment

public class Comment { 
    private String comment; 
    private Date date; 
    private String author; 

    public Comment() { } 

    public Comment(String comment, Date date, String author) { 
     this.comment = comment; 
     this.date = date; 
     this.author = author; 
    } 
    // getters and setters 
} 

Как я хочу вставить новый пользователь в коллекцию с каким-то комментарии о нем, как это:

public static void main(String[] args) { 
    MongoClient client = new MongoClient(); 
    MongoDatabase db = client.getDatabase("example"); 
    MongoCollection<Document> collection = db.getCollection("object_arrays"); 

    collection.drop(); 

    List<Comment> reviews = new ArrayList<Comment>(){{ 
     add(new Comment("cool guy", new Date(), "John Doe")); 
     add(new Comment("best joker", new Date(), "Vas Negas")); 
     add(new Comment("very stupid but very funny man", new Date(), "Bill Murphy")); 
    }}; 
    Document user = new Document(); 
    user.append("user", new User("0xFF", "[email protected]")) 
      .append("reviews", reviews) 
      .append("createDate", new Date()); 
    collection.insertOne(user); 
} 

К сожалению, у меня есть Исключение:

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.course.com.mongodb.course.model.User. 
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) 
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) 
    at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51) 
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:174) 
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189) 
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131) 
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45) 
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63) 
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29) 
    at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:101) 
    at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:43) 
    at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129) 
    at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160) 
    at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:212) 
    at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101) 
    at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:67) 
    at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37) 
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159) 
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286) 
    at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:115) 
    at com.mongodb.operation.MixedBulkWriteOperation$Run$2.executeWriteCommandProtocol(MixedBulkWriteOperation.java:455) 
    at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:646) 
    at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:401) 
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:179) 
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168) 
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230) 
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221) 
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168) 
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74) 
    at com.mongodb.Mongo.execute(Mongo.java:781) 
    at com.mongodb.Mongo$2.execute(Mongo.java:764) 
    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515) 
    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:306) 
    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:297) 
    at com.mongodb.course.week3.ArrayListWithObject.main(ArrayListWithObject.java:34) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

Я понимаю, что драйвер Java для MongoDB не может преобразовать мой объект в Document и он хочет какой-то конвертер. Также я знаю около Codec, CodecRegistry и CodecProvider интерфейсов. Кстати, есть ли более простой способ конвертировать объект в документ mongo? Можете ли вы показать мне пример, как я могу это сделать? Спасибо.

ответ

1

Если вы хотите работать с объектами Java, например, Morphia - ваш лучший выбор. Сейчас выполняется работа по поддержке произвольных классов Java, как вы пытаетесь, но это еще не сделано.

2

Проблема с кодом, который вы опубликовали, заключается в том, что он по умолчанию не знает, как сериализовать ваши объекты pojo в Json, чтобы сохранить их в базе данных. Вы можете сделать это с помощью драйверов Java MongoDB, но вам нужно выполнить некоторую работу для сериализации комментариев ArrayList и User pojos. Если вы добавите Джексон код отображения вы можете сделать это следующим образом:

import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.function.Function; 
import java.util.stream.Collectors; 

import org.bson.Document; 

import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.mongodb.MongoClient; 
import com.mongodb.client.MongoCollection; 
import com.mongodb.client.MongoDatabase; 

public class Problem { 
    public static void main(String[] args) { 
     try (final MongoClient client = new MongoClient()) { 
      final MongoDatabase db = client.getDatabase("example"); 
      final MongoCollection<Document> collection = db.getCollection("object_arrays"); 

      collection.drop(); 

      final List<Comment> reviews = new ArrayList<Comment>() { 
       { 
        add(new Comment("cool guy", new Date(), "John Doe")); 
        add(new Comment("best joker", new Date(), "Vas Negas")); 
        add(new Comment("very stupid but very funny man", new Date(), "Bill Murphy")); 
       } 
      }; 

      final ObjectMapper mapper = new ObjectMapper(); 
      final User user = new User("0xFF", "[email protected]"); 
      try { 
       //Create a Document representation of the User object 
       final String userJson = mapper.writeValueAsString(user); 
       final Document userDoc = Document.parse(userJson); 

       //Convert the review ArrayList into a Mongo Document. Need to amend this if not using Java8 
       final List<Document> reviewDocs = reviews.stream().map(convertToJson()) 
         .map(reviewJson -> Document.parse(reviewJson)).collect(Collectors.toList()); 

       //Wrap it all up to it can be saved to the database 
       final Document wrapperDoc = new Document(); 
       wrapperDoc.append("user", userDoc).append("reviews", reviewDocs).append("createDate", new Date()); 
       collection.insertOne(wrapperDoc); 
      } catch (final JsonProcessingException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    private static Function<Comment, String> convertToJson() { 
     final ObjectMapper mapper = new ObjectMapper(); 
     return review -> { 
      try { 
       return mapper.writeValueAsString(review); 
      } catch (final JsonProcessingException e) { 
       e.printStackTrace(); 
      } 
      return ""; 
     }; 
    } 
} 

* это использует некоторый Java8 код, который вы, возможно, потребуется изменить в зависимости от того, какая версия Java вы используете

В другой ответ на этот вопрос говорит, что существуют рамки, которые могут сочетать сериализацию объектов и взаимодействие с MongoDB, так что вам не нужно вручную запускать код сериализации. Например, у Spring есть монгольский водитель, и я использовал еще один под названием Jongo, который я считаю неплохим.

Смежные вопросы