Search in sources :

Example 6 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class DatastoreImpl method update.

@Override
@SuppressWarnings("unchecked")
public <T> UpdateResults update(final T entity, final UpdateOperations<T> operations) {
    if (entity instanceof Query) {
        return update((Query<T>) entity, operations);
    }
    final MappedClass mc = mapper.getMappedClass(entity);
    Query<?> query = createQuery(mapper.getMappedClass(entity).getClazz()).disableValidation().filter(Mapper.ID_KEY, mapper.getId(entity));
    if (!mc.getFieldsAnnotatedWith(Version.class).isEmpty()) {
        final MappedField field = mc.getFieldsAnnotatedWith(Version.class).get(0);
        query.field(field.getNameToStore()).equal(field.getFieldValue(entity));
    }
    return update((Query<T>) query, operations);
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Query(org.mongodb.morphia.query.Query) Version(org.mongodb.morphia.annotations.Version) MappedClass(org.mongodb.morphia.mapping.MappedClass)

Example 7 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class DatastoreImpl method update.

@SuppressWarnings("unchecked")
private <T> UpdateResults update(final Query<T> query, final DBObject update, final UpdateOptions options) {
    DBCollection dbColl = query.getCollection();
    // TODO remove this after testing.
    if (dbColl == null) {
        dbColl = getCollection(query.getEntityClass());
    }
    if (query.getSortObject() != null && query.getSortObject().keySet() != null && !query.getSortObject().keySet().isEmpty()) {
        throw new QueryException("sorting is not allowed for updates.");
    }
    if (query.getOffset() > 0) {
        throw new QueryException("a query offset is not allowed for updates.");
    }
    if (query.getLimit() > 0) {
        throw new QueryException("a query limit is not allowed for updates.");
    }
    DBObject queryObject = query.getQueryObject();
    final MappedClass mc = getMapper().getMappedClass(query.getEntityClass());
    final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class);
    if (!fields.isEmpty()) {
        final MappedField versionMF = fields.get(0);
        if (update.get(versionMF.getNameToStore()) == null) {
            if (!update.containsField("$inc")) {
                update.put("$inc", new BasicDBObject(versionMF.getNameToStore(), 1));
            } else {
                ((Map<String, Object>) (update.get("$inc"))).put(versionMF.getNameToStore(), 1);
            }
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert()));
    }
    return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()).getOptions()));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) QueryException(org.mongodb.morphia.query.QueryException) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 8 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class IndexHelper method findField.

String findField(final MappedClass mc, final IndexOptions options, final List<String> path) {
    String segment = path.get(0);
    if (segment.equals("$**")) {
        return segment;
    }
    MappedField mf = mc.getMappedField(segment);
    if (mf == null) {
        mf = mc.getMappedFieldByJavaField(segment);
    }
    if (mf == null && mc.isInterface()) {
        for (final MappedClass mappedClass : mapper.getSubTypes(mc)) {
            try {
                return findField(mappedClass, options, new ArrayList<String>(path));
            } catch (MappingException e) {
            // try the next one
            }
        }
    }
    String namePath;
    if (mf != null) {
        namePath = mf.getNameToStore();
    } else {
        if (!options.disableValidation()) {
            throw pathFail(mc, path);
        } else {
            return join(path, '.');
        }
    }
    if (path.size() > 1) {
        try {
            Class concreteType = !mf.isSingleValue() ? mf.getSubClass() : mf.getConcreteType();
            namePath += "." + findField(mapper.getMappedClass(concreteType), options, path.subList(1, path.size()));
        } catch (MappingException e) {
            if (!options.disableValidation()) {
                throw pathFail(mc, path);
            } else {
                return join(path, '.');
            }
        }
    }
    return namePath;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 9 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class IndexHelperTest method indexPartialFilters.

@Test
public void indexPartialFilters() {
    MongoCollection<Document> collection = getDatabase().getCollection("indexes");
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    Index index = new IndexBuilder().fields(new FieldBuilder().value("text")).options(new IndexOptionsBuilder().partialFilter("{ name : { $gt : 13 } }"));
    indexHelper.createIndex(collection, mappedClass, index, false);
    findPartialIndex(BasicDBObject.parse(index.options().partialFilter()));
}
Also used : Index(org.mongodb.morphia.annotations.Index) MappedClass(org.mongodb.morphia.mapping.MappedClass) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 10 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class IndexHelperTest method index.

@Test
public void index() {
    checkMinServerVersion(3.4);
    MongoCollection<Document> indexes = getDatabase().getCollection("indexes");
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    indexes.drop();
    Index index = new IndexBuilder().fields(new FieldBuilder().value("indexName"), new FieldBuilder().value("text").type(IndexType.DESC)).options(indexOptions());
    indexHelper.createIndex(indexes, mappedClass, index, false);
    List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class).getIndexInfo();
    for (DBObject dbObject : indexInfo) {
        if (dbObject.get("name").equals("indexName")) {
            checkIndex(dbObject);
            assertEquals("en", dbObject.get("default_language"));
            assertEquals("de", dbObject.get("language_override"));
            assertEquals(new BasicDBObject().append("locale", "en").append("caseLevel", true).append("caseFirst", "upper").append("strength", 5).append("numericOrdering", true).append("alternate", "shifted").append("maxVariable", "space").append("backwards", true).append("normalization", true).append("version", "57.1"), dbObject.get("collation"));
        }
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Index(org.mongodb.morphia.annotations.Index) MappedClass(org.mongodb.morphia.mapping.MappedClass) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Aggregations

MappedClass (org.mongodb.morphia.mapping.MappedClass)92 Test (org.junit.Test)73 MappedField (org.mongodb.morphia.mapping.MappedField)56 Mapper (org.mongodb.morphia.mapping.Mapper)53 ArrayList (java.util.ArrayList)48 BasicDBObject (com.mongodb.BasicDBObject)18 DBObject (com.mongodb.DBObject)12 BsonDocument (org.bson.BsonDocument)8 Document (org.bson.Document)8 MappingException (org.mongodb.morphia.mapping.MappingException)6 DBCollection (com.mongodb.DBCollection)5 List (java.util.List)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 Key (org.mongodb.morphia.Key)4 Index (org.mongodb.morphia.annotations.Index)4 LinkedHashMap (java.util.LinkedHashMap)3 NotSaved (org.mongodb.morphia.annotations.NotSaved)3 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 WriteResult (com.mongodb.WriteResult)2