Example usage for com.mongodb DBCollection remove

List of usage examples for com.mongodb DBCollection remove

Introduction

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

Prototype

public WriteResult remove(final DBObject query) 

Source Link

Document

Remove documents from a collection.

Usage

From source file:com.intuit.utils.PopulateTweets.java

public static void main(String[] args) {
    Date now = new Date();
    System.out.println("Current date is: " + now.toString());

    MongoClient mongo = new MongoClient("localhost", 27017);
    DB db = mongo.getDB("tweetsdb");
    DBCollection collection = db.getCollection("tweetscollection");
    WriteResult result = collection.remove(new BasicDBObject());

    String[] users = { "user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9",
            "user10" };
    // I am not introducing enough randomness in terms of the insertion of 
    // tweets for users at a random time orders, due to lack of time.
    for (String user : users) {
        int tweetIndex = 0;
        for (int i = 1; i <= 10; i++) {
            BasicDBObject document = new BasicDBObject();
            // This is a way to maintain uniqueness of the tweetid value across the system
            // Ideally, this should be the "_id" value, but due to lack of time, I am skipping
            // that part.  That would help to partition the tweets across multiple shards in a 
            // large scale system.
            String tweetId = user + "|tweet" + tweetIndex;
            document.put("tweetId", tweetId);
            document.put("user", user);
            document.put("text", "tweet number" + tweetIndex);
            document.put("tweetedOn", new Date().toString());
            System.out.println("tweet number: " + tweetIndex + "   " + document.toString());
            collection.insert(document);
            tweetIndex++;/* w  w  w.j  ava2  s.c o  m*/
            try {
                // Just introducing some delay between tweets to make the testing a bit easy
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                Logger.getLogger(PopulateTweets.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    BasicDBObject indexObj = new BasicDBObject();
    indexObj.put("user", 1);
    indexObj.put("tweetedOn", -1);
    collection.createIndex(indexObj);

    BasicDBObject tweetIdObj = new BasicDBObject();
    tweetIdObj.put("tweetId", 1);
    collection.createIndex(tweetIdObj);
}

From source file:com.intuit.utils.PopulateUsers.java

public static void main(String[] args) {
    Date now = new Date();
    System.out.println("Current date is: " + now.toString());

    MongoClient mongo = new MongoClient("localhost", 27017);
    DB db = mongo.getDB("tweetsdb");
    DBCollection collection = db.getCollection("userscollection");
    WriteResult result = collection.remove(new BasicDBObject());

    int userIndex = 1;
    for (int i = 1; i <= 10; i++) {
        JSONObject userDocument = new JSONObject();
        String user = "user" + userIndex;
        userDocument.put("user", user);

        JSONArray followerList = new JSONArray();
        Random randomGenerator = new Random();
        for (int j = 0; j < 3; j++) {
            int followerId = randomGenerator.nextInt(10) + 1;
            // Assumption here is, a user will not be a follower on himself
            while (followerId == userIndex) {
                followerId = randomGenerator.nextInt(10) + 1;
            }/*from   w w w.  j  a va 2 s  .c o m*/

            String follower = "user" + followerId;
            if (!followerList.contains(follower)) {
                followerList.add(follower);
            }
        }
        userDocument.put("followers", followerList);

        JSONArray followingList = new JSONArray();
        for (int k = 0; k < 3; k++) {
            int followingId = randomGenerator.nextInt(10) + 1;
            // Assumption here is, a user will not be following his own tweets
            while (followingId == userIndex) {
                followingId = randomGenerator.nextInt(10) + 1;
            }

            String followingUser = "user" + followingId;
            if (!followingList.contains(followingUser)) {
                followingList.add(followingUser);
            }
        }
        userDocument.put("following", followingList);
        System.out.println("Json string is: " + userDocument.toString());
        DBObject userDBObject = (DBObject) JSON.parse(userDocument.toString());
        collection.insert(userDBObject);
        userIndex++;

    }

    //        try {
    //            FileWriter file = new FileWriter("/Users/dmurty/Documents/MongoData/usersCollection.js");
    //            file.write(usersArray.toJSONString());
    //            file.flush();
    //            file.close();
    //        } catch (IOException ex) {
    //            Logger.getLogger(PopulateUsers.class.getName()).log(Level.SEVERE, null, ex);
    //        } 
}

From source file:com.javamongodb.application.RecordsActivity.java

@Override
public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == editButton) {
        if (editToggle == 0) {
            editButton.setText("   " + messages.getString("disable.edit.records") + "  ");
            isEditable = true;/*from   w  w w. j  a  v  a  2  s.  c  o m*/
            editToggle = 1;
        } else if (editToggle == 1) {
            editButton.setText("   " + messages.getString("enable.edit.records") + "   ");
            isEditable = false;
            editToggle = 0;
        }
    } else if (ae.getSource() == deleteButton) {
        if (jtable.getSelectedRows().length > 0) {
            int i = JOptionPane.showConfirmDialog(null, messages.getString("question.to.delete"),
                    messages.getString("title.delete.confirm"), JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) {
                int j[] = jtable.getSelectedRows();
                try {
                    logger.debug("Opening database connection");
                    DBCollection collection = DatabaseUtils.openDBConnection();
                    for (int count = 0; count < j.length; count++) {
                        String mobileno = (String) jtable.getValueAt(j[count], 9);
                        collection.remove(new BasicDBObject().append("MobileNumber", mobileno)); //remove the document
                    }
                    removeSelectedRows(jtable);
                    logger.debug("Object deletion succeeded");
                } catch (Exception exception) {
                    logger.error("Object deletion failed:\n" + exception.getMessage());
                } finally {
                    logger.debug("Closing database connection");
                    DatabaseUtils.closeDBConnection();
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "First Select the rows to delete...", "Delete Failure",
                    JOptionPane.WARNING_MESSAGE);
        }
    }
}

From source file:com.jhkt.playgroundArena.shared.tasks.MongoDBTaskRunner.java

License:Apache License

public void removeDocument(AbstractDocument removeCriteria) {
    DBCollection collection = getDBCollection(removeCriteria);
    WriteResult wr = collection.remove(removeCriteria.resolveToBasicDBObject());
    processWriteResult(wr);/*from   www.  ja v a  2  s.  co m*/
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoRowLocalServiceImpl.java

License:Open Source License

@Override
public void deleteRow(long tableId, long classPK) throws PortalException {
    ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(tableId);

    DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable);

    dbCollection.remove(new BasicDBObject("classPK", classPK));
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java

License:Open Source License

@Override
public void deleteValues(long classNameId, long classPK) {
    long companyId = CompanyThreadLocal.getCompanyId();

    List<ExpandoTable> expandoTables = ExpandoTableLocalServiceUtil.getTables(companyId, classNameId);

    for (ExpandoTable expandoTable : expandoTables) {
        DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable);

        DBObject queryDBObject = new BasicDBObject();

        queryDBObject.put("companyId", expandoTable.getCompanyId());
        queryDBObject.put("tableId", expandoTable.getTableId());
        queryDBObject.put("rowId", classPK);
        queryDBObject.put("classNameId", expandoTable.getClassNameId());
        queryDBObject.put("classPK", classPK);

        dbCollection.remove(queryDBObject);
    }/*from w w  w. j a v  a2s. co m*/
}

From source file:com.mebigfatguy.mongobrowser.actions.DeleteAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent e) {
    MongoTreeNode node = context.getSelectedNode();
    switch (node.getType()) {
    case Collection: {
        DBCollection collection = (DBCollection) node.getUserObject();
        collection.drop();/* www .  j a va  2s .c o  m*/
        MongoTreeNode dbNode = (MongoTreeNode) node.getParent();
        dbNode.remove(node);
        DefaultTreeModel model = (DefaultTreeModel) context.getTree().getModel();
        model.nodeStructureChanged(dbNode);
    }
        break;

    case Object: {
        DBObject object = (DBObject) node.getUserObject();
        MongoTreeNode collectionNode = TreeUtils.findCollectionNode(node);
        DBCollection collection = (DBCollection) collectionNode.getUserObject();
        collection.remove(object);
        collectionNode.remove(node);
        DefaultTreeModel model = (DefaultTreeModel) context.getTree().getModel();
        model.nodeStructureChanged(collectionNode);
    }
        break;

    case KeyValue: {
        MongoTreeNode.KV kv = (MongoTreeNode.KV) node.getUserObject();
        String key = kv.getKey();
        if (!key.startsWith("_")) {
            MongoTreeNode objectNode = (MongoTreeNode) node.getParent();
            DBObject object = (DBObject) objectNode.getUserObject();
            object.removeField(key);
            MongoTreeNode collectionNode = TreeUtils.findCollectionNode(objectNode);
            DBCollection collection = (DBCollection) collectionNode.getUserObject();
            collection.save(object);
            objectNode.remove(node);
            DefaultTreeModel model = (DefaultTreeModel) context.getTree().getModel();
            model.nodeStructureChanged(objectNode);
        }
    }
        break;
    }
}

From source file:com.miya.twit.mongodb.DBConnectSentiment.java

public void deleteWordFromMongo(String word, String polarity) {
    DBCollection collection = dbConnection();
    BasicDBObject document = new BasicDBObject();
    document.put("text", word);
    document.put("polarity", polarity); //override above value 2
    collection.remove(document);
}

From source file:com.mycompany.bean.PlytaService.java

public String deleteDocument(String DBName, String CollectionName, Plyta PlytaInstance) throws Exception {
    try {//from   www.  ja  v a 2 s  .c o  m
        DBCollection collection = getConnection(DBName, CollectionName);
        BasicDBObject query = new BasicDBObject();
        query.append("tytul", PlytaInstance.getTytul());
        collection.remove(query);

        return "Dokument pomyslnie usuniety";
    } catch (Exception e) {
        System.out.println("Blad podaczas usuwania dokumentu!");
        e.printStackTrace();
    }
    return "Blad poczas usuwania dokumentu";
}

From source file:com.mycompany.model.MarkerDAOImpl.java

@Override
public void delete(String markerId) {

    DBCollection markerCollection = MongoConfig.getMongoConnection().getCollection("markers");
    BasicDBObject query = new BasicDBObject();
    query.append("_id", new ObjectId(markerId));
    markerCollection.remove(query);

}