Giter VIP home page Giter VIP logo

mongojack's Introduction

Mapping to POJOs couldn't be easier!

Since MongoDB uses BSON, a binary form of JSON, to store its documents, a JSON mapper is a perfect mechanism for mapping Java objects to MongoDB documents. And the best Java JSON mapper is Jackson. Jackson's parsing/generating interface fits serialising to MongoDBs documents like a glove. Its plugins, custom creators, serialisers, views, pluggable annotators and so on give this mapping library a massive head start, making it powerful, performant, and robust.

Maven Central

Snapshots, when appropriate, are available in Sonatype's snapshot repositories.

5.x Updates

In the 5.x update, deprecated objects (Aggregation, DBProjection, DBQuery, DBSort, DBUpdate) have been removed in favor of native mongo java driver constructs. This is a work in progress. For examples of how to replace the deprecated constructs, check out the tests as they differ between 4.x and 5.x versions.

Also note that this moves the minimum supported JVM to 11. Though it should work up to 21.

Some "gotchas" and issues:

  • Using Updates doesn't provide custom mapping, so if you have a custom serializer on a field in the object on which the collection is based, that won't take effect, and you will have to do the custom mapping yourself.

This attempts to support MqlValues. See some documentation (in the context of aggregation) here.

Ex from that documentation:

import static com.mongodb.client.model.Aggregates.*;
import static com.mongodb.client.model.Accumulators.*;
import static com.mongodb.client.model.Projections.*;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.mql.MqlValues.*;

class Foo {
    public void doSomething() {
        coll.aggregate(
            List.of(
                match(expr(
                    current()
                        .getArray("visitDates")
                        .size()
                        .gt(of(0))
                        .and(current()
                            .getString("state")
                            .eq(of("New Mexico")))
                )),
                group(current().getString("string"), min("integer", current().getInteger("integer")))
            ),
            MockObjectAggregationResult.class
        );
    }
}

Project documentation

The official documentation for this project lives here.

Mailing lists

The MongoDB Jackson Mapper users mailing list is hosted here.

Issues

The MongoDB Jackson Mapper issues are hosted here.

Quick start

Mongo driver compatibility

  • Version 2.3.0 and earlier are compatible only with the 2.x series mongo-java-driver.
  • Version 2.5.0 to 2.10.0 are compatible with the 3.x series mongodb-driver using the legacy 3.x series APIs.
  • Version 3.0.0 and later are compatible with versions 3.12.x and later of the mongo driver using mongodb-driver-sync without the legacy APIs.
  • Version 4.N.x+, 5.N.x are compatible with the matching oracle driver, although patch versions are not kept in sync

Installation

Using a Java dependency manager

The quickest and easiest way to start using MongoJack is to use one of the standard Java build tools, such as Maven or Gradle. For example:

Maven:

    <dependency>
      <groupId>org.mongojack</groupId>
      <artifactId>mongojack</artifactId>
      <version>5.0.0</version>
    </dependency>

Gradle:

    implementation 'org.mongojack:mongojack:5.0.0'

or

    implementation("org.mongojack:mongojack:5.0.0")

Writing code

Note: The 3.0 release of MongoJack removes all the functions of the older JacksonDBCollection, and all references to the legacy Mongo APIs.

MongoJack now supports only usage of the java mongo driver's 3.x API. There are two ways to use this feature.

  1. Use the JacksonCodecRegistry class
  2. Use the JacksonMongoCollection class

For a more detailed look at migration from 2.x to 3.x, see the migration guide

Using JacksonCodecRegistry

The java mongo 3.0 and higher driver supports the usage of codecs to map to specific types. MongoJack provides a Codec Registry which can be used for this purpose. Some example code making use of the JacksonCodecRegistry can be seen below:

MongoClient mongo = new MongoClient();
MongoDatabase mongoDatabase = mongo.getDatabase(testDatabaseName);
JacksonCodecRegistry jacksonCodecRegistry = new JacksonCodecRegistry();
jacksonCodecRegistry.addCodecForClass(MyObject.class);
MongoCollection<?> coll = mongoDatabase.getCollection("testCollection");
MongoCollection<MyObject> collection = coll.withDocumentClass(MyObject.class).withCodecRegistry(jacksonCodecRegistry);

The first two lines above get the database using the mongo driver. The third line constructs a new JacksonCodecRegistry. The fourth line tells the JacksonCodecRegistry to create a codec that will use Jackson for serialization/deserialization for the class MyObject. The fifth line gets a MongoCollection from the MongoDatabase, and the sixth tells the MongoCollection to use the MyObject class and work with the JacksonCodecRegsitry setup on lines three and four. JacksonCodecRegistry includes the default Mongo codecs, so it will also be capable of serializing and deserializing the Document and other default classes.

Using JacksonMongoCollection

JacksonMongoCollection is an implementation of MongoCollection which builds a JacksonCodecRegistry for you and adds some additional features such as mapping of queries and update documents. As an implementation of MongoCollection, it has all the features of the underlying driver, including map-reduce functionality, aggregation, transactions, etc. To use a JacksonMongoCollection the user will first need to initialize it using the builder.

MongoClient mongo = new MongoClient();
JacksonMongoCollection<MyObject> collection = JacksonMongoCollection.builder()
    .withObjectMapper(customObjectMapper)
    .build(mongo, "testDatabase", "testCollection", MyObject.class);

The builder allows you to specify the collection in a number of different ways; see the code and JavaDoc for specific options.

Usage largely follow the same pattern as the JacksonDBCollection with a few exceptions. In general, you use a JacksonMongoCollection as if it were a MongoCollection; it implements all methods and delegates almost everything to an underlying collection. The collection provides some additional helper methods, but in generally the interface is the same as com.mongodb.client.MongoCollection.

