Example usage for com.mongodb DBCursor explain

List of usage examples for com.mongodb DBCursor explain

Introduction

In this page you can find the example usage for com.mongodb DBCursor explain.

Prototype

@Deprecated
public DBObject explain() 

Source Link

Document

Returns an object containing basic information about the execution of the query that created this cursor.

Usage

From source file:org.basex.modules.MongoDB.java

License:BSD License

/**
 * MongoDB find with all parameters./*from   w  w w  .ja v a2  s . c o m*/
 * @param handler Database handler
 * @param col collection
 * @param query Query parameters
 * @param opt options in Map like: {"limit":2}
 * @param field  Projection
 * @return Item
 * @throws QueryException
 */
public Item find(final Str handler, final Item col, final Item query, final Item opt, final Item projection)
        throws QueryException {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        DBObject p = null;
        if (opt != null && opt instanceof Str) {
            p = getDbObjectFromStr(opt);
        } else if (projection != null && projection instanceof Str) {
            p = getDbObjectFromStr(projection);
        }
        final DBObject q = query != null ? getDbObjectFromStr(query) : null;
        final DBCollection coll = db.getCollection(itemToString(col));
        final DBCursor cursor = coll.find(q, p);
        Map options = null;
        options = (opt != null && opt instanceof Map) ? (Map) opt
                : (projection != null && projection instanceof Map) ? (Map) projection : null;
        if (options != null) {
            Value keys = options.keys();
            for (final Item key : keys) {
                if (!(key instanceof Str))
                    throw MongoDBErrors.generalExceptionError("String expected " + key.toJava());
                final String k = ((Str) key).toJava();
                final Value v = options.get(key, null);
                if (v instanceof Str || v.type().instanceOf(SeqType.ITR)) {
                    if (k.equals(LIMIT)) {
                        if (v.type().instanceOf(SeqType.ITR_OM)) {
                            long l = ((Item) v).itr(null);
                            cursor.limit((int) l);
                        } else {
                            throw MongoDBErrors
                                    .generalExceptionError("Number Expected for key '" + key.toJava() + "'");
                        }
                    } else if (k.equals(SKIP)) {
                        //cursor.skip(Token.toInt(v));
                    } else if (k.equals(SORT)) {
                        BasicDBObject sort = new BasicDBObject(k, v);
                        sort.append("name", "-1");
                        cursor.sort((DBObject) sort);
                    } else if (k.equals(COUNT)) {
                        int count = cursor.count();
                        BasicDBObject res = new BasicDBObject();
                        res.append("count", count);
                        return objectToItem(handler, res);
                    } else if (k.equals(EXPLAIN)) {
                        DBObject result = cursor.explain();
                        return objectToItem(handler, result);
                    }
                } else if (v instanceof Map) {
                } else {
                    throw MongoDBErrors.generalExceptionError("Invalid value 2...");
                }
            }
        }
        return cursorToItem(handler, cursor);
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(e.getMessage());
    } finally {
        db.requestDone();
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * MongoDB find with all parameters.//from   w  w w.j a  va2 s.c  o  m
 * @param handler database handler Database handler
 * @param col collection collection
 * @param query Query parameters
 * @param opt options in Map like: {"limit":2}
 * @param projection projection (selection field)
 * @return Item
 * @throws QueryException query exception
 */
public Item find(final Str handler, final Item col, final Item query, final Item opt, final Item projection)
        throws QueryException {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        DBObject p = null;
        if (opt != null && opt instanceof Str) {
            p = getDbObjectFromItem(opt);
        } else if (projection != null && projection instanceof Str) {
            p = getDbObjectFromItem(projection);
        }
        final DBObject q = query != null ? getDbObjectFromItem(query) : null;
        final DBCollection coll = db.getCollection(itemToString(col));
        final DBCursor cursor = coll.find(q, p);
        Map options = null;
        options = (opt != null && opt instanceof Map) ? (Map) opt
                : (projection != null && projection instanceof Map) ? (Map) projection : null;
        if (options != null) {
            Value keys = options.keys();
            for (final Item key : keys) {
                if (!(key instanceof Str))
                    throw MongoDBErrors.generalExceptionError("String expected " + key.toJava());
                final String k = ((Str) key).toJava();
                final Value v = options.get(key, null);
                if (v instanceof Str || v.seqType().instanceOf(SeqType.ITR)) {
                    if (k.equals(LIMIT)) {
                        if (v.seqType().instanceOf(SeqType.ITR_OM)) {
                            long l = ((Item) v).itr(null);
                            cursor.limit((int) l);
                        } else {
                            throw MongoDBErrors
                                    .generalExceptionError("Number Expected for key '" + key.toJava() + "'");
                        }
                    } else if (k.equals(SKIP)) {
                        //cursor.skip(Token.toInt(v));
                    } else if (k.equals(SORT)) {
                        BasicDBObject sort = new BasicDBObject(k, v);
                        sort.append("name", "-1");
                        cursor.sort(sort);
                    } else if (k.equals(COUNT)) {
                        int count = cursor.count();
                        BasicDBObject res = new BasicDBObject();
                        res.append("count", count);
                        return objectToItem(handler, res);
                    } else if (k.equals(EXPLAIN)) {
                        DBObject result = cursor.explain();
                        return objectToItem(handler, result);
                    }
                } else if (v instanceof Map) {
                } else {
                    throw MongoDBErrors.generalExceptionError("Invalid value 2...");
                }
            }
        }
        return cursorToItem(handler, cursor);
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(e.getMessage());
    } finally {
        db.requestDone();
    }
}

From source file:org.iternine.jeppetto.dao.mongodb.QueryLoggingCommand.java

License:Apache License

@Override
public DBCursor cursor(DBCollection dbCollection) {
    logger.debug("Executing {} for {} cursor", delegate, dbCollection.getFullName());

    DBCursor dbCursor = delegate.cursor(dbCollection);

    if (logger.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        DBObject plan = dbCursor.explain();

        sb.append("MongoDB query plan ").append(plan).append('\n');
        sb.append("\tcursor = \"").append(plan.get("cursor")).append("\"\n");
        sb.append("\tnscanned = \"").append(plan.get("nscanned")).append("\"\n");
        sb.append("\tn = \"").append(plan.get("n")).append("\"\n");
        sb.append("\tmillis = \"").append(plan.get("millis")).append("\"\n");

        logger.debug(sb.toString());/*from  w  w  w. j  a  v  a  2s.c  om*/
    }

    return dbCursor;
}