Example usage for com.mongodb DBCollection getName

List of usage examples for com.mongodb DBCollection getName

Introduction

In this page you can find the example usage for com.mongodb DBCollection getName.

Prototype

public String getName() 

Source Link

Document

Get the name of a collection.

Usage

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void findAndModify(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.famQuery)).getDBObject();
    final DBObject fields = ((DocBuilderField) getBoundUnit(Item.famFields)).getDBObject();
    final DBObject sort = ((DocBuilderField) getBoundUnit(Item.famSort)).getDBObject();
    final BasicDBObject update = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.famUpdate)).getDBObject();
    final boolean remove = getBooleanFieldValue(Item.famRemove);
    final boolean returnNew = getBooleanFieldValue(Item.famReturnNew);
    final boolean upsert = getBooleanFieldValue(Item.famUpsert);

    BasicDBObject cmd = new BasicDBObject("findandmodify", col.getName());
    if (query != null && !query.keySet().isEmpty()) {
        cmd.append("query", query);
    }//from  w ww.j  a v a 2s  .  com
    if (fields != null && !fields.keySet().isEmpty()) {
        cmd.append("fields", fields);
    }
    if (sort != null && !sort.keySet().isEmpty()) {
        cmd.append("sort", sort);
    }

    if (remove) {
        cmd.append("remove", remove);
    } else {
        if (update != null && !update.keySet().isEmpty()) {
            // if 1st key doesn't start with $, then object will be inserted as is, need to check it
            String key = update.keySet().iterator().next();
            if (key.charAt(0) != '$') {
                MongoUtils.checkObject(update, false, false);
            }
            cmd.append("update", (DBObject) update.copy());
        }
        if (returnNew) {
            cmd.append("new", returnNew);
        }
        if (upsert) {
            cmd.append("upsert", upsert);
        }
    }

    new DbJobCmd(col.getDB(), cmd, null, button).addJob();
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void fixCollection(ButtonBase button) {
    final MongoClient m = getCollectionNode().getDbNode().getMongoNode().getMongoClient();
    ArrayList<MongoNode> mongoNodes = UMongo.instance.getMongos();
    String[] mongonames = new String[mongoNodes.size() - 1];
    MongoClient[] mongos = new MongoClient[mongonames.length];
    int i = 0;/*from  ww w .  j a v a 2 s  . c  o m*/
    for (MongoNode node : mongoNodes) {
        MongoClient m2 = node.getMongoClient();
        if (m == m2) {
            continue;
        }
        mongonames[i] = m2.toString();
        mongos[i] = m2;
        ++i;
    }
    ComboBox src = (ComboBox) getBoundUnit(Item.fcSrcMongo);
    src.items = mongonames;
    src.structureComponent();
    FormDialog dialog = (FormDialog) getBoundUnit(Item.fcDialog);
    if (!dialog.show()) {
        return;
    }

    final DBCollection dstCol = getCollectionNode().getCollection();
    final MongoClient srcMongo = mongos[src.getIntValue()];
    final boolean upsert = getBooleanFieldValue(Item.fcUpsert);

    final String dbname = dstCol.getDB().getName();
    final String colname = dstCol.getName();
    final DBCollection srcCol = srcMongo.getDB(dbname).getCollection(colname);
    String txt = "About to copy from ";
    txt += srcMongo.getConnectPoint() + "(" + srcCol.count() + ")";
    txt += " to ";
    txt += m.getConnectPoint() + "(" + dstCol.count() + ")";
    if (!new ConfirmDialog(null, "Confirm Fix Collection", null, txt).show()) {
        return;
    }

    new DbJob() {
        @Override
        public Object doRun() {
            DBCursor cur = srcCol.find();
            int count = 0;
            int dup = 0;
            while (cur.hasNext()) {
                DBObject obj = cur.next();
                if (upsert) {
                    BasicDBObject id = new BasicDBObject("_id", obj.get("_id"));
                    dstCol.update(id, obj, true, false);
                } else {
                    try {
                        dstCol.insert(obj);
                    } catch (DuplicateKey e) {
                        // dup keys are expected here
                        ++dup;
                    }
                }
                ++count;
            }
            DBObject res = new BasicDBObject("count", count);
            res.put("dups", dup);
            return res;
        }

        @Override
        public String getNS() {
            return "*";
        }

        @Override
        public String getShortName() {
            return "Fix Collection";
        }
    }.addJob();
}