Old JacksonMongoCollection:

MockObject o1 = new MockObject("1", "ten", 10);
MockObject o2 = new MockObject("2", "ten", 10);
coll.insert(o1, o2, new MockObject("twenty", 20));
List<MockObject> results = collection.find(new BasicDBObject("string", "ten")).toArray();

New:

MockObject o1 = new MockObject("1", "ten", 10);
MockObject o2 = new MockObject("2", "ten", 10);
coll.insertMany(Arrays.asList(o1, o2, new MockObject("twenty", 20)));
List<MockObject> results = collection.find(Filters.eq("string", "ten")).into(new ArrayList<>());

The biggest difference between the usage of JacksonDBCollection and JacksonMongoCollection is that is that most of the inputs to the methods in the API must implement the org.bson.conversions.Bson interface. This allows you to pass instances of org.bson.Document, or com.mongodb.BasicDBObject, or any of Mongo's helper/builder objects like com.mongodb.client.model.Filters, com.mongodb.client.model.Aggregates, or com.mongodb.client.model.Updates.

MongoJack's older DBQuery, Updates, and Aggregation helpers should all still work with the new JacksonMongoCollection, but they have been deprecated as the Mongo driver provides a set of useful builders for all of these things in the com.mongodb.client.model package. The implementation attempts to do mapping on any Bson inputs.

Using a custom ObjectMapper

If you want to use a custom ObjectMapper, you need to install MongoJackModule on your ObjectMapper before using it. This can be done with one of two mechanisms. The first one installs the MongoJackModule, and also installs JavaTimeModule and changes some other settings on the object mapper.

ObjectMapper customObjectMapper = new ObjectMapper()
// ... configure your object mapper
ObjectMapperConfigurer.configureObjectMapper(customObjectMapper)
// ...
MongoClient mongo = new MongoClient();
JacksonMongoCollection<MyObject> collection = JacksonMongoCollection.builder()
    .withObjectMapper(customObjectMapper)
    .build(mongo, "testDatabase", "testCollection", MyObject.class);

If you want to install only the module itself, you can use the following, which installs the module but makes no other changes:

ObjectMapper customObjectMapper = new ObjectMapper()
// ... configure your object mapper
ObjectMapperConfigurer.addMongojackModuleOnly(customObjectMapper)
// ...
MongoClient mongo = new MongoClient();
JacksonMongoCollection<MyObject> collection = JacksonMongoCollection.builder()
    .withObjectMapper(customObjectMapper)
    .build(mongo, "testDatabase", "testCollection", MyObject.class);

mongojack's People

Contributors

aleskiewicz avatar antimony avatar bastien03 avatar benmccann avatar benromberg avatar camilobermudez85 avatar cfogrady avatar criedel avatar daymien avatar dependabot[bot] avatar jroper avatar jyemin avatar kdkeck avatar kfowler avatar lagenteestamuyloca81 avatar liorsl avatar lukepalmer avatar marcelb avatar maxkremer avatar mrserverless avatar paulbakker avatar pmaedel avatar rlodge avatar roughy avatar ryber avatar sdt-ork avatar sventorben avatar tbartley avatar timja avatar volomas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongojack's Issues

Unable to update using collection.update(Query, Object)

Hi,

I am unable to perform Update on a collection using OID. Using Mongojack 2.0.0-RC5

Following example throws ClassCastException: org.bson.types.ObjectId cannot be cast to java.lang.String

DB db = MongoAdapter.getInstance().getDB();
JacksonDBCollection<Category, String> myColl = JacksonDBCollection.wrap(db.getCollection("categories"), Category.class, String.class);

String strId = "51c2eaadc47ee7f1c4996e32";

// Find using ObjectId on Collection works:
DBObject q2 = new BasicDBObject("_id", new org.bson.types.ObjectId(strId));
Category fetchedObject = coll.findOne(q2); 

// Update Query using ObjectId fails:
Category objectToUpdate = new Category();
org.mongojack.DBQuery.Query q3 = DBQuery.is("_id", new org.bson.types.ObjectId(strId));
coll.update(q3, objectToUpdate); // Throws: ClassCastException: org.bson.types.ObjectId cannot be cast to java.lang.String

findById is not working

In version 2.0.0, findById is not working. Here is the code sample:

collection.findOneById(collection.find().get(0))

collection.find() returns all elements but collection.findOneById(collection.find().get(0)) returns no element.

Even tried this way:
collection.find().is("id", "idValue").next()

But it's not working neither.

Using String ObjectId mapping

It appears that there might be a bug with the ObjectId <-> String mapping for the ById query functionality. Because the query functionality converts the String id into an ObjectId, you can end up with the Exception below. This is quite likely to be the case if you are using ids that come in on a url, to lookup objects that are in your DB. Ideally the ById query functionality would either cause a null to be returned in this case, or would pass the string directly in as the query argument, though I'm not sure that would work with the generic handling that exists for the _id type.

java.lang.IllegalArgumentException: invalid ObjectId [fjdkaslfjdsak]
at org.bson.types.ObjectId.(ObjectId.java:128)
at org.bson.types.ObjectId.(ObjectId.java:122)
at org.mongojack.internal.ObjectIdSerializer.serialiseObject(ObjectIdSerializer.java:52)
at org.mongojack.internal.ObjectIdSerializer.serialize(ObjectIdSerializer.java:44)
at org.mongojack.internal.util.IdHandler$JacksonIdHandler.toDbId(IdHandler.java:95)
at org.mongojack.JacksonDBCollection.convertToDbId(JacksonDBCollection.java:1608)
at org.mongojack.JacksonDBCollection.createIdQuery(JacksonDBCollection.java:1600)
at org.mongojack.JacksonDBCollection.findOneById(JacksonDBCollection.java:915)
at org.mongojack.JacksonDBCollection.findOneById(JacksonDBCollection.java:904)

