本文整理匯總了Java中org.bson.BsonDocument類的典型用法代碼示例。如果您正苦於以下問題:Java BsonDocument類的具體用法?Java BsonDocument怎麽用?Java BsonDocument使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BsonDocument類屬於org.bson包,在下文中一共展示了BsonDocument類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testPatchAppliedCleanly
import org.bson.BsonDocument; //導入依賴的package包/類
@Test
public void testPatchAppliedCleanly() throws Exception {
for (int i = 0; i < jsonNode.size(); i++) {
BsonDocument node = jsonNode.get(i).asDocument();
BsonValue first = node.get("first");
BsonValue second = node.get("second");
BsonArray patch = node.getArray("patch");
String message = node.containsKey("message") ? node.getString("message").getValue() : "";
// System.out.println("Test # " + i);
// System.out.println(first);
// System.out.println(second);
// System.out.println(patch);
BsonValue secondPrime = BsonPatch.apply(patch, first);
// System.out.println(secondPrime);
Assert.assertThat(message, secondPrime, equalTo(second));
}
}
示例2: testRenderedOperationsExceptMoveAndCopy
import org.bson.BsonDocument; //導入依賴的package包/類
@Test
public void testRenderedOperationsExceptMoveAndCopy() throws Exception {
BsonDocument source = new BsonDocument();
source.put("age", new BsonInt32(10));
BsonDocument target = new BsonDocument();
target.put("height", new BsonInt32(10));
EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone(); //only have ADD, REMOVE, REPLACE, Don't normalize operations into MOVE & COPY
BsonArray diff = BsonDiff.asBson(source, target, flags);
// System.out.println(source);
// System.out.println(target);
// System.out.println(diff);
for (BsonValue d : diff) {
Assert.assertNotEquals(Operation.MOVE.rfcName(), d.asDocument().getString("op").getValue());
Assert.assertNotEquals(Operation.COPY.rfcName(), d.asDocument().getString("op").getValue());
}
BsonValue targetPrime = BsonPatch.apply(diff, source);
// System.out.println(targetPrime);
Assert.assertTrue(target.equals(targetPrime));
}
示例3: convertBinaryUUID
import org.bson.BsonDocument; //導入依賴的package包/類
/**
* Converts a binary uuid to a standard uuid
*
* @param json The json string (should contain "$binary" and "$type" or something like that)
* @return The uuid
*/
private UUID convertBinaryUUID(String key, String json) {
BsonDocument document = BsonDocument.parse(json);
BsonValue value = document.get(key);
if(!(value instanceof BsonBinary)) {
return null;
}
byte[] bytes = ((BsonBinary) value).getData();
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(ByteOrder.LITTLE_ENDIAN);
long l1 = bb.getLong();
long l2 = bb.getLong();
return new UUID(l1, l2);
}
示例4: getBsonValueList
import org.bson.BsonDocument; //導入依賴的package包/類
private ArrayList<BsonValue> getBsonValueList(Field field, Collection values, BsonMapperConfig bsonMapperConfig, Class<?> componentType) {
ArrayList<BsonValue> arrayList = new ArrayList<BsonValue>();
for (Object o : values) {
if (o == null) {
continue;
}
Class<?> oClazz = o.getClass();
if (Utils.isArrayType(oClazz)) {
BsonArray innerBsonArray = new BsonArray();
encode(innerBsonArray, field, o, bsonMapperConfig);
arrayList.add(innerBsonArray);
}
if (componentType.isInstance(o)) {
if (BsonValueConverterRepertory.isCanConverterValueType(componentType)) {
arrayList.add(BsonValueConverterRepertory.getValueConverterByClazz(componentType).encode(o));
} else {
BsonDocument arrayEle = new BsonDocument();
BsonValueConverterRepertory.getBsonDocumentConverter().encode(arrayEle, o, bsonMapperConfig);
arrayList.add(arrayEle);
}
} else {
throw new BsonMapperConverterException(String.format("array field has element which has different type with declaring componentType.field name: %s", field.getName()));
}
}
return arrayList;
}
示例5: readFrom
import org.bson.BsonDocument; //導入依賴的package包/類
@Test
public void readFrom() throws Exception {
BsonDocument bsonDocument1 = getBsonDocument();
BsonMapper bsonMapper = DefaultBsonMapper.defaultBsonMapper();
BsonTest bsonTest = bsonMapper.readFrom(bsonDocument1, BsonTest.class);
System.out.println(bsonTest.getTestDouble());
System.out.println(bsonTest.getTestString());
System.out.println(bsonTest.getTestArray());
System.out.println(Arrays.toString(bsonTest.getTestBinary().getData()));
System.out.println(bsonTest.getTestObjectId());
System.out.println(bsonTest.getTestStringObjectId());
System.out.println(bsonTest.isTestBoolean());
System.out.println(bsonTest.getTestDate());
System.out.println(bsonTest.getTestNull());
System.out.println(bsonTest.getTestInt());
System.out.println(bsonTest.getTestLong());
System.out.println(bsonTest);
}
示例6: getBsonDocument
import org.bson.BsonDocument; //導入依賴的package包/類
public static BsonDocument getBsonDocument() {
BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777));
List<BsonDocument> list = new ArrayList<BsonDocument>();
list.add(bsonObj);
list.add(bsonObj);
byte[] bytes = new byte[3];
bytes[0] = 3;
bytes[1] = 2;
bytes[2] = 1;
BsonDocument bsonDocument = new BsonDocument().append("testDouble", new BsonDouble(20.99))
.append("testString", new BsonString("testStringV"))
.append("testArray", new BsonArray(list));
return new BsonDocument().append("testDouble", new BsonDouble(20.99))
.append("testString", new BsonString("testStringV"))
.append("testArray", new BsonArray(list))
.append("bson_test", bsonDocument)
.append("testBinary", new BsonBinary(bytes))
.append("testBsonUndefined", new BsonUndefined())
.append("testObjectId", new BsonObjectId())
.append("testStringObjectId", new BsonObjectId())
.append("testBoolean", new BsonBoolean(true))
.append("testDate", new BsonDateTime(time))
.append("testNull", new BsonNull())
.append("testInt", new BsonInt32(233))
.append("testLong", new BsonInt64(233332));
}
示例7: testBsonDocumentDeSerialize
import org.bson.BsonDocument; //導入依賴的package包/類
@Test
public void testBsonDocumentDeSerialize() {
BsonDocument document = new BsonDocument().append("a", new BsonString("MongoDB"))
.append("b", new BsonArray(Arrays.asList(new BsonInt32(1), new BsonInt32(2))))
.append("c", new BsonBoolean(true))
.append("d", new BsonDateTime(0));
String json = oson.useAttribute(false).setValueOnly(true).serialize(document);
String expected = "{\"a\":\"MongoDB\",\"b\":[1,2],\"c\":true,\"d\":0}";
assertEquals(expected, json);
BsonDocument document2 = oson.deserialize(json, BsonDocument.class);
assertEquals(expected, oson.serialize(document2));
}
示例8: testRecursiveDocuments
import org.bson.BsonDocument; //導入依賴的package包/類
@Test
public void testRecursiveDocuments() throws IOException {
BsonDocument topDoc = new BsonDocument();
final int count = 3;
for (int i = 0; i < count; ++i) {
BsonDocument bsonDoc = new BsonDocument();
BsonWriter bw = new BsonDocumentWriter(bsonDoc);
bw.writeStartDocument();
bw.writeName("k1" + i);
bw.writeString("drillMongo1" + i);
bw.writeName("k2" + i);
bw.writeString("drillMongo2" + i);
bw.writeEndDocument();
bw.flush();
topDoc.append("doc" + i, bsonDoc);
}
writer.reset();
bsonReader.write(writer, new BsonDocumentReader(topDoc));
SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
for (int i = 0; i < count; ++i) {
SingleMapReaderImpl reader = (SingleMapReaderImpl) mapReader.reader("doc" + i);
assertEquals("drillMongo1" + i, reader.reader("k1" + i).readText().toString());
assertEquals("drillMongo2" + i, reader.reader("k2" + i).readText().toString());
}
}
示例9: fromGridRef
import org.bson.BsonDocument; //導入依賴的package包/類
private BsonValue fromGridRef(SmofGridRef fileRef, PrimaryField fieldOpts) {
if(fileRef.isEmpty()) {
return new BsonNull();
}
if(fileRef.getId() == null) {
final SmofObject annotation = fieldOpts.getSmofAnnotationAs(SmofObject.class);
if(fileRef.getBucketName() == null) {
fileRef.setBucketName(annotation.bucketName());
}
//TODO test if upload file adds id to fileRef
if(!annotation.preInsert() && !fieldOpts.isForcePreInsert()) {
return new BsonLazyObjectId(fieldOpts.getName(), fileRef);
}
fieldOpts.setForcePreInsert(false);
dispatcher.insert(fileRef);
}
final BsonDocument bsonRef = new BsonDocument("id", new BsonObjectId(fileRef.getId()))
.append("bucket", new BsonString(fileRef.getBucketName()));
return bsonRef;
}
示例10: fromObject
import org.bson.BsonDocument; //導入依賴的package包/類
private BsonDocument fromObject(Object value) {
final BsonDocument document = new BsonDocument();
final TypeParser<?> metadata = getTypeParser(value.getClass());
final BsonArray lazyStack = new BsonArray();
for(PrimaryField field : metadata.getAllFields()) {
final Object fieldValue = extractValue(value, field);
final BsonValue parsedValue;
checkRequired(field, fieldValue);
parsedValue = topParser.toBson(fieldValue, field);
if(parsedValue instanceof BsonLazyObjectId) {
lazyStack.add(parsedValue);
}
else {
document.put(field.getName(), parsedValue);
}
}
document.append(SmofParser.ON_INSERT, lazyStack);
return document;
}
示例11: replace
import org.bson.BsonDocument; //導入依賴的package包/類
private SmofInsertResult replace(T element, SmofOpOptions options) {
final SmofInsertResult result = new SmofInsertResultImpl();
result.setSuccess(true);
options.upsert(true);
if(options.isBypassCache() || !cache.asMap().containsValue(element)) {
final BsonDocument document = parser.toBson(element);
final Bson query = createUniquenessQuery(document);
result.setPostInserts(BsonUtils.extrackPosInsertions(document));
options.setReturnDocument(ReturnDocument.AFTER);
document.remove(Element.ID);
final BsonDocument resDoc = collection.findOneAndReplace(query, document, options.toFindOneAndReplace());
element.setId(resDoc.get(Element.ID).asObjectId().getValue());
cache.put(element.getId(), element);
}
return result;
}
示例12: filterValue
import org.bson.BsonDocument; //導入依賴的package包/類
public static BsonValue filterValue(BsonValue value)
{
BsonValue returnedValue = QUESTION_MARK_BSON;
if (value instanceof BsonDocument)
{
returnedValue = filterParameters((BsonDocument) value);
}
else if (value instanceof BsonArray)
{
BsonArray array = (BsonArray) value;
array = array.clone();
returnedValue = array;
int length = array.size();
for (int i = 0; i < length; ++i)
{
BsonValue bsonValue = array.get(i);
array.set(i, filterValue(bsonValue));
}
}
return returnedValue;
}
示例13: testArrayOfDocumentType
import org.bson.BsonDocument; //導入依賴的package包/類
@Test
public void testArrayOfDocumentType() throws IOException {
BsonDocument bsonDoc = new BsonDocument();
BsonWriter bw = new BsonDocumentWriter(bsonDoc);
bw.writeStartDocument();
bw.writeName("a");
bw.writeString("MongoDB");
bw.writeName("b");
bw.writeStartArray();
bw.writeStartDocument();
bw.writeName("c");
bw.writeInt32(1);
bw.writeEndDocument();
bw.writeEndArray();
bw.writeEndDocument();
bw.flush();
writer.reset();
bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
FieldReader reader = writer.getMapVector().getReader();
SingleMapReaderImpl mapReader = (SingleMapReaderImpl) reader;
FieldReader reader3 = mapReader.reader("b");
assertEquals("MongoDB", mapReader.reader("a").readText().toString());
}
示例14: toBson
import org.bson.BsonDocument; //導入依賴的package包/類
BsonValue toBson() {
@SuppressWarnings("unchecked")
Encoder<T> encoder = BsonEncoding.encoderFor((Class<T>) value.getClass(), adapter);
BsonDocument bson = new BsonDocument();
org.bson.BsonWriter writer = new BsonDocumentWriter(bson);
// Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a document.
writer.writeStartDocument();
writer.writeName("$");
encoder.encode(writer, value, EncoderContext.builder().build());
writer.writeEndDocument();
writer.flush();
return bson.get("$");
}
示例15: bsonToGson
import org.bson.BsonDocument; //導入依賴的package包/類
/**
* Reading from BSON to GSON
*/
@Test
public void bsonToGson() throws Exception {
BsonDocument document = new BsonDocument();
document.append("boolean", new BsonBoolean(true));
document.append("int32", new BsonInt32(32));
document.append("int64", new BsonInt64(64));
document.append("double", new BsonDouble(42.42D));
document.append("string", new BsonString("foo"));
document.append("null", new BsonNull());
document.append("array", new BsonArray());
document.append("object", new BsonDocument());
JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document)));
check(element.isJsonObject());
check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean());
check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean());
check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber());
check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32);
check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber());
check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L);
check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber());
check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D);
check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString());
check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo");
check(element.getAsJsonObject().get("null").isJsonNull());
check(element.getAsJsonObject().get("array").isJsonArray());
check(element.getAsJsonObject().get("object").isJsonObject());
}