@MongoId and @MongoObjectId

437 views
Skip to first unread message

Neal Sorensen

unread,
Jul 27, 2015, 5:16:44 PM7/27/15
to Jongo
I'm trying to create mongo objects with auto-increment string object ids that look like this (to be backwards compatible with previous jongo versions):
{
  "_id" : "55b69cd314b811a73c2f65f5"
}

I've tried every combination of @MongoId, @MongoObjectId, and variable names and still can't get it to insert as an auto-increment object id string.  It always ends up looking like this:
{
  "_id" : ObjectId("55b69cd314b811a73c2f65f5")
}


public static class Friend {
      @MongoId
      @MongoObjectId
      private String key;
}

Friend friend = new Friend();
        getJongoFileCollection()
                .withWriteConcern(WriteConcern.SAFE)
                .insert(friend);

<dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.0.3</version>
        </dependency>
<dependency>
            <groupId>org.jongo</groupId>
            <artifactId>jongo</artifactId>
            <version>1.2</version>
</dependency>

Mongo Server version: 2.4.9

What do I need to do to insert documents with string _id fields that are auto-created?

Benoît GUEROUT

unread,
Jul 28, 2015, 8:30:55 AM7/28/15
to jongo...@googlegroups.com
Correct me if i'm wrong.
You are trying to insert a document with a property named 'key' which is a String in you java application and should be the _id of the document in the Mongo ?

--
You received this message because you are subscribed to the Google Groups "Jongo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jongo-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Neal Sorensen

unread,
Jul 28, 2015, 9:41:06 AM7/28/15
to Jongo, bgue...@gmail.com
Correct.  I've tried naming it _id in Java too, but haven't had any luck getting a String object id into Mongo upon creation regardless of the combination of annotations I try.  I ended up getting it to work by putting private String _id = new ObjectId().toString();  and create the object id when the object is created, rather than when it is persisted to Mongo.  I could set it right before inserting it too, but I was hoping the annotations would do it like it has worked in previous Jongo versions.

Benoît GUEROUT

unread,
Jul 28, 2015, 11:10:52 AM7/28/15
to Neal Sorensen, Jongo
If you want to stored document with property "_id" as String instead of ObjectId, you have to remove @MongoObjectId.
Nevertheless you need to set the key attribute manually (eg. ObjectId.get())

Neal Sorensen

unread,
Jul 28, 2015, 11:12:23 AM7/28/15
to Jongo, bgue...@gmail.com
We didn't have to set it manually in previous versions of Jongo, is there anyway to fix this?

@MongoId
private String key;

Benoît GUEROUT

unread,
Jul 28, 2015, 11:29:31 AM7/28/15
to jongo...@googlegroups.com
Can you post the old version of your annotated POJO ?

Benoît GUEROUT

unread,
Jul 28, 2015, 11:37:09 AM7/28/15
to jongo...@googlegroups.com
What is the version you used before the 1.2 update ?

Benoît GUEROUT

unread,
Jul 28, 2015, 12:06:47 PM7/28/15
to jongo...@googlegroups.com
Since 1.2 or 1.1 (don"t remember), you have to annotate your attribute with @MongoId to tell Jongo to generate an ObjectId.

Your POJO seems good to me but this will generate a document with an ObjectId property which will be named _id

public class Friend {
      @MongoId
      @MongoObjectId
      private String key;
}

If you want to force Jongo to generate an ObjectId for the following case : 

public class Friend {
      @MongoId
      private String key;
}

You can create a custom Mapper : 

 Mapper mapper = new JacksonMapper.Builder()
                .withObjectIdUpdater(new CustomObjectIdUpdater(new JacksonIdFieldSelector()))
                .build();
 new Jongo(mapper);
...

with 

public class CustomObjectIdUpdater extends ReflectiveObjectIdUpdater {
...
@Override
        public boolean mustGenerateObjectId(Object pojo) {
           if(pojo instanceof ...){
           //check if you need to generate id
             //Beware this will have global impact on all Jongo's operation
            }
           return super.mustGenerateObjectId(pojo);
         }
    ...
}

Neal Sorensen

unread,
Jul 29, 2015, 9:36:49 AM7/29/15
to Jongo, bgue...@gmail.com
I used Jongo 1.1 and didn't annotate the field, I just named it _id and had the value as a String and it auto-created a string version of an ObjectId as expected.  

Your suggested solution is super complex.  Just manually setting the _id field to new ObjectId().toString() is a much more simple approach.

Benoît GUEROUT

unread,
Jul 29, 2015, 10:29:17 AM7/29/15
to Neal Sorensen, Jongo
The easiest solution is clearly to set manually your _id with ObjectId.get() as explained in documentation (http://jongo.org)
When @MongoObjectId is detected, Jongo does exactly the same.

If you would like to do something a bit more complicated, you can still rely on ObjectIdUpdater.
Reply all
Reply to author
Forward
0 new messages