Upgrade Jackson 2.3.2 and Bson4Jackson 2.3.1 dependencies

Hi MongoJack devs,

I experimented with upgrading Jackson to 2.3.2 and Bson4Jackson to 2.3.1. As a result I made changes to 2 files:

MongoAnnotationIntrospector - removed deprecated methods and added following

public PropertyName findNameForDeserialization(Annotated a) { ... }
public PropertyName findNameForSerialization(Annotated a) { ... }

BsonObjectGenerator - added following methods

@Override
public int getFeatureMask() {
    return JsonGenerator.Feature.collectDefaults();
}

@Override
public JsonGenerator setFeatureMask(int i) { return this; }

All tests are passing and I didn't write any extra tests since the changes are covered by the existing: TestIdAnnotatedClass

I will send through a push request for you to consider.

Unrecognized field at subclass

I use mongojack to mapp my POJOs for mongoDB.

It works fine with a Single POJO.
But if I make a Subclass of this an insert fails:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "created" (class de.hbt.dps.data.PoJo), not marked as ignorable (2 known properties: , "_id", "url"])

This ar the classe:

public class PoJo {

    @Id
    protected String id;

    protected String url;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "PoJo [id=" + id + ", url=" + url + "]";
    }    
}

public class SubPoJo extends PoJo {

    private Date created;

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Override
    public String toString() {
        return "PoJo [id=" + id + ", url=" + url + ", created="+created+"]";
    }
}

and this the Code where the exception occurs:

MongoClient = new MongoClient(DB_SERVER, PORT);
DB db = mongoClient.getDB(DATABASE_NAME);
DBCollection dbColl = db.getCollection(SUBSCRIPTION_TABLE_NAME);

JacksonDBCollection<PoJo, String> coll = JacksonDBCollection.wrap(dbColl, PoJo.class, String.class);

// this works:
SubPoJo pojo = new PoJo();
pojo.setUrl("test");
coll.insert(pojo);

// this doesnt work:
SubPoJo pojo1 = new SubPoJo();
pojo1.setUrl("test");
pojo1.setCreated(new Date());
coll.insert(pojo1);

Error mapping BSON to POJOs with $pull DBQuery vs BasicDBObject

This fails for some reason:

        getJacksonCollection().updateById(presentationId, publishContent(user).pull("publishContent.library", DBQuery.is("id", libraryItemId)));

        org.mongojack.MongoJsonMappingException: Error mapping BSON to POJOs
            at org.mongojack.internal.util.SerializationUtils.serializeField(SerializationUtils.java:288) ~[mongojack-2.0.0-RC5.jar:na]
            at org.mongojack.internal.util.SerializationUtils.serializeDBUpdate(SerializationUtils.java:320) ~[mongojack-2.0.0-RC5.jar:na]
            at org.mongojack.DBUpdate$Builder.serialiseAndGet(DBUpdate.java:527) ~[mongojack-2.0.0-RC5.jar:na]
            at org.mongojack.JacksonDBCollection.updateById(JacksonDBCollection.java:476) ~[mongojack-2.0.0-RC5.jar:na]

while this works:

    getJacksonCollection().updateById(presentationId, publishContent(user).pull("publishContent.library", new BasicDBObject("id", libraryItemId)));

is this a bug or expected behavior?

Advanced query question.

Given the following data set:

db.predicates.save({"predicate":{"arguments":[{"name":"firstName","value":"John"},{"name":"lastName","value":"Montgomery"}]}});
db.predicates.save({"predicate":{"arguments":[{"name":"firstName","value":"John"},{"name":"lastName","value":"Greystone"}]}});
db.predicates.save({"predicate":{"arguments":[{"name":"lastName","value":"John"},{"name":"firstName","value":"Montgomery"}]}});

The following is a valid query in mongodb which will match the last element added above:

db.predicates.find({"predicate.arguments":{$all:[{$elemMatch:{"name":"firstName","value":"Montgomery"}},{$elemMatch:{"name":"lastName","value":"John"}}]}});

How would we write this query usig the DBQuery interface?

Release 2.3.0

@marcelb @roughy thanks for cutting the release. would it be possible to do another?

we added MongoDB 3.0 compatibility just after the release was cut. i'd love to get the 3.0 compatible version out since MongoDB 3.0 will be released very soon. sorry about the miscommunication. thought you guys knew about the pending PR

cdb4d61
774780c

BUG Can't find in array using Query

"Item" is a very simple class with an @id String id and a List<String> tags, and proper getters/setters.

Mongo mongo = new Mongo("localhost");
DB db = mongo.getDB("mongojack-test");
DBCollection coll = db.createCollection("items", null);

JacksonDBCollection<Item, String> items = JacksonDBCollection.wrap(coll, Item.class, String.class);

// Doesn't work
items.find().is("tags", "one").toArray();

// Doesn't work
Query q = DBQuery.is("tags", "one");
items.find(q).toArray();        

Stacktrace:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
at com.fasterxml.jackson.databind.ser.impl.StringCollectionSerializer.serialize(StringCollectionSerializer.java:24)
at org.mongojack.internal.util.SerializationUtils.serializeQueryField(SerializationUtils.java:219)
at org.mongojack.internal.util.SerializationUtils.serializeQueryCondition(SerializationUtils.java:145)
at org.mongojack.internal.util.SerializationUtils.serializeQuery(SerializationUtils.java:124)
at org.mongojack.internal.util.SerializationUtils.serializeQuery(SerializationUtils.java:116)
at org.mongojack.JacksonDBCollection.serializeQuery(JacksonDBCollection.java:1707)
at org.mongojack.JacksonDBCollection.find(JacksonDBCollection.java:829)
at mongojack.Main.main(Main.java:29)