From source file:com.edgytech.umongo.DbJobCmd.java

License:Apache License

public DbJobCmd(DBCollection col, String cmdStr) {
    this(col.getDB(), new BasicDBObject(cmdStr, col.getName()), null, null);
}

From source file:com.edgytech.umongo.DbPanel.java

License:Apache License

public void listFiles(ButtonBase button) {
    final DB db = getDbNode().getDb();
    final DBCollection col = db.getCollection("fs.files");

    new DbJob() {

        @Override/*  w  w w .j  ava 2 s .c  o  m*/
        public Object doRun() throws IOException {
            return col.find();
        }

        @Override
        public String getNS() {
            return col.getName();
        }

        @Override
        public String getShortName() {
            return "List Files";
        }
    }.addJob();
}

From source file:com.edgytech.umongo.DbPanel.java

License:Apache License

public void editUser(ButtonBase button) {
    final DB db = getDbNode().getDb();
    final DBCollection col = db.getCollection("system.users");

    final String user = getComponentStringFieldValue(Item.userList);
    if (user == null) {
        return;//ww  w . j  ava2s .c  om
    }

    final BasicDBObject userObj = (BasicDBObject) col.findOne(new BasicDBObject("user", user));
    UserDialog ud = (UserDialog) getBoundUnit(Item.userDialog);
    ud.resetForEdit(userObj);
    if (!ud.show()) {
        return;
    }

    final BasicDBObject newUser = ud.getUser(userObj);

    new DbJob() {

        @Override
        public Object doRun() throws IOException {
            return col.save(newUser);
        }

        @Override
        public String getNS() {
            return col.getName();
        }

        @Override
        public String getShortName() {
            return "Edit User";
        }

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            refreshUserList();
        }
    }.addJob();

}

From source file:com.hangum.tadpole.mongodb.core.test.ConAndAuthentication.java

License:Open Source License

/**
 * collection info/*from w ww .  jav  a2  s .  c  o m*/
 * 
 * @param db
 * @param collection
 * @return
 */
public String getCollectionInfo(DB db, String collection) {
    DBCollection coll = db.getCollection(collection);

    System.out.println("#################################################");
    System.out.println("[collection name]" + coll.getName());
    System.out.println("[count]" + coll.getCount());

    // index list
    List<DBObject> listIndex = coll.getIndexInfo();
    List<CollectionFieldDAO> columnInfo = MongoDBTableColumn.tableColumnInfo(listIndex,
            db.getCollection(collection).findOne());
    for (CollectionFieldDAO collectionFieldDAO : columnInfo) {
        System.out.println("[field]" + collectionFieldDAO.getField());

        if (!collectionFieldDAO.getChildren().isEmpty()) {
            List<CollectionFieldDAO> childColl = collectionFieldDAO.getChildren();
            for (CollectionFieldDAO collectionFieldDAO2 : childColl) {
                System.out.println("\t [child field]" + collectionFieldDAO2.getField());

                if (!collectionFieldDAO2.getChildren().isEmpty()) {
                    List<CollectionFieldDAO> childColl2 = collectionFieldDAO2.getChildren();
                    for (CollectionFieldDAO collectionFieldDAO3 : childColl2) {
                        System.out.println("\t\t [child field]" + collectionFieldDAO3.getField());
                    }

                }
            }

        }
    }

    //      //
    //      StringBuffer sbJson = new StringBuffer();
    //
    //      DBCursor cursor = coll.find();
    //      while (cursor.hasNext()) {
    //         DBObject dbObj = cursor.next();
    //         String jsonData = dbObj.toString();
    //         System.out.println("[data] \t" + jsonData);
    //
    //         sbJson.append(jsonData);
    //      }
    //
    //      System.out
    //            .println("#####[fully text]#########################################################");

    // System.out.println("\t\t ******[detail data][start]*******************************************");
    // ObjectMapper om = new ObjectMapper();
    // try {
    // Map<String, Object> mapObj = om.readValue(sbJson.toString(),
    // new TypeReference<Map<String, Object>>() {});
    // System.out.println("[json to object]" + mapObj);
    //
    // Set<String> keys = mapObj.keySet();
    // for (String key : keys) System.out.print(key + "\t:\t");
    // System.out.println();
    //
    //
    // } catch (JsonParseException e) {
    // e.printStackTrace();
    // } catch (JsonMappingException e) {
    // e.printStackTrace();
    // } catch (IOException e) {
    // e.printStackTrace();
    // }

    System.out.println("\t\t ******[detail data][end]*******************************************");

    return "";
}

From source file:com.redhat.mongotx.TransactionLogger.java

License:Open Source License

/**
 * Returns a transactional data collection for the given
 * collection. It is a collection obtained by appending the name
 * ".tx". // ww  w.ja  v  a  2s  . c om
 */
private DBCollection getTxDataCollection(DBCollection collection) {
    return db.getCollection(collection.getName() + ".tx");
}

From source file:com.stratio.connector.mongodb.core.engine.metadata.DiscoverMetadataUtils.java

License:Apache License

/**
 * Discover the existing indexes stored in the collection.
 *
 * @param collection//from  w  ww.  j  a  v  a 2 s  . c  o m
 *            the collection
 * @return the list of indexMetadata.
 */
public static List<IndexMetadata> discoverIndexes(DBCollection collection) {
    // TODO add TextIndex, Geospatial,etc...
    // TODO supported only simple, compound and hashed index
    // TODO remove _id?
    // TODO return options?? e.g sparse, unique??
    // TODO custom (asc and desc)
    List<DBObject> indexInfo = collection.getIndexInfo();
    String db = collection.getDB().getName();
    String collName = collection.getName();

    List<IndexMetadata> indexMetadataList = new ArrayList<>(indexInfo.size());
    for (DBObject dbObject : indexInfo) {
        BasicDBObject key = (BasicDBObject) dbObject.get("key");

        IndexMetadataBuilder indexMetadataBuilder = new IndexMetadataBuilder(db, collName,
                (String) dbObject.get("name"), getIndexType(key));
        for (String field : key.keySet()) {
            indexMetadataBuilder.addColumn(field, null);
        }
        indexMetadataList.add(indexMetadataBuilder.build());
    }
    return indexMetadataList;
}

From source file:com.stratio.deep.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Calculate splits.//from   w  w w  . ja  v a 2  s  . c o  m
 *
 * @param collection the collection
 * @return the deep partition [ ]
 */
private DeepPartition[] calculateSplits(DBCollection collection) {

    BasicDBList splitData = getSplitData(collection);
    List<ServerAddress> serverAddressList = collection.getDB().getMongo().getServerAddressList();

    if (splitData == null) {
        Pair<BasicDBList, List<ServerAddress>> pair = getSplitDataCollectionShardEnviroment(
                getShards(collection), collection.getDB().getName(), collection.getName());
        splitData = pair.left;
        serverAddressList = pair.right;
    }

    Object lastKey = null; // Lower boundary of the first min split

    List<String> stringHosts = new ArrayList<>();

    for (ServerAddress serverAddress : serverAddressList) {
        stringHosts.add(serverAddress.toString());
    }
    int i = 0;

    MongoPartition[] partitions = new MongoPartition[splitData.size() + 1];

    for (Object aSplitData : splitData) {

        BasicDBObject currentKey = (BasicDBObject) aSplitData;

        Object currentO = currentKey.get(MONGO_DEFAULT_ID);

        partitions[i] = new MongoPartition(mongoDeepJobConfig.getRddId(), i,
                new DeepTokenRange(lastKey, currentO, stringHosts), MONGO_DEFAULT_ID);

        lastKey = currentO;
        i++;
    }
    QueryBuilder queryBuilder = QueryBuilder.start(MONGO_DEFAULT_ID);
    queryBuilder.greaterThanEquals(lastKey);
    partitions[i] = new MongoPartition(0, i, new DeepTokenRange(lastKey, null, stringHosts), MONGO_DEFAULT_ID);
    return partitions;
}

From source file:com.sube.daos.mongodb.generators.GenericDBRefGenerator.java

License:Apache License

@Override
public DBRef generateDBRef(T toGenerate) {
    DBCollection dbCollection = db.getCollection(collection);
    // { $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }
    BSONObject dbRefObj = new BasicDBObject("$ref", dbCollection.getName())
            .append("$id", toGenerate.getMongoId()).append("$db", db.getName());
    DBRef dbRef = new DBRef(db, dbRefObj);
    return dbRef;
}