use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldUseFriendlyQueryType.
@Test
public void shouldUseFriendlyQueryType() {
collection.insertOne(new Document("product", "Book").append("numTimesOrdered", "some"));
collection.insertOne(new Document("product", "CD").append("numTimesOrdered", "6"));
collection.insertOne(new Document("product", "DVD").append("numTimesOrdered", 9));
collection.insertOne(new Document("product", "SomethingElse").append("numTimesOrdered", 10));
collection.insertOne(new Document("product", "VeryPopular").append("numTimesOrdered", 7843273657286478L));
List<Document> results = new ArrayList<Document>();
//TODO make BSON type serializable
Document filter = new Document("$or", asList(new Document("numTimesOrdered", new Document("$type", INT32.getValue())), new Document("numTimesOrdered", new Document("$type", INT64.getValue()))));
collection.find(filter).sort(new Document("numTimesOrdered", -1)).into(results);
assertThat(results.size(), is(3));
assertThat(results.get(0).get("product").toString(), is("VeryPopular"));
assertThat(results.get(1).get("product").toString(), is("SomethingElse"));
assertThat(results.get(2).get("product").toString(), is("DVD"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldBeAbleToFilterByType.
@Test
public void shouldBeAbleToFilterByType() {
collection.insertOne(new Document("product", "Book").append("numTimesOrdered", "some"));
collection.insertOne(new Document("product", "CD").append("numTimesOrdered", "6"));
collection.insertOne(new Document("product", "DVD").append("numTimesOrdered", 9));
collection.insertOne(new Document("product", "SomethingElse").append("numTimesOrdered", 10));
List<Document> results = new ArrayList<Document>();
collection.find(new Document("numTimesOrdered", new Document("$type", 16))).sort(new Document("numTimesOrdered", -1)).into(results);
assertThat(results.size(), is(2));
assertThat(results.get(0).get("product").toString(), is("SomethingElse"));
assertThat(results.get(1).get("product").toString(), is("DVD"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldBeAbleToSortAscending.
@Test
public void shouldBeAbleToSortAscending() {
collection.insertOne(new Document("product", "Book"));
collection.insertOne(new Document("product", "DVD"));
collection.insertOne(new Document("product", "CD"));
List<Document> results = new ArrayList<Document>();
collection.find().sort(new Document("product", 1)).into(results);
assertThat(results.size(), is(3));
assertThat(results.get(0).get("product").toString(), is("Book"));
assertThat(results.get(1).get("product").toString(), is("CD"));
assertThat(results.get(2).get("product").toString(), is("DVD"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldBeAbleToQueryTypedCollectionWithDocument.
@Test
public void shouldBeAbleToQueryTypedCollectionWithDocument() {
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new PersonCodecProvider()));
MongoCollection<Person> collection = database.getCollection(getCollectionName(), Person.class).withCodecRegistry(codecRegistry);
collection.insertOne(new Person("Bob"));
MongoCursor<Person> results = collection.find(new Document("name", "Bob")).iterator();
assertThat(results.next().name, is("Bob"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class DBCollectionTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
BasicDBObject doc = new BasicDBObject();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("short", (short) 4);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("float", 6.0f);
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BSONTimestamp(5, 1));
doc.append("pattern", Pattern.compile(".*"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWScope("code", new BasicDBObject()));
doc.append("null", null);
doc.append("uuid", UUID.randomUUID());
doc.append("db ref", new com.mongodb.DBRef("test", new ObjectId()));
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("byte array", new byte[] { 1, 2, 3 });
doc.append("int array", new int[] { 4, 5, 6 });
doc.append("list", asList(7, 8, 9));
doc.append("doc list", asList(new Document("x", 1), new Document("x", 2)));
collection.insert(doc);
DBObject found = collection.findOne();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Integer.class, found.get("short").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("float").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BSONTimestamp.class, found.get("ts").getClass());
assertEquals(Pattern.class, found.get("pattern").getClass());
assertEquals(MinKey.class, found.get("minKey").getClass());
assertEquals(MaxKey.class, found.get("maxKey").getClass());
assertEquals(Code.class, found.get("js").getClass());
assertEquals(CodeWScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(UUID.class, found.get("uuid").getClass());
assertEquals(DBRef.class, found.get("db ref").getClass());
assertEquals(Binary.class, found.get("binary").getClass());
assertEquals(byte[].class, found.get("byte array").getClass());
assertTrue(found.get("int array") instanceof List);
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
Aggregations