Upgrade to Jackson 2.5.x

A couple of the tests currently fail with Jackson 2.5.x

TestDBUpdateSerialization.testObjectIdCollection:143 ยป Network Read operation ...
TestObjectIdHandling.testCollectionOfObjectIdStrings:186 ยป Network Read operat...

It's the "stream-des" tests that fail. Stream deserialization is the default, so this is concerning

internal NPE trying to serialize DBUpdate which sets a Double field to null

I have a class with a Double field, something like this:

public class TestClass {
   String id;
   Double prop;

   public String getId() { return id; }
   public void setId(String id) { this.id = id; }
   public Double getProp() { return prop; }
   public void setProp(Double prop) { this.prop = prop; }
}

If I try to update it using mongojack:

myMonogojackCollection.updateById("someId", DBUpdate.set("prop", null));

I get an internal NPE from Mongojack:

java.lang.NullPointerException
    at com.fasterxml.jackson.databind.ser.std.NumberSerializers$DoubleSerializer.serialize(NumberSerializers.java:195)
    at com.fasterxml.jackson.databind.ser.std.NumberSerializers$DoubleSerializer.serialize(NumberSerializers.java:183)
    at org.mongojack.internal.util.SerializationUtils.serializeUpdateField(SerializationUtils.java:354)
    at org.mongojack.internal.util.SerializationUtils.serializeUpdateField(SerializationUtils.java:346)
    at org.mongojack.internal.util.SerializationUtils.serializeDBUpdate(SerializationUtils.java:316)
    at org.mongojack.DBUpdate$Builder.serialiseAndGet(DBUpdate.java:527)
    at org.mongojack.JacksonDBCollection.updateById(JacksonDBCollection.java:476)

I think this is because SerializationUtils.serializeDBUpdate uses the valueSerializer from the relevant BeanPropertyWriter, ignoring any null check logic or the nullSerializer configured for it.
Jackson's standard DoubleSerializer, in turn, does not perform any null checking and assumes that if it was invoked, the value is non-null.

As far as I can tell, this affects all invocations of DBUpdate.set which set a boxed primitive to null.

Feature Request - JavaDoc

How about some JavaDoc documentation. For example what are the semantics of the coll.remove() operation?

Add Aggregation Support

We are attempting to migrate from an in house mongojack like solution to mongojack, however, mongojack does not have aggregation support.

Serialization issues with `Date(De)Serializer` when using type information

We've enabled type information in our Jackson ObjectMapper via objectMapper.enableDefaultTyping(...) because we serialize polymorphic types.

However now we cannot serialize values of type java.util.Date because we receive the following exception:

com.fasterxml.jackson.databind.JsonMappingException: Type id handling not implemented for type java.util.Date

The reason is that MongoJack's DateSerializer which is registered in MongoJackModule overwrites Jackson's default serializer for java.util.Date.
MongoJack's DateSerializer extends from JsonSerializer and doesn't implement/overwrite serializeWithType() which however is called by Jackson when typing is enabled. That's the reason for the exception.

Jackson's DateSerializer extends from DateTimeSerializerBase which extends from StdScalarSerializer where serializeWithType() is correctly implemented/overwritten.

For testing we created our own MongoJackModule where we copied everything from your module except the registration of Date (de)serializers and the problem is gone.

So the question is why MongoJack's Date(De)Serializers are required at all? I guess the same problem is true for Calendar(De)Serializers.

Fluent aggregation builder

#35 was closed with a PR however the implementation does not really jive with the rest of Mongojack which makes heavy use of fluent builders and abstracts the details of building Mongo DB objects. The current aggregation implementation requires heavy use of mongo's DBObject for constructing aggregation queries - this results in messy, unreadable code. I have a working implementation of an aggregation builder that follows the design principles set forth in classes such as '''org.mongojack.DBUpdate.Builder''' and will try and add it in PR.

2.1 Release!

Would it be possible to create a release (non SNAPSHOT) for 2.1? I'm in an environment where snapshots aren't permitted.

aggregation framework ?

How can we use aggregation framework with mongojack ?

Ex of aggregation request i need to :

db.myCollection.aggregate([
    { $match : { "date" : { "$gt" : "2014-01-01" , "$lt" : "2015-01-01"}}},
    { $group: { _id : {elt1:"$field1", elt2:"$field2"}, sum: {$sum:1}  } },
    { $sort : {sum:-1}}
   ]);

Thanks

MongoJackModule can never be used for advanced configuration in OSGi environment

The MongoJackModule resides in a non-exported package org.mongojack.internal and hence one could never actually use the module or configure an ObjectMapper with it in an OSGi environment.

One question that comes to mind is why the MongoJackModule is hidden for starters?

We configure the ObjectMapper with a raft of other modules such as Joda and adding the
MongoJackModule via mapper.registerModule(new MongoJackModule()); works nicely ...
and is much more natural than using the "helper" method MongoJackModule.configure(myObjectMapper);.

Could you please refactor the Module to be in a normal package longer term? And shorter term export the org.mongojack.internal package?

Serializing objects with Dates causes a StackOverflowError

I've applied MongoJackModule.configure to an ObjectMapper and am calling mapper.writeValueAsString on a HashMap that has a Date as one of its values. When I do this I get a StackOverflowError. Here's the top part of the trace:

