@Override public String apply(final BsonDocument result) { return result.getString("name").getValue(); } });
@Override public String apply(final BsonDocument result) { return result.getString("name").getValue(); } });
@Override public String apply(final BsonDocument result) { return result.getString("name").getValue(); } });
private static String getString(final BsonDocument response, final String key) { if (response.containsKey(key)) { return response.getString(key).getValue(); } else { return null; } }
private static String extractErrorCodeName(final BsonDocument response) { return response.getString("codeName", new BsonString("")).getValue(); }
static String getErrorMessage(final BsonDocument response, final String errorMessageFieldName) { return response.getString(errorMessageFieldName, new BsonString("")).getValue(); }
private static String extractErrorMessage(final BsonDocument response) { String errorMessage = response.getString("errmsg", new BsonString("")).getValue(); // Satisfy nullability checker if (errorMessage == null) { throw new MongoInternalException("This value should not be null"); } return errorMessage; } }
/** * For internal use only: extract the error message from the response to a write command. * * @param response the response * @return the error message */ @Nullable public static String extractErrorMessage(final BsonDocument response) { if (response.isString("err")) { return response.getString("err").getValue(); } else if (response.isString("errmsg")) { return response.getString("errmsg").getValue(); } else { return null; } }
private List<T> projectFromFullNamespaceToCollectionName(final List<BsonDocument> unstripped) { if (unstripped == null) { return null; } List<T> stripped = new ArrayList<T>(unstripped.size()); String prefix = databaseName + "."; for (BsonDocument cur : unstripped) { String name = cur.getString("name").getValue(); String collectionName = name.substring(prefix.length()); cur.put("name", new BsonString(collectionName)); stripped.add(decoder.decode(new BsonDocumentReader(cur), DecoderContext.builder().build())); } return stripped; } }
/** * Returns the namespace * * The invalidate operation type does include a MongoNamespace in the ChangeStreamDocument response. The * dropDatabase operation type includes a MongoNamespace, but does not include a collection name as part * of the namespace. * * @return the namespace. If the namespaceDocument is null or if it is missing either the 'db' or 'coll' keys, * then this will return null. */ @BsonIgnore @Nullable public MongoNamespace getNamespace() { if (namespaceDocument == null) { return null; } if (!namespaceDocument.containsKey("db") || !namespaceDocument.containsKey("coll")) { return null; } return new MongoNamespace(namespaceDocument.getString("db").getValue(), namespaceDocument.getString("coll").getValue()); }
/** * Returns the database name * * @return the databaseName. If the namespaceDocument is null or if it is missing the 'db' key, then this will * return null. * @since 3.8 */ @BsonIgnore @Nullable public String getDatabaseName() { if (namespaceDocument == null) { return null; } if (!namespaceDocument.containsKey("db")) { return null; } return namespaceDocument.getString("db").getValue(); }
public static WriteConcernError createWriteConcernError(final BsonDocument writeConcernErrorDocument) { return new WriteConcernError(writeConcernErrorDocument.getNumber("code").intValue(), writeConcernErrorDocument.getString("codeName", new BsonString("")).getValue(), writeConcernErrorDocument.getString("errmsg").getValue(), writeConcernErrorDocument.getDocument("errInfo", new BsonDocument())); }
private static <T> QueryResult<T> cursorDocumentToQueryResult(final BsonDocument cursorDocument, final ServerAddress serverAddress, final String fieldNameContainingBatch) { long cursorId = ((BsonInt64) cursorDocument.get("id")).getValue(); MongoNamespace queryResultNamespace = new MongoNamespace(cursorDocument.getString("ns").getValue()); return new QueryResult<T>(queryResultNamespace, BsonDocumentWrapperHelper.<T>toList(cursorDocument, fieldNameContainingBatch), cursorId, serverAddress); }
@SuppressWarnings("unchecked") private static List<BulkWriteError> getWriteErrors(final BsonDocument result) { List<BulkWriteError> writeErrors = new ArrayList<BulkWriteError>(); BsonArray writeErrorsDocuments = (BsonArray) result.get("writeErrors"); if (writeErrorsDocuments != null) { for (BsonValue cur : writeErrorsDocuments) { BsonDocument curDocument = (BsonDocument) cur; writeErrors.add(new BulkWriteError(curDocument.getNumber("code").intValue(), curDocument.getString("errmsg").getValue(), curDocument.getDocument("errInfo", new BsonDocument()), curDocument.getNumber("index").intValue())); } } return writeErrors; }
@SuppressWarnings("unchecked") private List<BulkWriteError> getWriteErrors(final BsonDocument result) { List<BulkWriteError> writeErrors = new ArrayList<BulkWriteError>(); BsonArray writeErrorsDocuments = (BsonArray) result.get("writeErrors"); if (writeErrorsDocuments != null) { for (BsonValue cur : writeErrorsDocuments) { BsonDocument curDocument = (BsonDocument) cur; writeErrors.add(new BulkWriteError(curDocument.getNumber("code").intValue(), curDocument.getString("errmsg").getValue(), curDocument.getDocument("errInfo", new BsonDocument()), curDocument.getNumber("index").intValue())); } } return writeErrors; }
private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) { BsonDocument document = new BsonDocument(); BsonDocument transformedFilter = null; if (filter != null) { if (filter.containsKey("name")) { if (!filter.isString("name")) { throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field " + "must be a string"); } transformedFilter = new BsonDocument(); transformedFilter.putAll(filter); transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue()))); } else { transformedFilter = filter; } } BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$")); BsonDocument query = transformedFilter == null ? indexExcludingRegex : new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex, transformedFilter))); document.put("$query", query); if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) { document.put("$readPreference", readPreference.toDocument()); } if (maxTimeMS > 0) { document.put("$maxTimeMS", new BsonInt64(maxTimeMS)); } return document; }
@Override public GridFSFile decode(final BsonReader reader, final DecoderContext decoderContext) { BsonDocument bsonDocument = bsonDocumentCodec.decode(reader, decoderContext); BsonValue id = bsonDocument.get("_id"); String filename = bsonDocument.get("filename", new BsonString("")).asString().getValue(); long length = bsonDocument.getNumber("length").longValue(); int chunkSize = bsonDocument.getNumber("chunkSize").intValue(); Date uploadDate = new Date(bsonDocument.getDateTime("uploadDate").getValue()); String md5 = bsonDocument.containsKey("md5") ? bsonDocument.getString("md5").getValue() : null; BsonDocument metadataBsonDocument = bsonDocument.getDocument("metadata", new BsonDocument()); Document optionalMetadata = asDocumentOrNull(metadataBsonDocument); for (String key : VALID_FIELDS) { bsonDocument.remove(key); } Document deprecatedExtraElements = asDocumentOrNull(bsonDocument); return new GridFSFile(id, filename, length, chunkSize, uploadDate, md5, optionalMetadata, deprecatedExtraElements); }
public static ServerDescription createServerDescription(final ServerAddress serverAddress, final BsonDocument isMasterResult, final ServerVersion serverVersion, final long roundTripTime) { return ServerDescription.builder() .state(CONNECTED) .version(serverVersion) .address(serverAddress) .type(getServerType(isMasterResult)) .canonicalAddress(isMasterResult.containsKey("me") ? isMasterResult.getString("me").getValue() : null) .hosts(listToSet(isMasterResult.getArray("hosts", new BsonArray()))) .passives(listToSet(isMasterResult.getArray("passives", new BsonArray()))) .arbiters(listToSet(isMasterResult.getArray("arbiters", new BsonArray()))) .primary(getString(isMasterResult, "primary")) .maxDocumentSize(getMaxBsonObjectSize(isMasterResult)) .tagSet(getTagSetFromDocument(isMasterResult.getDocument("tags", new BsonDocument()))) .setName(getString(isMasterResult, "setName")) .minWireVersion(getMinWireVersion(isMasterResult)) .maxWireVersion(getMaxWireVersion(isMasterResult)) .electionId(getElectionId(isMasterResult)) .setVersion(getSetVersion(isMasterResult)) .lastWriteDate(getLastWriteDate(isMasterResult)) .roundTripTime(roundTripTime, NANOSECONDS) .logicalSessionTimeoutMinutes(getLogicalSessionTimeoutMinutes(isMasterResult)) .ok(CommandHelper.isCommandOk(isMasterResult)).build(); }
private Document callStoredProcedure(StringBuilder commandLine) { try { Document result = provider.getDatabase().runCommand( new Document( "$eval", commandLine.toString() ) ); return result; } catch (MongoCommandException mce) { BsonDocument response = mce.getResponse(); throw log.unableToExecuteCommand( commandLine.toString(), response.getString( "errmsg" ).getValue(), response.getString( "codeName" ).getValue(), mce ); } }
/** * * @return */ public String getHref() { return doc.getDocument(getRef()).getString("href").getValue(); }