org.bson.conversions.Bson类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(334)

本文整理了Java中org.bson.conversions.Bson类的一些代码示例,展示了Bson类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bson类的具体详情如下:
包路径:org.bson.conversions.Bson
类名称:Bson

Bson介绍

[英]An interface for types that are able to render themselves into a BsonDocument.
[中]类型的接口,这些类型能够将自己呈现到BsonDocument中。

代码示例

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. BsonDocument toBsonDocumentOrNull(final Bson document) {
  2. return document == null ? null : document.toBsonDocument(documentClass, codecRegistry);
  3. }
  4. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. private BsonDocument toBsonDocument(final Bson bson) {
  2. return bson == null ? null : bson.toBsonDocument(documentClass, codecRegistry);
  3. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. private List<BsonDocument> createBsonDocumentList(final List<? extends Bson> pipeline) {
  2. List<BsonDocument> aggregateList = new ArrayList<BsonDocument>(pipeline.size());
  3. for (Bson obj : pipeline) {
  4. if (obj == null) {
  5. throw new IllegalArgumentException("pipeline cannot contain a null value");
  6. }
  7. aggregateList.add(obj.toBsonDocument(BsonDocument.class, codecRegistry));
  8. }
  9. return aggregateList;
  10. }
  11. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. private List<BsonDocument> createBsonDocumentList(final List<? extends Bson> pipeline) {
  2. notNull("pipeline", pipeline);
  3. List<BsonDocument> bsonDocumentPipeline = new ArrayList<BsonDocument>(pipeline.size());
  4. for (Bson obj : pipeline) {
  5. if (obj == null) {
  6. throw new IllegalArgumentException("pipeline can not contain a null value");
  7. }
  8. bsonDocumentPipeline.add(obj.toBsonDocument(BsonDocument.class, codecRegistry));
  9. }
  10. return bsonDocumentPipeline;
  11. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. return new BsonDocument(name, value.toBsonDocument(documentClass, codecRegistry));
  4. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Nullable
  2. private BsonValue getOutCollection() {
  3. if (pipeline.size() == 0) {
  4. return null;
  5. }
  6. Bson lastStage = notNull("last stage", pipeline.get(pipeline.size() - 1));
  7. return lastStage.toBsonDocument(documentClass, codecRegistry).get("$out");
  8. }
  9. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Nullable
  2. private BsonDocument toBsonDocument(@Nullable final Bson document) {
  3. return document == null ? null : document.toBsonDocument(BsonDocument.class, codecRegistry);
  4. }
  5. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. /**
  2. * Get a list of index names for the given list of index models
  3. *
  4. * @param indexes the index models
  5. * @param codecRegistry the codec registry to convert each Bson key to a BsonDocument
  6. * @return the list of index names
  7. */
  8. public static List<String> getIndexNames(final List<IndexModel> indexes, final CodecRegistry codecRegistry) {
  9. List<String> indexNames = new ArrayList<String>(indexes.size());
  10. for (IndexModel index : indexes) {
  11. String name = index.getOptions().getName();
  12. if (name != null) {
  13. indexNames.add(name);
  14. } else {
  15. indexNames.add(IndexHelper.generateIndexName(index.getKeys().toBsonDocument(BsonDocument.class, codecRegistry)));
  16. }
  17. }
  18. return indexNames;
  19. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. return new BsonDocument(fieldName, new BsonDocument("$elemMatch", filter.toBsonDocument(documentClass, codecRegistry)));
  4. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. return new BsonDocument(fieldName, new BsonDocument("$elemMatch", filter.toBsonDocument(documentClass, codecRegistry)));
  4. }
  5. };

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. BsonDocument filterDocument = filter.toBsonDocument(documentClass, codecRegistry);
  4. if (filterDocument.size() == 1) {
  5. Map.Entry<String, BsonValue> entry = filterDocument.entrySet().iterator().next();
  6. return createFilter(entry.getKey(), entry.getValue());
  7. } else {
  8. BsonArray values = new BsonArray();
  9. for (Map.Entry<String, BsonValue> docs : filterDocument.entrySet()) {
  10. values.add(new BsonDocument(docs.getKey(), docs.getValue()));
  11. }
  12. return createFilter("$and", values);
  13. }
  14. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. DropIndexOperation dropIndex(final Bson keys, final DropIndexOptions dropIndexOptions) {
  2. return new DropIndexOperation(namespace, keys.toBsonDocument(BsonDocument.class, codecRegistry), writeConcern)
  3. .maxTime(dropIndexOptions.getMaxTime(MILLISECONDS), MILLISECONDS);
  4. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentClass, final CodecRegistry codecRegistry) {
  3. BsonDocument document = new BsonDocument();
  4. for (Bson update : updates) {
  5. BsonDocument rendered = update.toBsonDocument(tDocumentClass, codecRegistry);
  6. for (Map.Entry<String, BsonValue> element : rendered.entrySet()) {
  7. if (document.containsKey(element.getKey())) {
  8. BsonDocument currentOperatorDocument = (BsonDocument) element.getValue();
  9. BsonDocument existingOperatorDocument = document.getDocument(element.getKey());
  10. for (Map.Entry<String, BsonValue> currentOperationDocumentElements : currentOperatorDocument.entrySet()) {
  11. existingOperatorDocument.append(currentOperationDocumentElements.getKey(),
  12. currentOperationDocumentElements.getValue());
  13. }
  14. } else {
  15. document.append(element.getKey(), element.getValue());
  16. }
  17. }
  18. }
  19. return document;
  20. }

代码示例来源:origin: immutables/immutables

  1. /**
  2. * Convert criteria to mongo query (for testing). Currently using reflection (since exposed
  3. * only in tests).
  4. *
  5. * @return Query (as {@link BsonDocument}) which will be sent to mongo.
  6. */
  7. public static BsonDocument extractQuery(Repositories.Criteria criteria) {
  8. Preconditions.checkNotNull(criteria, "criteria");
  9. try {
  10. final Field field = criteria.getClass().getDeclaredField("constraint");
  11. field.setAccessible(true);
  12. Constraints.Constraint constraint = (Constraints.Constraint) field.get(criteria);
  13. return Support.convertToBson(constraint)
  14. .toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry());
  15. } catch (NoSuchFieldException e) {
  16. throw new RuntimeException("private field 'constraint' not found in " + criteria, e);
  17. } catch (IllegalAccessException e) {
  18. throw new RuntimeException(e);
  19. }
  20. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. BsonDocument compoundIndex = new BsonDocument();
  4. for (Bson index : indexes) {
  5. BsonDocument indexDocument = index.toBsonDocument(documentClass, codecRegistry);
  6. for (String key : indexDocument.keySet()) {
  7. compoundIndex.append(key, indexDocument.get(key));
  8. }
  9. }
  10. return compoundIndex;
  11. }
  12. };

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. BsonDocument combinedDocument = new BsonDocument();
  4. for (Bson sort : sorts) {
  5. BsonDocument sortDocument = sort.toBsonDocument(documentClass, codecRegistry);
  6. for (String key : sortDocument.keySet()) {
  7. combinedDocument.append(key, sortDocument.get(key));
  8. }
  9. }
  10. return combinedDocument;
  11. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. BsonDocument orRenderable = new BsonDocument();
  4. BsonArray filtersArray = new BsonArray();
  5. for (Bson filter : filters) {
  6. filtersArray.add(filter.toBsonDocument(documentClass, codecRegistry));
  7. }
  8. orRenderable.put(operator.name, filtersArray);
  9. return orRenderable;
  10. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @Override
  2. public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
  3. BsonDocument combinedDocument = new BsonDocument();
  4. for (Bson sort : projections) {
  5. BsonDocument sortDocument = sort.toBsonDocument(documentClass, codecRegistry);
  6. for (String key : sortDocument.keySet()) {
  7. combinedDocument.remove(key);
  8. combinedDocument.append(key, sortDocument.get(key));
  9. }
  10. }
  11. return combinedDocument;
  12. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. <TResult> DistinctOperation<TResult> distinct(final String fieldName, final Bson filter,
  2. final Class<TResult> resultClass, final long maxTimeMS,
  3. final Collation collation) {
  4. return new DistinctOperation<TResult>(namespace, fieldName, codecRegistry.get(resultClass))
  5. .filter(filter == null ? null : filter.toBsonDocument(documentClass, codecRegistry))
  6. .maxTime(maxTimeMS, MILLISECONDS)
  7. .collation(collation);
  8. }

代码示例来源:origin: org.mongodb/mongo-java-driver

  1. @SuppressWarnings("unchecked")
  2. static <TItem> void encodeValue(final BsonDocumentWriter writer, final TItem value, final CodecRegistry codecRegistry) {
  3. if (value == null) {
  4. writer.writeNull();
  5. } else if (value instanceof Bson) {
  6. ((Encoder) codecRegistry.get(BsonDocument.class)).encode(writer,
  7. ((Bson) value).toBsonDocument(BsonDocument.class, codecRegistry),
  8. EncoderContext.builder().build());
  9. } else {
  10. ((Encoder) codecRegistry.get(value.getClass())).encode(writer, value, EncoderContext.builder().build());
  11. }
  12. }

相关文章