本文整理汇总了Java中org.bson.BsonDocument.put方法的典型用法代码示例。如果您正苦于以下问题:Java BsonDocument.put方法的具体用法?Java BsonDocument.put怎么用?Java BsonDocument.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bson.BsonDocument
的用法示例。
在下文中一共展示了BsonDocument.put方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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));
}
示例2: 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;
}
示例3: get
import org.bson.BsonDocument; //导入方法依赖的package包/类
@Override
public Bson get() {
BsonDocument orderByObject = new BsonDocument();
List<OrderBy> orderBys = getOrderBys();
for (OrderBy orderBy : orderBys) {
orderByObject.put(orderBy.getName(), new BsonInt32(orderBy.isAscending() ? 1 : -1));
}
return orderByObject;
}
示例4: getBsonNode
import org.bson.BsonDocument; //导入方法依赖的package包/类
private static BsonDocument getBsonNode(Diff diff, EnumSet<DiffFlags> flags) {
BsonDocument bsonNode = new BsonDocument();
bsonNode.put(Constants.OP, new BsonString(diff.getOperation().rfcName()));
switch (diff.getOperation()) {
case MOVE:
case COPY:
bsonNode.put(Constants.FROM, new BsonString(PathUtils.getPathRepresentation(diff.getPath()))); // required {from} only in case of Move Operation
bsonNode.put(Constants.PATH, new BsonString(PathUtils.getPathRepresentation(diff.getToPath()))); // destination Path
break;
case REMOVE:
bsonNode.put(Constants.PATH, new BsonString(PathUtils.getPathRepresentation(diff.getPath())));
if (!flags.contains(DiffFlags.OMIT_VALUE_ON_REMOVE))
bsonNode.put(Constants.VALUE, diff.getValue());
break;
case REPLACE:
if (flags.contains(DiffFlags.ADD_ORIGINAL_VALUE_ON_REPLACE)) {
bsonNode.put(Constants.FROM_VALUE, diff.getSrcValue());
}
case ADD:
case TEST:
bsonNode.put(Constants.PATH, new BsonString(PathUtils.getPathRepresentation(diff.getPath())));
bsonNode.put(Constants.VALUE, diff.getValue());
break;
default:
// Safety net
throw new IllegalArgumentException("Unknown operation specified:" + diff.getOperation());
}
return bsonNode;
}
示例5: testRenderedRemoveOperationOmitsValueByDefault
import org.bson.BsonDocument; //导入方法依赖的package包/类
@Test
public void testRenderedRemoveOperationOmitsValueByDefault() throws Exception {
BsonDocument source = new BsonDocument();
BsonDocument target = new BsonDocument();
source.put("field", new BsonString("value"));
BsonArray diff = BsonDiff.asBson(source, target);
Assert.assertEquals(Operation.REMOVE.rfcName(), diff.get(0).asDocument().getString("op").getValue());
Assert.assertEquals("/field", diff.get(0).asDocument().getString("path").getValue());
Assert.assertNull(diff.get(0).asDocument().get("value"));
}
示例6: testRenderedRemoveOperationRetainsValueIfOmitDiffFlagNotSet
import org.bson.BsonDocument; //导入方法依赖的package包/类
@Test
public void testRenderedRemoveOperationRetainsValueIfOmitDiffFlagNotSet() throws Exception {
BsonDocument source = new BsonDocument();
BsonDocument target = new BsonDocument();
source.put("field", new BsonString("value"));
EnumSet<DiffFlags> flags = DiffFlags.defaults().clone();
Assert.assertTrue("Expected OMIT_VALUE_ON_REMOVE by default", flags.remove(DiffFlags.OMIT_VALUE_ON_REMOVE));
BsonArray diff = BsonDiff.asBson(source, target, flags);
Assert.assertEquals(Operation.REMOVE.rfcName(), diff.get(0).asDocument().getString("op").getValue());
Assert.assertEquals("/field", diff.get(0).asDocument().getString("path").getValue());
Assert.assertEquals("value", diff.get(0).asDocument().getString("value").getValue());
}
示例7: generate
import org.bson.BsonDocument; //导入方法依赖的package包/类
public static BsonArray generate(int count) {
BsonArray jsonNode = new BsonArray();
for (int i = 0; i < count; i++) {
BsonDocument objectNode = new BsonDocument();
objectNode.put("name", new BsonString(name.get(random.nextInt(name.size()))));
objectNode.put("age", new BsonInt32(age.get(random.nextInt(age.size()))));
objectNode.put("gender", new BsonString(gender.get(random.nextInt(gender.size()))));
BsonArray countryNode = getArrayNode(country.subList(random.nextInt(country.size() / 2), (country.size() / 2) + random.nextInt(country.size() / 2)));
objectNode.put("country", countryNode);
BsonArray friendNode = getArrayNode(friends.subList(random.nextInt(friends.size() / 2), (friends.size() / 2) + random.nextInt(friends.size() / 2)));
objectNode.put("friends", friendNode);
jsonNode.add(objectNode);
}
return jsonNode;
}
示例8: getSort
import org.bson.BsonDocument; //导入方法依赖的package包/类
@Override
public Bson getSort() {
BsonDocument orderBy = new BsonDocument();
for (SortCriteria sortCriteria : getSortCriterias()) {
if (sortCriteria.isDescending()) {
orderBy.put(sortCriteria.getAttribute().getName(), new BsonInt32(-1));
} else {
orderBy.put(sortCriteria.getAttribute().getName(), new BsonInt32(1));
}
}
return orderBy;
}
示例9: addToObject
import org.bson.BsonDocument; //导入方法依赖的package包/类
private void addToObject(List<String> path, BsonValue node, BsonValue value) {
final BsonDocument target = node.asDocument();
String key = path.get(path.size() - 1).replaceAll("\"", "");
target.put(key, value);
}
示例10: encode
import org.bson.BsonDocument; //导入方法依赖的package包/类
public void encode(BsonDocument bsonDocument, Object object, BsonMapperConfig bsonMapperConfig) {
MAPPER_LAYER_COUNTER.addCount(bsonMapperConfig);
try {
Map<String, Field> bsonNameFieldInfoMap = getBsonNameFieldInfoMap(object.getClass());
for (Entry<String, Field> entry : bsonNameFieldInfoMap.entrySet()) {
String bsonName = entry.getKey();
Field field = entry.getValue();
Object fieldValue = Utils.getFieldValue(field, object);
if (fieldValue == null || Utils.isIgnored(field)) {
continue;
}
Class<?> fieldType = field.getType();
if (Utils.isArrayType(fieldType)) {
BsonArray bsonArray = new BsonArray();
BsonValueConverterRepertory.getBsonArrayConverter().encode(bsonArray, field, fieldValue, bsonMapperConfig);
bsonDocument.put(bsonName, bsonArray);
continue;
}
if (BsonValueConverterRepertory.isCanConverterValueType(fieldType)) {
if (Utils.fieldIsObjectId(field)) {
if (fieldType == String.class) {
fieldValue = new StringObjectId(new ObjectId((String) fieldValue));
fieldType = StringObjectId.class;
}
}
BsonValueConverter<Object, BsonValue> valueConverter = BsonValueConverterRepertory.getValueConverterByClazz(fieldType);
if (valueConverter != null) {
bsonDocument.put(bsonName, valueConverter.encode(fieldValue));
} else {
// maybe log warn message to remind user add converter
}
} else {
BsonDocument valueDocument = new BsonDocument();
BsonValueConverterRepertory.getBsonDocumentConverter().encode(valueDocument, fieldValue, bsonMapperConfig);
bsonDocument.put(bsonName, valueDocument);
}
}
} finally {
MAPPER_LAYER_COUNTER.reduceCount();
}
}