Search in sources :

Example 81 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-java-driver by mongodb.

the class JSONTest method testRegexNoOptions.

@Test
public void testRegexNoOptions() {
    String x = "^Hello$";
    String serializedPattern = "{ \"$regex\" : \"" + x + "\"}";
    Pattern pattern = Pattern.compile(x);
    assertEquals(JSON.serialize(pattern), serializedPattern);
    BasicDBObject a = new BasicDBObject("x", pattern);
    assertEquals(a.toString(), "{ \"x\" : " + serializedPattern + "}");
    DBObject b = (DBObject) JSON.parse(a.toString());
    assertEquals(Pattern.class, b.get("x").getClass());
    assertEquals(b.toString(), a.toString());
}
Also used : Pattern(java.util.regex.Pattern) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 82 with BasicDBObject

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

the class FindAndModifyOptionsTest method passThrough.

@Test
public void passThrough() {
    Collation collation = Collation.builder().locale("en").caseLevel(true).build();
    DBCollectionFindAndModifyOptions options = new FindAndModifyOptions().bypassDocumentValidation(true).collation(collation).getOptions().maxTime(15, TimeUnit.MINUTES).projection(new BasicDBObject("field", "value")).remove(true).returnNew(true).sort(new BasicDBObject("field", -1)).update(new BasicDBObject("$inc", "somefield")).upsert(true).writeConcern(WriteConcern.JOURNALED);
    assertTrue(options.getBypassDocumentValidation());
    assertEquals(collation, options.getCollation());
    assertEquals(15, options.getMaxTime(TimeUnit.MINUTES));
    assertEquals(new BasicDBObject("field", "value"), options.getProjection());
    assertTrue(options.isRemove());
    assertTrue(options.returnNew());
    assertEquals(new BasicDBObject("field", -1), options.getSort());
    assertEquals(new BasicDBObject("$inc", "somefield"), options.getUpdate());
    assertTrue(options.isUpsert());
    assertEquals(WriteConcern.JOURNALED, options.getWriteConcern());
}
Also used : DBCollectionFindAndModifyOptions(com.mongodb.client.model.DBCollectionFindAndModifyOptions) BasicDBObject(com.mongodb.BasicDBObject) DBCollectionFindAndModifyOptions(com.mongodb.client.model.DBCollectionFindAndModifyOptions) Collation(com.mongodb.client.model.Collation) Test(org.junit.Test)

Example 83 with BasicDBObject

use of com.mongodb.BasicDBObject 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)

Example 84 with BasicDBObject

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

the class QueryImpl method parseFieldsString.

/**
     * Parses the string and validates each part
     *
     * @param str      the String to parse
     * @param clazz    the class to use when validating
     * @param mapper   the Mapper to use
     * @param validate true if the results should be validated
     * @return the DBObject
     */
public static BasicDBObject parseFieldsString(final String str, final Class clazz, final Mapper mapper, final boolean validate) {
    BasicDBObject ret = new BasicDBObject();
    final String[] parts = str.split(",");
    for (String s : parts) {
        s = s.trim();
        int dir = 1;
        if (s.startsWith("-")) {
            dir = -1;
            s = s.substring(1).trim();
        }
        if (validate) {
            final StringBuilder sb = new StringBuilder(s);
            validateQuery(clazz, mapper, sb, FilterOperator.IN, "", true, false);
            s = sb.toString();
        }
        ret.put(s, dir);
    }
    return ret;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject)

Example 85 with BasicDBObject

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

the class QueryImpl method getFieldsObject.

@Override
@Deprecated
public DBObject getFieldsObject() {
    DBObject projection = getOptions().getProjection();
    if (projection == null || projection.keySet().size() == 0) {
        return null;
    }
    final MappedClass mc = ds.getMapper().getMappedClass(clazz);
    Entity entityAnnotation = mc.getEntityAnnotation();
    final BasicDBObject fieldsFilter = copy(projection);
    if (includeFields && entityAnnotation != null && !entityAnnotation.noClassnameStored()) {
        fieldsFilter.put(Mapper.CLASS_NAME_FIELDNAME, 1);
    }
    return fieldsFilter;
}
Also used : Entity(org.mongodb.morphia.annotations.Entity) BasicDBObject(com.mongodb.BasicDBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)566 DBObject (com.mongodb.DBObject)338 Test (org.junit.Test)175 DBCollection (com.mongodb.DBCollection)77 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)68 ApiOperation (io.swagger.annotations.ApiOperation)65 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)64 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)54 ArrayList (java.util.ArrayList)53 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)52 ObjectId (org.bson.types.ObjectId)43 DBCursor (com.mongodb.DBCursor)41 HashMap (java.util.HashMap)35 BasicDBList (com.mongodb.BasicDBList)32 List (java.util.List)30 Map (java.util.Map)26 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)22 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)18