java.lang.StackOverflowError
at com.fasterxml.jackson.databind.ser.SerializerCache.getReadOnlyLookupMap(SerializerCache.java:48)
at com.fasterxml.jackson.databind.SerializerProvider.(SerializerProvider.java:198)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.(DefaultSerializerProvider.java:63)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider$Impl.(DefaultSerializerProvider.java:437)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider$Impl.createInstance(DefaultSerializerProvider.java:443)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider$Impl.createInstance(DefaultSerializerProvider.java:429)
at com.fasterxml.jackson.databind.ObjectMapper._serializerProvider(ObjectMapper.java:2687)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1819)
at com.fasterxml.jackson.core.base.GeneratorBase.writeObject(GeneratorBase.java:259)
at org.mongojack.internal.DateSerializer.serialize(DateSerializer.java:34)
at org.mongojack.internal.DateSerializer.serialize(DateSerializer.java:31)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1819)
at com.fasterxml.jackson.core.base.GeneratorBase.writeObject(GeneratorBase.java:259)
at org.mongojack.internal.DateSerializer.serialize(DateSerializer.java:34)
at org.mongojack.internal.DateSerializer.serialize(DateSerializer.java:31)

I've verified that if I don't call MongJackModule.configure on the ObjectMapper that the error doesn't happen.

I'm using MongoJack 2.0.0-RC5 with Jackson 2.2.3.

Tests do not pass on MongoDB 2.6

To reproduce, change the version of MongoDB that the tests are run with. Currently hardcoded to Version.Main.V2_4 in src/test/java/org/mongojack/testing/DbManager.java

If I comment out the following code in JacksonDBObject then the tests begin to pass:

    if ("_id".equals(key)) {
        return "Generated _id retrieval not supported when using stream serialization";
    }

Bug in DBCursor.putGroup()

Calling DBCursor.and(Query...) fails to modify the query, in some cases. I've created a PR showing the problem: #93

Without the Ignore tag, it produces the following error:

  TestDBCursor.testPutGroup:81 
Expected: not "{ \"$and\" : [ { \"string\" : { \"$exists\" : true}}]}"
     but: was "{ \"$and\" : [ { \"string\" : { \"$exists\" : true}}]}"

JacksonDBCollection#removeById does not call serializeQuery

Ran into an interesting problem with JacksonDBCollection when switching from a String id to a custom id object.

Calling JacksonDBCollection#removeById with this custom id gave a failure:

java.lang.IllegalArgumentException: can't serialize class <custom id class>
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:155)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120)
    at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
    at com.mongodb.OutMessage.putObject(OutMessage.java:289)
    at com.mongodb.OutMessage.writeRemove(OutMessage.java:189)
    at com.mongodb.OutMessage.remove(OutMessage.java:69)
    at com.mongodb.DBApiLayer$MyCollection.remove(DBApiLayer.java:287)
    at com.mongodb.DBCollection.remove(DBCollection.java:250)
    at com.mongodb.DBCollection.remove(DBCollection.java:272)
    at org.mongojack.JacksonDBCollection.removeById(JacksonDBCollection.java:570)

Calling JacksonDBCollection#findOneById with the same custom id works as expected. Tracing the method calls shows that findOneById (eventually) ensures that it calls serializeFields on the query before passing it to mongo:

    public final org.mongojack.DBCursor<T> find(DBObject query, DBObject keys) {
        return new org.mongojack.DBCursor<T>(this, dbCollection.find(serializeFields(query), keys));
    }

I believe removeById should do something similar -- please let me know if this is incorrect. One way is to delegate in removeById to remove:

    public WriteResult<T, K> removeById(K id) throws MongoException {
        return remove(createIdQuery(id)));
    }

Thanks!

JsonCreator exemple not work

I want to generate uniqueid for a child element exemple

{ name : "toto"
  children : [{
     id:generateId
     name:"riri"
  },
 {
     id:generateId
     name:"fifi"
  }]
}

the child is a specific class:

   class Child
   private final String id;
   @JsonCreator
    public void Child(@ObjectId @Id id) {
        this.id = id;
  }

But i have a error in Eclipse :
The annotation @objectid is disallowed for this location

  • Syntax error on token "id", VariableDeclaratorId expected after this token
  • The annotation @id is disallowed for this location

how to use annotation to achieve this type of operation?
I am inspired by this example http://mongojack.org/tutorial.html

public class BlogPost {
  private final String id;
  @JsonCreator
  public BlogPost(@Id @ObjectId id) {
    this.id = id;
  }
  @Id
  @ObjectId
  public String getId() {
    return id;
  }
}

BuilderBasedDeserializer and *ById() methods not working

I have an Event class which uses an immutable builder pattern during deserialization, something like this:

@JsonDeserialize(build=Event.Builder.class)
class Event {
  private final String id;
  @ObjectId @Id
  public String getId() { ... }
  @JsonPOJOBuilder
  public static class Builder {
    @ObjectId @Id
    public Builder withId(String value) { ... }
    public Event build() { ... }
  }
}

During deserialization, Jackson uses a BuilderBasedDeserializer to instantiate an instance of Event.

After much debugging I believe the problem is with findOneById() and other *ById() methods that rely on IdHandler to serialize and deserialize the ID field. Specifically, IdHandlerFactory does not handle the case of BuilderBasedDeserializer. The if-statement on line 45 is false, and idDeserializer remains null. The last if-statement on line 63 therefore returns a NoopIdHandler instead of the desired JacksonIdHandler. As a result, all *ById() methods return null. Presumably because the internal query is using a String instead of an ObjectId.

My workaround is to query the database using statements like

if (id != null) {
  wrappedCollection.find(DBQuery.is("_id", new ObjectId(id)))...
}

but I would prefer to use the built in methods.

BUG: Query problem with custom object mapper

Summary:
Queries using operators and custom ObjectMapper doesn't work like:

    DBQuery.Query q = DBQuery.greaterThanEquals("instant", greater);
    assertNotNull(beanCollection.findOne(q));

