简体   繁体   English

MongoDB Java驱动程序Jackson映射器MongoJack

[英]MongoDB Java Driver Jackson Mapper MongoJack

I am looking to find the most industry standard way to achieve the following. 我正在寻找实现以下目标的最行业标准方法。 I am writing a Java application which will manage documents in a MongoDB. 我正在编写一个Java应用程序,它将在MongoDB中管理文档。 Everything I have read so far points to using Mongo Java driver (3.0) for connecting to the Database then using Jackson Object Mapper to map the JSON to a Class for easy manipulation. 到目前为止,我所读的一切都指向使用Mongo Java驱动程序(3.0)连接到数据库,然后使用Jackson Object Mapper将JSON映射到Class以便于操作。 Then using json.org: 然后使用json.org:

JSONObject json = new JSONObject(mapper.writeValueAsString(user));

To get it back into Mongo. 让它回到Mongo。

 Document doc1 = Document.parse(json.toString());
 collection.insertOne(doc1)

So with the new 3.0 driver things have changed a little as DBObject is no longer recommended. 因此,使用新的3.0驱动程序后,情况发生了一些变化,因为不再建议使用DBObject。

So what is the best way to get documents from Mongo edit and update them and then save the updated document. 那么从Mongo获取文档的最佳方法是编辑并更新它们,然后保存更新的文档。

MongoCollection<Document> collection = database.getCollection("mycoll");

Does: 请问:

MongoCollection<BasicDBObject> collection = database.getCollection("mycoll", BasicDBObject.class);

MongoCollection<MyObject> collection = database.getCollection("mycoll", MyObject.class);

Does the MyObject ability remove the need to using object mapping ? MyObject功能是否消除了使用对象映射的需要?

As you can see I'm a little confused now and any help to straighten out the best was to do this for Driver 3.0 > would be great. 如您所见,我现在有些困惑,要弄清最好的任何帮助就是针对Driver 3.0>这样做会很棒。

The latest MongoJack currently available (2.5.1 from November 2015) still uses the now deprecated DBCollection (now, as of the MongoDb driver 3.0). 当前可用的最新MongoJack(从2015年11月开始为2.5.1)仍使用现已弃用的DBCollection(从MongoDb驱动程序3.0开始)。

So you could still do this if you wanted: 因此,您仍然可以根据需要执行以下操作:

MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB("databaseName");
DBCollection coll = db.getCollection("my.collection");
JacksonDBCollection<MyDomainObject, String> wrapped =
  JacksonDBCollection.wrap(coll, MyDomainObject.class, String.class);

In your question, you bring up the Codec registry capability of the MongoDb driver 3.0 and essentially ask, is MongoJack still relevant since the native driver has this capability built-in? 在您的问题中,您提出了MongoDb驱动程序3.0的编解码器注册表功能,并且本质上是在问,由于本机驱动程序内置了此功能,MongoJack仍然有用吗?

I have not written a codec yet, but it seems to require some fair amount of boiler plate code - a far cry from the simplicity of MongoJack. 我还没有编写编解码器,但是它似乎需要相当数量的样板代码-与MongoJack的简单性相去甚远。 Take a look at an example in this blog post . 看一下此博客文章中的示例。

In my opinion, a new version of MongoJack would take advantage of the Codec capabilities of the driver and abstract them to something as simple as what the current version of MongoJack provides. 我认为,新版本的MongoJack将利用驱动程序的编解码器功能,并将其抽象为与当前版本的MongoJack一样简单的东西。 I have posted a question about this on the MongoJack github project. 我已经在MongoJack github项目上发布了一个与此有关的问题

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

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