/** * Gets the first key in the document. * * @return the first key in the document * @throws java.util.NoSuchElementException if the document is empty * @since 3.6 */ public String getFirstKey() { return keySet().iterator().next(); }
private boolean containsOperator(final BsonDocument value) { Set<String> keys = value.keySet(); if (keys.equals(DBREF_KEYS) || keys.equals(DBREF_KEYS_WITH_DB)) { return false; } for (String key : keys) { if (key.startsWith("$")) { return true; } } return false; }
private boolean keysIntersect(final BsonDocument first, final BsonDocument second) { for (String name : first.keySet()) { if (second.containsKey(name)) { return true; } } return false; }
@Override public Set<String> keySet() { return getUnwrapped().keySet(); }
@Override public Set<String> keySet() { return toBsonDocument().keySet(); }
@Override public Set<String> keySet() { return toBsonDocument().keySet(); }
/** * Convenience method to generate an index name from the set of fields it is over. * * @return a string representation of this index's fields */ public static String generateIndexName(final BsonDocument index) { StringBuilder indexName = new StringBuilder(); for (final String keyNames : index.keySet()) { if (indexName.length() != 0) { indexName.append('_'); } indexName.append(keyNames).append('_'); BsonValue ascOrDescValue = index.get(keyNames); if (ascOrDescValue instanceof BsonNumber) { indexName.append(((BsonNumber) ascOrDescValue).intValue()); } else if (ascOrDescValue instanceof BsonString) { indexName.append(((BsonString) ascOrDescValue).getValue().replace(' ', '_')); } } return indexName.toString(); }
private boolean sendQueryStartedEvent(final InternalConnection connection, final QueryMessage message, final ByteBufferBsonOutput bsonOutput, final RequestMessage.EncodingMetadata metadata) { boolean isExplainEvent = false; if (commandListener != null) { BsonDocument command = asFindCommandDocument(bsonOutput, metadata.getFirstDocumentPosition()); isExplainEvent = command.keySet().iterator().next().equals(EXPLAIN_COMMAND_NAME); sendCommandStartedEvent(message, namespace.getDatabaseName(), getCommandName(isExplainEvent), command, connection.getDescription(), commandListener); } return isExplainEvent; }
@Override public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) { BsonDocument compoundIndex = new BsonDocument(); for (Bson index : indexes) { BsonDocument indexDocument = index.toBsonDocument(documentClass, codecRegistry); for (String key : indexDocument.keySet()) { compoundIndex.append(key, indexDocument.get(key)); } } return compoundIndex; } };
@Override public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) { BsonDocument combinedDocument = new BsonDocument(); for (Bson sort : sorts) { BsonDocument sortDocument = sort.toBsonDocument(documentClass, codecRegistry); for (String key : sortDocument.keySet()) { combinedDocument.append(key, sortDocument.get(key)); } } return combinedDocument; }
private void addClause(final BsonDocument document, final Map.Entry<String, BsonValue> clause) { if (clause.getKey().equals("$and")) { for (BsonValue value : clause.getValue().asArray()) { for (Map.Entry<String, BsonValue> element : value.asDocument().entrySet()) { addClause(document, element); } } } else if (document.size() == 1 && document.keySet().iterator().next().equals("$and")) { document.get("$and").asArray().add(new BsonDocument(clause.getKey(), clause.getValue())); } else if (document.containsKey(clause.getKey())) { if (document.get(clause.getKey()).isDocument() && clause.getValue().isDocument()) { BsonDocument existingClauseValue = document.get(clause.getKey()).asDocument(); BsonDocument clauseValue = clause.getValue().asDocument(); if (keysIntersect(clauseValue, existingClauseValue)) { promoteRenderableToDollarForm(document, clause); } else { existingClauseValue.putAll(clauseValue); } } else { promoteRenderableToDollarForm(document, clause); } } else { document.append(clause.getKey(), clause.getValue()); } }
@Override public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) { BsonDocument combinedDocument = new BsonDocument(); for (Bson sort : projections) { BsonDocument sortDocument = sort.toBsonDocument(documentClass, codecRegistry); for (String key : sortDocument.keySet()) { combinedDocument.remove(key); combinedDocument.append(key, sortDocument.get(key)); } } return combinedDocument; }
@Test public void withDate() { final Date date = new Date(); final ObjectId id = ObjectId.get(); final Jackson expected = ImmutableJackson.builder() .id(id) .prop1("prop1") .prop2("22") .date(new Date(date.getTime())) .build(); repository.insert(expected).getUnchecked(); check(collection.count()).is(1L); final Jackson actual = repository.findAll().fetchAll().getUnchecked().get(0); check(expected).is(actual); final BsonDocument doc = collection.find().first(); check(doc.keySet()).hasContentInAnyOrder("_id", "prop1", "prop2", "date", "uuid"); check(doc.get("date").asDateTime().getValue()).is(date.getTime()); check(doc.get("_id").asObjectId().getValue()).is(id); }
@Test public void dateCodec() throws IOException { TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec()); Date date = new Date(); BsonDocument doc = new BsonDocument(); BsonDocumentWriter writer = new BsonDocumentWriter(doc); writer.writeStartDocument(); writer.writeName("$date"); adapter.write(new BsonWriter(writer), date); writer.writeEndDocument(); check(doc.keySet()).hasSize(1); check(doc.get("$date").getBsonType()).is(BsonType.DATE_TIME); check(doc.get("$date").asDateTime().getValue()).is(date.getTime()); } }
/** * persist empty Optional of Date */ @Test public void nullDate() { final Jackson expected = ImmutableJackson.builder() .id(ObjectId.get()) .prop1("prop11") .prop2("prop22") .build(); repository.insert(expected).getUnchecked(); final Jackson actual = repository.findAll() .fetchAll().getUnchecked().get(0); check(expected.date().asSet()).isEmpty(); check(expected).is(actual); final BsonDocument doc = collection.find().first(); check(doc.keySet()).hasContentInAnyOrder("_id", "prop1", "prop2", "date", "uuid"); check(doc.get("date")).is(BsonNull.VALUE); }
/** * * @return */ public String getRef() { return (String) doc.keySet().toArray()[0]; }
private static boolean _containsUpdateOperators(BsonDocument json, boolean ignoreCurrentDate) { if (json == null) { return false; } return json.asDocument().keySet().stream() .filter(key -> !ignoreCurrentDate || !key.equals("$currentDate")) .anyMatch(key -> isUpdateOperator(key)); }
public static void unescapeSchema(BsonDocument schema) { BsonValue unescaped = JsonUtils.unescapeKeys(schema); if (unescaped != null && unescaped.isDocument()) { List<String> keys = Lists.newArrayList(schema.keySet().iterator()); keys.stream().forEach(f -> schema.remove(f)); schema.putAll(unescaped.asDocument()); } }
public static void escapeSchema(BsonDocument schema) { BsonValue escaped = JsonUtils.escapeKeys(schema, false); if (escaped.isDocument()) { List<String> keys = Lists.newArrayList(schema.keySet().iterator()); keys.stream().forEach(f -> schema.remove(f)); schema.putAll(escaped.asDocument()); } }