Qhereas equivalent code not using mongojack works fine:

    DBObject dbObject = new BasicDBObject("instant", new BasicDBObject("$gte", objectMapper.writeValueAsString(greater)));
    assertNotNull(db.getCollection("beans").findOne(dbObject, null));

Some interesting symptoms:

  • Calling toMap() on query makes it explode inside java mongo driver saying it doesn't know how to serialize classes for which custom ObjectMapper is defined.
  • It actually returns an object if value in query is equal to the value in database.
  • serialize on custom serializer IS called by beanCollection.findOne(q)

Whole test class pasted at bottom for reference.

package dk.jhh.jackson;

import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.net.UnknownHostException;

import org.joda.time.Duration;
import org.joda.time.Instant;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mongojack.DBQuery;
import org.mongojack.Id;
import org.mongojack.JacksonDBCollection;
import org.mongojack.internal.MongoJacksonMapperModule;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class Tester {

    private static DB db;
    private static ObjectMapper objectMapper;
    private static Instant instant;
    private static Instant start;
    private static Instant greater; 
    private static JacksonDBCollection<Bean, String> beanCollection;


    @BeforeClass
    public static void beforeClass() throws UnknownHostException {
        Mongo mongo = new Mongo();
        db = mongo.getDB("test");       

        SimpleModule module = new SimpleModule("MySimpleModule", new Version(1,0,0,null,"",""));
        module.addDeserializer(Interval.class, new IntervalDeserializer());
        module.addSerializer(Interval.class, new IntervalSerializer());
        module.addDeserializer(Instant.class, new InstantDeserializer());
        module.addSerializer(Instant.class, new InstantSerializer());

        objectMapper = new ObjectMapper();
        objectMapper.registerModule(module);
        MongoJacksonMapperModule.configure(objectMapper);

        instant = Instant.now();
        start = instant.plus(Duration.standardSeconds(1));
        greater = start.plus(Duration.standardSeconds(1));
        beanCollection = JacksonDBCollection.wrap(db.getCollection("beans"), Bean.class, String.class, objectMapper);
    }

    @Before
    public void before() {
        db.dropDatabase();              
        beanCollection.insert(new Bean("Name0", instant, new Interval(start, Period.years(1))));
    }

    @Test
    public void testInstantGreaterThanEqualWithoutMongoJackAndIsGreater() throws JsonProcessingException {
        DBObject dbObject = new BasicDBObject("instant", new BasicDBObject("$gte", objectMapper.writeValueAsString(greater)));
        assertNotNull(db.getCollection("beans").findOne(dbObject, null));
    }

    @Test
    public void testIntervalGreaterThanEqualWithoutMongoJackAndIsGreater() throws JsonProcessingException {
        DBObject dbObject = new BasicDBObject("interval.start", new BasicDBObject("$gte", objectMapper.writeValueAsString(greater)));
        assertNotNull(db.getCollection("beans").findOne(dbObject, null));
    }

    @Test
    public void testInstantGreaterThanEqualWithMapperAndIsGreater() {       
        DBQuery.Query q = DBQuery.greaterThanEquals("instant", greater);
        assertNotNull(beanCollection.findOne(q));
    }

    @Test
    public void testIntervalGreaterThanEqualWithMapperAndIsGreater() {
        DBQuery.Query q = DBQuery.greaterThanEquals("interval.start", greater);
        assertNotNull(beanCollection.findOne(q));
    }

    @Test
    public void testInstantGreaterThanEqualWithMapperAndIsEqual() {     
        DBQuery.Query q = DBQuery.greaterThanEquals("instant", instant);
        assertNotNull(beanCollection.findOne(q));
    }

    @Test
    public void testIntervalGreaterThanEqualWithMapperAndIsEqual() {
        DBQuery.Query q = DBQuery.greaterThanEquals("interval.start", start);
        assertNotNull(beanCollection.findOne(q));
    }

    public static class Bean {
        @Id
        private String name;
        private Instant instant;
        private Interval interval;

        @JsonCreator
        public Bean(@JsonProperty("_id") String name, @JsonProperty("instant") Instant instant, @JsonProperty("interval") Interval interval) {
            this.name = name;
            this.instant = instant;
            this.interval = interval;
        }

        public String getName() {
            return name;
        }

        public Instant getInstant() {
            return instant;
        }

        public Interval getInterval() {
            return interval;
        }
    }

    public static class InstantSerializer extends JsonSerializer<Instant> {     
        @Override
        public void serialize(Instant instant, JsonGenerator jsonGenerator,
                SerializerProvider arg2) throws IOException,
                JsonProcessingException {
            System.out.println(instant.toString());
            jsonGenerator.writeString(instant.toString());      
        }
    }

    public static class InstantDeserializer extends JsonDeserializer<Instant> {
        @Override
        public Instant deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            Instant ret = Instant.parse(jp.getText());
            return ret;
        }
    }

    public static class IntervalSerializer extends JsonSerializer<Interval> {
        @Override
        public void serialize(Interval interval, JsonGenerator jsonGenerator,
                SerializerProvider arg2) throws IOException,
                JsonProcessingException {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeStringField("start", interval.getStart().toString());
            jsonGenerator.writeStringField("end", interval.getEnd().toString());
            jsonGenerator.writeEndObject();
        }
    }

    public static class IntervalDeserializer extends JsonDeserializer<Interval> {
        @Override
        public Interval deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            JsonNode node = jp.readValueAsTree();
            String startAsString = node.get("start").textValue();       
            String endAsString = node.get("end").textValue();       
            Instant start = Instant.parse(startAsString);
            Instant end = Instant.parse(endAsString);
            return new Interval(start, end);
        }
    }   
}

