private MongoNamespace createNamespace() { return new MongoNamespace(databaseName, "$cmd.listCollections"); }
private MongoNamespace getNamespace() { return new MongoNamespace(databaseName, "system.namespaces"); }
@Override public <TDocument> MongoCollection<TDocument> getCollection(final String collectionName, final Class<TDocument> documentClass) { return new MongoCollectionImpl<TDocument>(new MongoNamespace(name, collectionName), documentClass, codecRegistry, readPreference, writeConcern, retryWrites, readConcern, executor); }
AggregateIterableImpl(@Nullable final ClientSession clientSession, final String databaseName, final Class<TDocument> documentClass, final Class<TResult> resultClass, final CodecRegistry codecRegistry, final ReadPreference readPreference, final ReadConcern readConcern, final WriteConcern writeConcern, final OperationExecutor executor, final List<? extends Bson> pipeline, final AggregationLevel aggregationLevel) { this(clientSession, new MongoNamespace(databaseName, "ignored"), documentClass, resultClass, codecRegistry, readPreference, readConcern, writeConcern, executor, pipeline, aggregationLevel); }
ChangeStreamIterableImpl(@Nullable final ClientSession clientSession, final String databaseName, final CodecRegistry codecRegistry, final ReadPreference readPreference, final ReadConcern readConcern, final OperationExecutor executor, final List<? extends Bson> pipeline, final Class<TResult> resultClass, final ChangeStreamLevel changeStreamLevel) { this(clientSession, new MongoNamespace(databaseName, "ignored"), codecRegistry, readPreference, readConcern, executor, pipeline, resultClass, changeStreamLevel); }
private MongoNamespace getIndexNamespace() { return new MongoNamespace(namespace.getDatabaseName(), "system.indexes"); }
MongoNamespace createNamespaceFromOptions(RequestOptions options) { String collectionName = StringUtils.hasText(options.getCollectionName()) ? options.getCollectionName() : "unknown"; String databaseName = StringUtils.hasText(options.getDatabaseName()) ? options.getDatabaseName() : "unknown"; return new MongoNamespace(databaseName, collectionName); }
private BsonDocument queryUnlock(final Connection connection) { return connection.query(new MongoNamespace("admin", "$cmd.sys.unlock"), new BsonDocument(), null, 0, 1, 0, false, false, false, false, false, false, new BsonDocumentCodec()).getResults().get(0); }
MongoNamespace getNamespace() { return new MongoNamespace(getDB().getName(), getName()); }
private static CommandMessage getCommandMessage(final String database, final BsonDocument command, final InternalConnection internalConnection) { return new CommandMessage(new MongoNamespace(database, COMMAND_COLLECTION_NAME), command, new NoOpFieldNameValidator(), primary(), MessageSettings .builder() // Note: server version will be 0.0 at this point when called from InternalConnectionInitializer, // which means OP_MSG will not be used .maxWireVersion(internalConnection.getDescription().getMaxWireVersion()) .build() ); }
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); }
private List<BsonElement> getExtraElements(final SessionContext sessionContext) { List<BsonElement> extraElements = new ArrayList<BsonElement>(); extraElements.add(new BsonElement("$db", new BsonString(new MongoNamespace(getCollectionName()).getDatabaseName()))); if (sessionContext.getClusterTime() != null) { extraElements.add(new BsonElement("$clusterTime", sessionContext.getClusterTime())); } if (sessionContext.hasSession() && responseExpected) { extraElements.add(new BsonElement("lsid", sessionContext.getSessionId())); } boolean firstMessageInTransaction = sessionContext.notifyMessageSent(); if (sessionContext.hasActiveTransaction()) { checkServerVersionForTransactionSupport(); extraElements.add(new BsonElement("txnNumber", new BsonInt64(sessionContext.getTransactionNumber()))); if (firstMessageInTransaction) { extraElements.add(new BsonElement("startTransaction", BsonBoolean.TRUE)); addReadConcernDocument(extraElements, sessionContext); } extraElements.add(new BsonElement("autocommit", BsonBoolean.FALSE)); } if (readPreference != null) { if (!readPreference.equals(primary())) { extraElements.add(new BsonElement("$readPreference", readPreference.toDocument())); } else if (isDirectConnectionToNonShardRouter()) { extraElements.add(new BsonElement("$readPreference", primaryPreferred().toDocument())); } } return extraElements; }
/** * Change the name of an existing collection. * * @param newName specifies the new name of the collection * @param dropTarget If {@code true}, mongod will drop the collection with the target name if it exists * @return the collection with new name * @throws MongoException if target is the name of an existing collection and {@code dropTarget=false}. * @mongodb.driver.manual reference/command/renameCollection/ Rename Collection */ public DBCollection rename(final String newName, final boolean dropTarget) { try { executor.execute(new RenameCollectionOperation(getNamespace(), new MongoNamespace(getNamespace().getDatabaseName(), newName), getWriteConcern()) .dropTarget(dropTarget), getReadConcern()); return getDB().getCollection(newName); } catch (MongoWriteConcernException e) { throw createWriteConcernException(e); } }
CommandProtocolImpl(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final boolean responseExpected, final SplittablePayload payload, final FieldNameValidator payloadFieldNameValidator, final ClusterConnectionMode clusterConnectionMode) { notNull("database", database); this.namespace = new MongoNamespace(notNull("database", database), MongoNamespace.COMMAND_COLLECTION_NAME); this.command = notNull("command", command); this.commandFieldNameValidator = notNull("commandFieldNameValidator", commandFieldNameValidator); this.readPreference = readPreference; this.commandResultDecoder = notNull("commandResultDecoder", commandResultDecoder); this.responseExpected = responseExpected; this.payload = payload; this.payloadFieldNameValidator = payloadFieldNameValidator; this.clusterConnectionMode = notNull("clusterConnectionMode", clusterConnectionMode); isTrueArgument("payloadFieldNameValidator cannot be null if there is a payload.", payload == null || payloadFieldNameValidator != null); }
@Override public BsonDocument call(final Connection connection) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { return executeWrappedCommandProtocol(binding, "admin", new BsonDocument("currentOp", new BsonInt32(1)), connection); } else { return connection.query(new MongoNamespace("admin", "$cmd.sys.inprog"), new BsonDocument(), null, 0, 1, 0, binding.getReadPreference().isSlaveOk(), false, false, false, false, false, new BsonDocumentCodec()) .getResults().get(0); } } });
/** * 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()); }
@Override public ReadOperation<BatchCursor<TResult>> asReadOperation() { if (inline) { ReadOperation<MapReduceBatchCursor<TResult>> operation = operations.mapReduce(mapFunction, reduceFunction, finalizeFunction, resultClass, filter, limit, maxTimeMS, jsMode, scope, sort, verbose, collation); return new WrappedMapReduceReadOperation<TResult>(operation); } else { getExecutor().execute(createMapReduceToCollectionOperation(), getReadConcern(), getClientSession()); String dbName = databaseName != null ? databaseName : namespace.getDatabaseName(); FindOptions findOptions = new FindOptions().collation(collation); Integer batchSize = getBatchSize(); if (batchSize != null) { findOptions.batchSize(batchSize); } return operations.find(new MongoNamespace(dbName, collectionName), new BsonDocument(), resultClass, findOptions); } }
@Override @SuppressWarnings("deprecation") public ReadOperation<BatchCursor<TResult>> asReadOperation() { BsonValue outCollection = getOutCollection(); if (outCollection != null) { getExecutor().execute(operations.aggregateToCollection(pipeline, maxTimeMS, allowDiskUse, bypassDocumentValidation, collation, hint, comment, aggregationLevel), getReadConcern(), getClientSession()); FindOptions findOptions = new FindOptions().collation(collation); Integer batchSize = getBatchSize(); if (batchSize != null) { findOptions.batchSize(batchSize); } return operations.find(new MongoNamespace(namespace.getDatabaseName(), outCollection.asString().getValue()), new BsonDocument(), resultClass, findOptions); } else { return operations.aggregate(pipeline, resultClass, maxTimeMS, maxAwaitTimeMS, getBatchSize(), collation, hint, comment, allowDiskUse, useCursor, aggregationLevel); } }
MongoNamespace createNamespaceFromOptions(RequestOptions options) { String collectionName = StringUtils.hasText(options.getCollectionName()) ? options.getCollectionName() : "unknown"; String databaseName = StringUtils.hasText(options.getDatabaseName()) ? options.getDatabaseName() : "unknown"; return new MongoNamespace(databaseName, collectionName); }
@Test public void testRenameCollection() throws Exception { collection.insertOne(json("_id: 1")); collection.insertOne(json("_id: 2")); collection.insertOne(json("_id: 3")); collection.renameCollection(new MongoNamespace(collection.getNamespace().getDatabaseName(), "other-collection-name")); Collection<String> collectionNames = toArray(db.listCollectionNames()); assertThat(collectionNames).containsExactly("other-collection-name"); assertThat(getCollection("other-collection-name").countDocuments()).isEqualTo(3); }