Announcement Announcement Module
Collapse
No announcement yet.
Mongo: Field with multiple types Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X