use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
Document doc = new Document();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BsonTimestamp(5, 1));
doc.append("pattern", new BsonRegularExpression("abc"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWithScope("code", new Document()));
doc.append("null", null);
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("list", Arrays.asList(7, 8, 9));
doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
collection.insertOne(doc);
Document found = collection.find().first();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BsonTimestamp.class, found.get("ts").getClass());
assertEquals(BsonRegularExpression.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(CodeWithScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(Binary.class, found.get("binary").getClass());
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder.
@Test
public void shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 0);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 1);
Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan);
assertThat("Document, retrieved from getOneAndReplace, should match the document inserted before", returnedDocument, equalTo(pat));
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder.
@Test
public void shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 3);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 7);
Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Worker retrieved from replaceOneAndGet should match the updated Worker", returnedDocument, equalTo(jordan));
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet.
@Test
public void shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet() {
Document originalDocument = new Document(KEY, VALUE_TO_CARE_ABOUT);
collection.insertOne(originalDocument);
assertThat(collection.count(), is(1L));
Document replacementDocument = new Document("_id", new ObjectId()).append("foo", "bar");
Document document = collection.findOneAndReplace(new Document(KEY, "valueThatDoesNotMatch"), replacementDocument, new FindOneAndReplaceOptions().upsert(true).returnDocument(ReturnDocument.AFTER));
assertThat(collection.count(), is(2L));
assertThat("Document retrieved from replaceOneAndGet with filter that doesn't match should match the replacement document", document, equalTo(replacementDocument));
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class GridFSTest method doDelete.
private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
Throwable error = null;
try {
gridFSBucket.delete(arguments.getObjectId("id").getValue());
} catch (MongoGridFSException e) {
error = e;
}
if (assertion.containsKey("error")) {
assertNotNull("Should have thrown an exception", error);
} else {
assertNull("Should not have thrown an exception", error);
for (BsonValue rawDataItem : assertion.getArray("data")) {
BsonDocument dataItem = rawDataItem.asDocument();
for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
String delete = dataItem.getString("delete", new BsonString("none")).getValue();
BsonObjectId id = new BsonObjectId(new ObjectId());
if (delete.equals("expected.files")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
} else if (delete.equals("expected.chunks")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
}
assertEquals(filesCollection.count(new BsonDocument("_id", id)), 0);
assertEquals(chunksCollection.count(new BsonDocument("files_id", id)), 0);
}
}
}
}
Aggregations