Query, QueryBuilder debug ouput

Please provide a way (e.g. Query.toString()) to get an JSON representation of Your very fluent Query/Builder class.
The scope is: For myself i build programmaticly the the Query. And i need a way to so how the resulting query looks like.

Using a DBUpdate for findAndModify

(confession: using the old stuff)

I'm not seeing how to use a DBUpdate to build the update for a findAndModify operation. There is no public accessor for the object mapper. I could make my own object mapper, but is this intended to be convenient?

Deserialization issue

I am getting the following exception on query:

java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(CoderResult.java:277)
    at de.undercouch.bson4jackson.io.LittleEndianInputStream.readUTF(LittleEndianInputStream.java:297)
    at de.undercouch.bson4jackson.io.LittleEndianInputStream.readUTF(LittleEndianInputStream.java:233)
    at de.undercouch.bson4jackson.BsonParser.readString(BsonParser.java:505)
    at de.undercouch.bson4jackson.BsonParser.nextToken(BsonParser.java:227)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:291)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:449)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2860)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1608)
    at org.mongojack.internal.stream.JacksonDBDecoder.decode(JacksonDBDecoder.java:64)
    at com.mongodb.Response.<init>(Response.java:83)
    at com.mongodb.DBPort.go(DBPort.java:142)
    at com.mongodb.DBPort.call(DBPort.java:92)
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:184)
    at com.mongodb.DBApiLayer$Result._advance(DBApiLayer.java:430)
    at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:399)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:464)
    at com.mongodb.DBCursor._fill(DBCursor.java:518)
    at com.mongodb.DBCursor.toArray(DBCursor.java:553)
    at org.mongojack.DBCursor.toArray(DBCursor.java:385)
    at org.mongojack.DBCursor.toArray(DBCursor.java:372)
    at com.mnistu.Apply.apply(Apply.java:127)
    at com.mnistu.Apply.run(Apply.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

This happens very rarely, on some certain fields. Reading directly via java driver and manually deserializing with Jackson works though.

Thanks!

Remove final method

Hello

The method org.mongojack.JacksonDBCollection.find() is final, that cause troubling for mocking when using lib like "mockito".

Is it possible to remove the final option ?

Serialization issu

I use MongoJack with Play Framework. I've just migrate to the 2.2 version.

I have an issu using the .in(...) function. I use it like this:

add string to previousString then:

List<DBQuery.Query> queryElts = new ArrayList<DBQuery.Query>();
if(previousString.size() != 0){
queryElts.add(DBQuery.in("fromExperimentTypeCodes", previousString));
}
return DBQuery.and(queryElts.toArray(new DBQuery.Query[queryElts.size()]));

and then my query:

 MongoDBResult<Container> results = MongoDBDAO.find(InstanceConstants.CONTAINER_COLL_NAME, Container.class, query)
                .sort(DatatableHelpers.getOrderBy(containerFilledForm), FormUtils.getMongoDBOrderSense(containerFilledForm))
                .page(DatatableHelpers.getPageNumber(containerFilledForm), DatatableHelpers.getNumberRecordsPerPage(containerFilledForm)); 
        List<Container> containers = results.toList();

the field in my in request is an array.

there is my trace in the console:

play.api.Application$$anon$1: Execution exception[[ClassCastException: java.lang.String cannot be cast to java.util.Collection]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.0]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.0]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.0]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.0]
        at scala.Option.map(Option.scala:145) [scala-library.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.0]
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
        at com.fasterxml.jackson.databind.ser.impl.StringCollectionSerializer.serialize(StringCollectionSerializer.java:24) ~[jackson-databind.jar:2.2.2]
        at org.mongojack.internal.util.SerializationUtils.serializeQueryField(SerializationUtils.java:219) ~[mongojack-2.0.0-RC5.jar:na]
        at org.mongojack.internal.util.SerializationUtils.serializeQueryCondition(SerializationUtils.java:145) ~[mongojack-2.0.0-RC5.jar:na]
        at org.mongojack.internal.util.SerializationUtils.serializeQueryCondition(SerializationUtils.java:154) ~[mongojack-2.0.0-RC5.jar:na]
        at org.mongojack.internal.util.SerializationUtils.serializeQuery(SerializationUtils.java:124) ~[mongojack-2.0.0-RC5.jar:na]
        at org.mongojack.internal.util.SerializationUtils.serializeQueryCondition(SerializationUtils.java:162) ~[mongojack-2.0.0-RC5.jar:na]

mongojack does not handle custom map keys

I tried using field that was hashmap with custom keys, but mongojack makes keys null and it Jackson blows up on validating object.

When attempting to serialize object I was passing custom ObjectMapper that had custom map key serializer and deserializer registered for my custom key.

2.1.0-SNAPSHOT update

The last snapshot to sonatype seems to have been back in january, and since then many good things have been merged to master. Would it be possible to get the snapshot updated?

MongoJack.org site disabled

When I view the MongoJack.org website, I get a "Site Disabled" message:

Site Disabled

Oh no! It looks like this Pages site has been disabled due to a misconfigured custom domain.

Are you the site owner? You can quickly fix this issue by updating the DNS record for your Pages site:

If you're using a subdomain (coolthing.example.com, www.example.com, etc.), make sure you have a CNAME record that points to your-username.github.io.
If you're not using a subdomain (example.com), make sure you have two A records set to the following IPs: 192.30.252.153 and 192.30.252.154

in queries don't work with iterables

It'd be cool to make in queries work with Iterable. I passed one in and it was incorrectly serialized to an array of arrays because it matched the Object... method signature instead of the Collection<?> method signature.

DBQuery.Query no longer implements DBObject.

