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());
}
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());
}
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"));
}
}
}
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;
}
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;
}
Aggregations