static BsonDocument getClusterTime(final BsonDocument response) { BsonValue clusterTime = response.get("$clusterTime"); if (clusterTime == null) { return null; } return clusterTime.asDocument(); }
/** * Returns the hint for which index to use. The default is not to set a hint. * * @return the hint * @since 3.6 * @mongodb.server.release 3.6 */ public BsonDocument getHint() { if (hint == null) { return null; } if (!hint.isDocument()) { throw new IllegalArgumentException("Hint is not a BsonDocument please use the #getHintBsonValue() method. "); } return hint.asDocument(); }
/** * Gets the value of the key if it is a BsonDocument, or throws if not. * * @param key the key * @return the value of the key as a BsonDocument * @throws org.bson.BsonInvalidOperationException if the document does not contain the key or the value is not a BsonDocument */ public BsonDocument getDocument(final Object key) { throwIfKeyAbsent(key); return get(key).asDocument(); }
/** * Returns the hint for which index to use. The default is not to set a hint. * * @return the hint * @since 3.6 * @mongodb.server.release 3.6 */ public BsonDocument getHint() { BsonValue hint = wrapped.getHint(); if (hint == null) { return null; } if (!hint.isDocument()) { throw new IllegalArgumentException("Hint is not a BsonDocument please use the #getHintBsonValue() method. "); } return hint.asDocument(); }
/** * If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a * BsonDocument. * * @param key the key * @param defaultValue the default value * @return the value of the key as a BsonDocument * @throws org.bson.BsonInvalidOperationException if the document contains the key but the value is not of the expected type */ public BsonDocument getDocument(final Object key, final BsonDocument defaultValue) { if (!containsKey(key)) { return defaultValue; } return get(key).asDocument(); }
@Override public List<BatchCursor<T>> apply(final BsonDocument result, final ServerAddress serverAddress) { List<BatchCursor<T>> cursors = new ArrayList<BatchCursor<T>>(); for (BsonValue cursorValue : getCursorDocuments(result)) { cursors.add(new QueryBatchCursor<T>(createQueryResult(getCursorDocument(cursorValue.asDocument()), source.getServerDescription().getAddress()), 0, getBatchSize(), decoder, source)); } return cursors; } };
@Override public List<AsyncBatchCursor<T>> apply(final BsonDocument result, final ServerAddress serverAddress) { List<AsyncBatchCursor<T>> cursors = new ArrayList<AsyncBatchCursor<T>>(); for (BsonValue cursorValue : getCursorDocuments(result)) { cursors.add(new AsyncQueryBatchCursor<T>(createQueryResult(getCursorDocument(cursorValue.asDocument()), source.getServerDescription().getAddress()), 0, getBatchSize(), 0, decoder, source, connection)); } return cursors; } };
static MongoException createSpecialWriteConcernException(final ResponseBuffers responseBuffers, final ServerAddress serverAddress) { BsonValue writeConcernError = getField(createBsonReader(responseBuffers), "writeConcernError"); if (writeConcernError == null) { return null; } else { return createSpecialException(writeConcernError.asDocument(), serverAddress, "errmsg"); } }
static BsonDocument getClusterTime(final ResponseBuffers responseBuffers) { try { BsonValue clusterTime = getField(createBsonReader(responseBuffers), "$clusterTime"); if (clusterTime == null) { return null; } return clusterTime.asDocument(); } finally { responseBuffers.reset(); } }
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 BsonDocument clone() { BsonDocument to = new BsonDocument(); for (Entry<String, BsonValue> cur : entrySet()) { switch (cur.getValue().getBsonType()) { case DOCUMENT: to.put(cur.getKey(), cur.getValue().asDocument().clone()); break; case ARRAY: to.put(cur.getKey(), cur.getValue().asArray().clone()); break; case BINARY: to.put(cur.getKey(), BsonBinary.clone(cur.getValue().asBinary())); break; case JAVASCRIPT_WITH_SCOPE: to.put(cur.getKey(), BsonJavaScriptWithScope.clone(cur.getValue().asJavaScriptWithScope())); break; default: to.put(cur.getKey(), cur.getValue()); } } return to; }
/** * For internal use only: extract the error code from the response to a write command. * @param response the response * @return the code, or -1 if there is none */ public static int extractErrorCode(final BsonDocument response) { // mongos may set an err field containing duplicate key error information String errorMessage = extractErrorMessage(response); if (errorMessage != null) { if (response.containsKey("err")) { if (errorMessage.contains("E11000 duplicate key error")) { return 11000; } } // mongos may return a list of documents representing write command responses from each shard. Return the one with a matching // "err" field, so that it can be used to get the error code if (!response.containsKey("code") && response.containsKey("errObjects")) { for (BsonValue curErrorDocument : response.getArray("errObjects")) { if (errorMessage.equals(extractErrorMessage(curErrorDocument.asDocument()))) { return curErrorDocument.asDocument().getNumber("code").intValue(); } } } } return response.getNumber("code", new BsonInt32(-1)).intValue(); }
@Override protected void doReadStartDocument() { BsonDocument document; if (currentValue.getBsonType() == BsonType.JAVASCRIPT_WITH_SCOPE) { document = currentValue.asJavaScriptWithScope().getScope(); } else { document = currentValue.asDocument(); } setContext(new Context(getContext(), BsonContextType.DOCUMENT, document)); }
resumeToken = changeStreamOptions.getResumeToken().get().asDocument();
private BsonDocument createFilter(final String fieldName, final BsonValue value) { if (fieldName.startsWith("$")) { return new BsonDocument("$not", new BsonDocument(fieldName, value)); } else if ((value.isDocument() && containsOperator(value.asDocument())) || value.isRegularExpression()) { return new BsonDocument(fieldName, new BsonDocument("$not", value)); } return new BsonDocument(fieldName, new BsonDocument("$not", new BsonDocument("$eq", value))); }
@Override public BsonArray clone() { BsonArray to = new BsonArray(); for (BsonValue cur : this) { switch (cur.getBsonType()) { case DOCUMENT: to.add(cur.asDocument().clone()); break; case ARRAY: to.add(cur.asArray().clone()); break; case BINARY: to.add(BsonBinary.clone(cur.asBinary())); break; case JAVASCRIPT_WITH_SCOPE: to.add(BsonJavaScriptWithScope.clone(cur.asJavaScriptWithScope())); break; default: to.add(cur); } } return to; } }
BsonDocument docs = keyValuesforSchema.getValue().asDocument(); final Map<String, BsonType> union = new HashMap<>(); for (BsonValue element: keyValuesforSchema.getValue().asArray()) { final BsonDocument arrayDocs = element.asDocument(); for (Entry<String, BsonValue> arrayDoc: arrayDocs.entrySet()) { final BsonType prevType = union.putIfAbsent(arrayDoc.getKey(), arrayDoc.getValue().getBsonType());
return value.asArray().toArray(); case DOCUMENT: return Document.parse(value.asDocument().toJson()); default: return value;
check(doc.get("array").asArray()).isEmpty(); check(doc.get("object").getBsonType()).is(BsonType.DOCUMENT); check(doc.get("object").asDocument().keySet()).isEmpty();
private void pipeValue(final BsonValue value) { switch (value.getBsonType()) { case DOCUMENT: pipeDocument(value.asDocument()); break; case ARRAY: