Java Code Examples for com.mongodb.CommandResult#get()

The following examples show how to use com.mongodb.CommandResult#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoDBTestHelper.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
public static boolean isConfigServer(AbstractMongoDBServer entity) {
    LOG.info("Checking if {} is a config server", entity);
    MongoClient mongoClient = clientForServer(entity);
    try {
        DB db = mongoClient.getDB(ADMIN_DB);
        CommandResult commandResult = db.command("getCmdLineOpts");
        Map<?, ?> parsedArgs = (Map<?, ?>)commandResult.get("parsed");
        if (parsedArgs == null) return false;
        Boolean configServer = (Boolean)parsedArgs.get("configsvr");
        if (configServer != null) {
            // v2.5 format
            return Boolean.TRUE.equals(configServer);
        } else {
            // v2.6 format
            String role = (String) ((Map)parsedArgs.get("sharding")).get("clusterRole");
            return "configsvr".equals(role);
        }
    } finally {
        mongoClient.close();
    }
}
 
Example 2
Source File: MongoCommander.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
/**
 * get list of  the shards
 * @param dbConn
 * @return
 */
private static List<String> getShards(DB dbConn) {
    List<String> shards = new ArrayList<String>();

    DBObject listShardsCmd = new BasicDBObject("listShards", 1);
    CommandResult res = dbConn.command(listShardsCmd);
    if (!res.ok()) {
        LOG.error("Error getting shards for {}: {}", dbConn, res.getErrorMessage());
    }

    BasicDBList listShards = (BasicDBList) res.get("shards");

    //Only get shards for sharding mongo
    if (listShards != null) {
        ListIterator<Object> iter = listShards.listIterator();

        while (iter.hasNext()) {
            BasicDBObject shard = (BasicDBObject) iter.next();
            shards.add(shard.getString(ID));
        }
    }
    return shards;
}
 
Example 3
Source File: MongoNativeExtractor.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Gets split data.
 *
 * @param collection the collection
 * @return the split data
 */
private BasicDBList getSplitData(DBCollection collection) {

    final DBObject cmd = BasicDBObjectBuilder.start("splitVector", collection.getFullName())
            .add("keyPattern", new BasicDBObject(MONGO_DEFAULT_ID, 1))
            .add("force", false)
            .add("maxChunkSize", splitSize)
            .get();

    CommandResult splitVectorResult = collection.getDB().getSisterDB("admin").command(cmd);
    return (BasicDBList) splitVectorResult.get(SPLIT_KEYS);

}
 
Example 4
Source File: SubDocAccessor.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public boolean doUpdate(Query query, Update update) {
    DBObject queryDBObject = toSubDocQuery(query, true);

    DBObject elementMatch = new BasicDBObject("$elemMatch", query.getQueryObject());
    queryDBObject.put(subField, elementMatch);

    DBObject patchUpdate = toSubDocUpdate(update);
    String updateCommand = "{findAndModify:\"" + collection + "\",query:" + queryDBObject.toString()
            + ",update:" + patchUpdate.toString() + "}";
    LOG.debug("the update date mongo command is: {}", updateCommand);
    TenantContext.setIsSystemCall(false);
    CommandResult result = template.executeCommand(updateCommand);
    return result.get("value") != null;

}
 
Example 5
Source File: SubDocAccessor.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
        private List<Entity> findSubDocs(DBObject parentQuery, DBObject limitQuery) {
            StringBuilder limitQuerySB = new StringBuilder();
            if (limitQuery != null && limitQuery.keySet().size() > 0) {
                if (limitQuery.get("$sort") != null) {
                    limitQuerySB.append(",{$sort:" + limitQuery.get("$sort").toString() + "}");
                }
                if (limitQuery.get("$skip") != null) {
                    limitQuerySB.append(",{$skip:" + limitQuery.get("$skip") + "}");
                }
                if (limitQuery.get("$limit") != null) {
                    limitQuerySB.append(",{$limit:" + limitQuery.get("$limit") + "}");
                }
            }
            simplifyParentQuery(parentQuery);

            DBObject idQuery = buildIdQuery(parentQuery);
//            String queryCommand = buildAggregateQuery(idQuery != null ? idQuery.toString() : parentQuery.toString(),
//                    parentQuery.toString(), limitQuerySB.toString());
            String queryCommand;
            if (idQuery == null) {
                queryCommand = buildAggregateQuery(parentQuery.toString(), null, limitQuerySB.toString());
            } else {
                queryCommand = buildAggregateQuery(idQuery.toString(), parentQuery.toString(), limitQuerySB.toString());
            }
            TenantContext.setIsSystemCall(false);
            CommandResult result = template.executeCommand(queryCommand);
            List<DBObject> subDocs = (List<DBObject>) result.get("result");
            List<Entity> entities = new ArrayList<Entity>();

            if (subDocs != null && subDocs.size() > 0) {
                for (DBObject dbObject : subDocs) {
                    entities.add(convertDBObjectToSubDoc(((DBObject) dbObject.get(subField))));
                }
            }
            return entities;
        }