Search in sources :

Example 66 with DBRef

use of com.mongodb.DBRef in project morphia by mongodb.

the class ReaderState method unwind.

private Object unwind(Object value) {
    Object unwind = value;
    if (value instanceof DBRef) {
        DBRef dbRef = (DBRef) value;
        Document document = new Document("$ref", dbRef.getCollectionName()).append("$id", dbRef.getId());
        if (dbRef.getDatabaseName() != null) {
            document.append("$db", dbRef.getDatabaseName());
        }
        unwind = document;
    } else if (value instanceof UUID) {
        unwind = new BsonBinary((UUID) value);
    }
    return unwind;
}
Also used : BsonBinary(org.bson.BsonBinary) DBRef(com.mongodb.DBRef) Document(org.bson.Document) UUID(java.util.UUID)

Example 67 with DBRef

use of com.mongodb.DBRef in project morphia by mongodb.

the class ReferenceCodec method processId.

/**
 * Decodes an ID value
 *
 * @param datastore      the Datastore to use
 * @param decode         the value to decode
 * @param decoderContext the decoder context
 * @return the decoded value
 */
@NonNull
public static Object processId(Datastore datastore, Object decode, DecoderContext decoderContext) {
    Object id = decode;
    if (id instanceof Iterable) {
        Iterable<?> iterable = (Iterable<?>) id;
        List<Object> ids = new ArrayList<>();
        for (Object o : iterable) {
            ids.add(processId(datastore, o, decoderContext));
        }
        id = ids;
    } else if (id instanceof Document) {
        Document document = (Document) id;
        if (document.containsKey("$ref")) {
            id = processId(datastore, new DBRef(document.getString("$db"), document.getString("$ref"), document.get("$id")), decoderContext);
        } else if (document.containsKey(datastore.getMapper().getOptions().getDiscriminatorKey())) {
            try {
                id = datastore.getCodecRegistry().get(datastore.getMapper().getClass(document)).decode(new DocumentReader(document), decoderContext);
            } catch (CodecConfigurationException e) {
                throw new MappingException(Sofia.cannotFindTypeInDocument(), e);
            }
        }
    } else if (id instanceof DBRef) {
        DBRef ref = (DBRef) id;
        Object refId = ref.getId();
        if (refId instanceof Document) {
            refId = datastore.getCodecRegistry().get(Object.class).decode(new DocumentReader((Document) refId), decoderContext);
        }
        id = new DBRef(ref.getDatabaseName(), ref.getCollectionName(), refId);
    }
    return id;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) NonNull(com.mongodb.lang.NonNull)

Example 68 with DBRef

use of com.mongodb.DBRef in project mongo-hadoop by mongodb.

the class PigBoxedDBRef method getObject.

@Override
public DBRef getObject() {
    byte[] bytes = get();
    ObjectId id = new ObjectId(Arrays.copyOfRange(bytes, bytes.length - 12, bytes.length));
    String collectionName = new String(Arrays.copyOfRange(bytes, 0, bytes.length - 13));
    return new DBRef(collectionName, id);
}
Also used : ObjectId(org.bson.types.ObjectId) DBRef(com.mongodb.DBRef)

Aggregations

DBRef (com.mongodb.DBRef)68 Document (org.bson.Document)26 Test (org.junit.jupiter.api.Test)24 BasicDBObject (com.mongodb.BasicDBObject)22 ObjectId (org.bson.types.ObjectId)20 DBObject (com.mongodb.DBObject)17 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)13 BasicDBList (com.mongodb.BasicDBList)11 ArrayList (java.util.ArrayList)7 Binary (org.bson.types.Binary)7 Test (org.junit.Test)6 Document (org.springframework.data.mongodb.core.mapping.Document)6 DBCollection (com.mongodb.DBCollection)5 Map (java.util.Map)5 BasicBSONObject (org.bson.BasicBSONObject)5 Date (java.util.Date)4 List (java.util.List)4 UUID (java.util.UUID)4 Bson (org.bson.conversions.Bson)4 MaxKey (org.bson.types.MaxKey)4