I tried an update from my old mongojack version to the last one.

But i done't understand why DBQuery.Query no longer implements DBObject.

My version (mongo-jackson-mapper-1.4.0.jar)

public static class Query extends DBQuery.AbstractBuilder implements DBObject {

Last version (mongojack 2.0.0)

public static class Query extends DBQuery.AbstractBuilder {

Consequences, i cannot use "DBQuery.Query" as DBObject as i did with the method "com.mongodb.DBCollection.find(DBObject ref)"

query for uuid in bindata field

given the mongo document:

{
    "_id": ObjectId("..."),
    "name": "example",
    "tag": BinData(3,"Yk2qH7ibk4vQ4XQ31kNCiw==")
}

I can query for this document using the regular java mongo driver like this:

MongoClient client = new MongoClient();
DB db = client.getDB("mydb");
DBCollection coll = db.getCollection("items");
String tagUuid = "8b939bb8-1faa-4d62-8b42-43d63774e1d0";
DBObject query = QueryBuilder.start("tag").is(UUID.fromString(tagUuid)).get();
DBCursor cursor = coll.find(query).limit(1);
System.out.println(cursor.hasNext());

will print true.

Using mongojack however, I do not get the same result:

// borrowing the same setup from the example above
JacksonDBCollection<Item,Object> jacksonColl = JacksonDBCollection.wrap(coll, Item.class);
// using the same query object as the example above
DBCursor<Item> cursor = jacksonColl.find(query).limit(1);
System.out.println(cursor.hasNext());

will print false.

Now I understand there are different ways of building the query. I've tried the following, but all with the same result:

jacksonColl.find().is("tag", UUID.fromString(tagUuid)).limit(1);
jacksonColl.find(DBQuery.is("tag", UUID.fromString(tagUuid)).limit(1);

As far as I could trace, the query object produces the right mongo query, but at some point it is serialized into the uuid string value. The serialization occurs in SerializationUtils.java#L103 and SerializationUtils.java#L128, IIRC.

Now this might be the wrong way to query for BinData fields with mongojack. If so, could someone point me to some examples on how to do it?

DBUpdate.push + DBRef doesn't serialize correctly

I wanted to add a new DBRef to refs array, so I tried something like this:

DBRef userRef = new DBRef(null, "users", "516e76a8f2fada7dd94dde74")
getDbCollection().update(query, DBUpdate.push("refs", userRef));

what is being serialized as:

"refs" : [
  {
    "id" : "516e76a8f2fada7dd94dde74",
    "ref" : "users"
  }
]

what in turn causes problems during deserialization to list of DBRefs - I get null as element of the list instead of DBRef instance.
Native MongoDB driver serializes DBRefs as:

"refs" : [
    DBRef("users", "516e76a8f2fada7dd94dde74")
]

and deserialization works correctly in this case.

BUG: NPE when trying to use DBUpdate with an Enum

I have a document with a Java Enum in it, and I want to update the field to a different Enum value. When I call JacksonDBCollection.updateMulti with the DBUpdate builder (i.e. DBUpdate.set("status", ImportStatusEnum.FAILED)), I get the following NPE:

java.lang.NullPointerException
at com.fasterxml.jackson.databind.SerializerProvider.isEnabled(SerializerProvider.java:287)
at com.fasterxml.jackson.databind.ser.std.EnumSerializer._serializeAsIndex(EnumSerializer.java:198)
at com.fasterxml.jackson.databind.ser.std.EnumSerializer.serialize(EnumSerializer.java:139)
at com.fasterxml.jackson.databind.ser.std.EnumSerializer.serialize(EnumSerializer.java:31)
at org.mongojack.internal.util.SerializationUtils.serializeUpdateField(SerializationUtils.java:330)
at org.mongojack.internal.util.SerializationUtils.serializeUpdateField(SerializationUtils.java:322)
at org.mongojack.internal.util.SerializationUtils.serializeDBUpdate(SerializationUtils.java:293)
at org.mongojack.DBUpdate$Builder.serialiseAndGet(DBUpdate.java:527)
at org.mongojack.JacksonDBCollection.updateMulti(JacksonDBCollection.java:497)

I've tracked down the problem to the org.mongojack.internal.util.SerializationUtils#serializeUpdateField method. Instead of calling objectMapper.getSerializerProvider(), it should call JacksonAccessor.getSerializerProvider(objectMapper). This will initialize the SerializerProvider properly, setting the _config variable so its not null when checking isEnabled. Please add this fix to your next release.

Thanks, and nice job on the library; it's very useful.

find objectId mongoDb

Hi,

When i insert an object with a key _id and i try to find it with updateById or with findOneById it can't handle with a ObjectId ....

Example: _id : ObjectId("5230b2e1c5ad483e246deccc")

When i try to find and update an object in my collection (I don't want to use Query Object to find it...):

coll.updateById ("5230b2e1c5ad483e246deccc" , object) ;

It will never find the content i want... because _id is not a String but an ObjectId. Can you add that to your code to support finding _id generated by mongodb?

Continue this magnific project giving a much easier solution to interact Mongodb with java Objects to developers.

Thanks :D

Can't serialize dates with custom object mapper

Hey James, I was curious if you knew why this doesn't work. I can't use the custom mapper I used with mongojack outside of mongojack. If I comment out the call to MongoJacksonMapperModule.configure then it works.

public static class ExampleClass {
  @JsonProperty
  Date d;
}

@Test
public void testPrintDate() throws JsonProcessingException, IOException {    
  ExampleClass o = new ExampleClass();
  o.d = new Date();

  ObjectMapper mapper = new ObjectMapper();
  MongoJacksonMapperModule.configure(mapper);

  mapper.writeValueAsBytes(o);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.