Results 1 to 2 of 2

Thread: Mongo: Field with multiple types

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Default Mongo: Field with multiple types

    Hello!

    In a collection I have 1 field which could be either ObjectId or UUID.
    I've wrote class ObjectIdOrUUIDField and specified it as a field type:
    Code:
    public class ObjectIdOrUUIDField implements Serializable {
    	private UUID uuid;
    	private ObjectId object_id;
    	
    	public ObjectIdOrUUIDField(UUID uuid) {
    		this.uuid = uuid;
    	}
    	
    	public ObjectIdOrUUIDField(ObjectId object_id) {
    		this.object_id = object_id;
    	}
    }
    This is allows me load objects from the collections but not save. If I tried to save the field is converted into JSON object either:
    Code:
    {
         "m" : {
    		"object_id" : ObjectId("512a4a7403648a7456929424")
         }
    }
    or

    Code:
    {
         "m" : {
    		"uuid" : BinData(3,"xIqiFeEaK499zB6ytJxZQw==")
         }
    }
    But I need the following:

    Code:
    {
         "m" : ObjectId("512a4a7403648a7456929424")
    }
    or

    Code:
    {
         "m" : BinData(3,"xIqiFeEaK499zB6ytJxZQw==")
    }
    How can I do it?

  2. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    Implement one Converter<ObjectIdOrUUIDField, DBObject> and one Converter<DBObject, ObjectIdOrUUIDField> and implement the behavior as you need it. I don't think we're going to add dedicated support in the mapping subsystem here as mapping two properties to a single field is usually an invalid mapping introduced by accident.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •