Search in sources :

Example 21 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class InternalStreamConnectionInitializer method initializeConnectionDescription.

private ConnectionDescription initializeConnectionDescription(final InternalConnection internalConnection) {
    BsonDocument isMasterResult = executeCommand("admin", createIsMasterCommand(), internalConnection);
    BsonDocument buildInfoResult = executeCommand("admin", new BsonDocument("buildinfo", new BsonInt32(1)), internalConnection);
    return createConnectionDescription(internalConnection.getDescription().getConnectionId(), isMasterResult, buildInfoResult);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument)

Example 22 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class InternalStreamConnectionInitializer method initializeConnectionDescriptionAsync.

private void initializeConnectionDescriptionAsync(final InternalConnection internalConnection, final SingleResultCallback<ConnectionDescription> callback) {
    executeCommandAsync("admin", createIsMasterCommand(), internalConnection, new SingleResultCallback<BsonDocument>() {

        @Override
        public void onResult(final BsonDocument isMasterResult, final Throwable t) {
            if (t != null) {
                callback.onResult(null, t);
            } else {
                executeCommandAsync("admin", new BsonDocument("buildinfo", new BsonInt32(1)), internalConnection, new SingleResultCallback<BsonDocument>() {

                    @Override
                    public void onResult(final BsonDocument buildInfoResult, final Throwable t) {
                        if (t != null) {
                            callback.onResult(null, t);
                        } else {
                            ConnectionId connectionId = internalConnection.getDescription().getConnectionId();
                            callback.onResult(createConnectionDescription(connectionId, isMasterResult, buildInfoResult), null);
                        }
                    }
                });
            }
        }
    });
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) SingleResultCallback(com.mongodb.async.SingleResultCallback)

Example 23 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class ListCollectionsOperation method projectFromFullNamespaceToCollectionName.

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;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) BsonDocumentReader(org.bson.BsonDocumentReader) BsonString(org.bson.BsonString)

Example 24 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class ListCollectionsOperation method asQueryDocument.

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;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonRegularExpression(org.bson.BsonRegularExpression)

Example 25 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class MapReduceWithInlineResultsOperation method getCommand.

private BsonDocument getCommand() {
    BsonDocument commandDocument = new BsonDocument("mapreduce", new BsonString(namespace.getCollectionName())).append("map", getMapFunction()).append("reduce", getReduceFunction()).append("out", new BsonDocument("inline", new BsonInt32(1))).append("query", asValueOrNull(getFilter())).append("sort", asValueOrNull(getSort())).append("finalize", asValueOrNull(getFinalizeFunction())).append("scope", asValueOrNull(getScope())).append("verbose", BsonBoolean.valueOf(isVerbose()));
    putIfNotZero(commandDocument, "limit", getLimit());
    putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS));
    putIfTrue(commandDocument, "jsMode", isJsMode());
    if (!readConcern.isServerDefault()) {
        commandDocument.put("readConcern", readConcern.asDocument());
    }
    if (collation != null) {
        commandDocument.put("collation", collation.asDocument());
    }
    return commandDocument;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Aggregations

BsonDocument (org.bson.BsonDocument)169 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)36 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 Document (org.bson.Document)7 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 ObjectId (org.bson.types.ObjectId)5