use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonReaderTest method testRegularExpressionStrict.
@Test
public void testRegularExpressionStrict() {
String json = "{ \"$regex\" : \"pattern\", \"$options\" : \"imxs\" }";
bsonReader = new JsonReader(json);
assertEquals(BsonType.REGULAR_EXPRESSION, bsonReader.readBsonType());
BsonRegularExpression regex = bsonReader.readRegularExpression();
assertEquals("pattern", regex.getPattern());
assertEquals("imsx", regex.getOptions());
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonScannerTest method testRegularExpressionPatternAndOptions.
@Test
public void testRegularExpressionPatternAndOptions() {
String json = "\t /pattern/im,";
JsonBuffer buffer = new JsonBuffer(json);
JsonScanner scanner = new JsonScanner(buffer);
JsonToken token = scanner.nextToken();
assertEquals(JsonTokenType.REGULAR_EXPRESSION, token.getType());
BsonRegularExpression regularExpression = token.getValue(BsonRegularExpression.class);
assertEquals("pattern", regularExpression.getPattern());
assertEquals("im", regularExpression.getOptions());
assertEquals(',', buffer.read());
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonWriterTest method testRegularExpressionStrict.
@Test
@SuppressWarnings("deprecation")
public void testRegularExpressionStrict() {
List<TestData<BsonRegularExpression>> tests;
tests = asList(new TestData<BsonRegularExpression>(new BsonRegularExpression(""), "{ \"$regex\" : \"\", " + "\"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a/b"), "{ \"$regex\" : " + "\"a/b\", " + "\"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a\\b"), "{ \"$regex\" : " + "\"a\\\\b\", " + "\"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "i"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"i\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "m"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"m\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "x"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"x\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "s"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"s\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "imxs"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"imsx\" }"));
for (final TestData<BsonRegularExpression> cur : tests) {
stringWriter = new StringWriter();
writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.STRICT).build());
writer.writeStartDocument();
writer.writeRegularExpression("regex", cur.value);
writer.writeEndDocument();
String expected = "{ \"regex\" : " + cur.expected + " }";
assertEquals(expected, stringWriter.toString());
}
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonReaderTest method testRegExpWithNew.
@Test
public void testRegExpWithNew() {
String json = "new RegExp(\"abc\",\"im\")";
bsonReader = new JsonReader(json);
assertEquals(BsonType.REGULAR_EXPRESSION, bsonReader.readBsonType());
BsonRegularExpression regularExpression = bsonReader.readRegularExpression();
assertEquals("abc", regularExpression.getPattern());
assertEquals("im", regularExpression.getOptions());
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class ListCollectionsOperation method asQueryDocument.
private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
BsonDocument document = new BsonDocument();
BsonDocument transformedFilter = null;
if (filter != null) {
if (filter.containsKey("name")) {
if (!filter.isString("name")) {
throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field " + "must be a string");
}
transformedFilter = new BsonDocument();
transformedFilter.putAll(filter);
transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue())));
} else {
transformedFilter = filter;
}
}
BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$"));
BsonDocument query = transformedFilter == null ? indexExcludingRegex : new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex, transformedFilter)));
document.put("$query", query);
if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) {
document.put("$readPreference", readPreference.toDocument());
}
if (maxTimeMS > 0) {
document.put("$maxTimeMS", new BsonInt64(maxTimeMS));
}
return document;
}
Aggregations