使用MongoDB Java驱动程序将DBObject转换为POJO

MongoDB似乎返回了BSON / JSON对象。

我认为你肯定能够以字符串,整数等方式检索值,然后可以保存为POJO。

作为迭代列表的结果,我有一个DBObject(实例化为BasicDBObject)…(cur.next())。

除了使用某种持久性框架之外,唯一的方法是将数据放入POJO以使用JSON serlialiser / deserialiser吗?

我的方法看起来像这样:

public List findByEmail(String email){ DBCollection userColl; try { userColl = Dao.getDB().getCollection("users"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace();} DBCursor cur = userColl.find(); List usersWithMatchEmail = new ArrayList(); while(cur.hasNext()) { // this is where I want to convert cur.next() into a  POJO usersWithMatchEmail.add(cur.next()); } return null; } 

编辑:很明显,只是做这样的事情。

有一些java库可以帮助你:

让Spring用它已经为此建造的东西做繁重的工作……

真正的诀窍是:mongoTemplate.getConverter()。read(Foo.class,obj);

例如,使用DBCursor时 –

 while (cursor.hasNext()) { DBObject obj = cursor.next(); Foo foo = mongoTemplate.getConverter().read(Foo.class, obj); returnList.add(foo); } 

http://revelfire.com/spring-data-mongodb-convert-from-raw-query-dbobject/

虽然回答很晚,但有人可能会觉得这很有用。

我使用GSON从BasicDBObject转换为我自己的POJO,即TinyBlogDBObject

 TinyBlogDBObject obj = convertJSONToPojo(cursor.next().toString()); 

 private static TinyBlogDBObject convertJSONToPojo(String json){ Type type = new TypeToken< TinyBlogDBObject >(){}.getType(); return new Gson().fromJson(json, type); } 

您可以使用Google提供的GSON库。 这是它的例子 。 还有许多其他的api可以用来将json转换为pojo,如jettision api等。

1.为MongoDatabase bean提供适当的CodecRegistry

 @Bean public MongoDatabase mongoDatabase() { PojoCodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build(); CodecRegistry pojoCodecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(), CodecRegistries.fromProviders(pojoCodecProvider)); MongoClient mongoClient = new MongoClient("localhost", 27017); return mongoClient.getDatabase("example_db_name").withCodecRegistry(pojoCodecRegistry); } 

2.注释POJOS

 public class ProductEntity { @BsonProperty("name") public final String name; @BsonProperty("description") public final String description; @BsonProperty("thumb") public final ThumbEntity thumbEntity; @BsonCreator public ProductEntity( @BsonProperty("name") String name, @BsonProperty("description") String description, @BsonProperty("thumb") ThumbEntity thumbEntity) { this.name = name; this.description = description; this.thumbEntity = thumbEntity; } } public class ThumbEntity { @BsonProperty("width") public final Integer width; @BsonProperty("height") public final Integer height; @BsonProperty("url") public final String url; @BsonCreator public ThumbEntity( @BsonProperty("width") Integer width, @BsonProperty("height") Integer height, @BsonProperty("url") String url) { this.width = width; this.height = height; this.url = url; } } 

3.查询mongoDB并获取POJOS

 MongoCollection collection = mongoDatabase.getCollection("product"); Document query = new Document(); List products = collection.find(query, ProductEntity.class).into(new ArrayList<>()); 

请在其他post中查看我的答案
POJO to org.bson.Document和Vice Versa