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:pruebamongo.PruebaMongo.java

private static void eliminarDocumentos(MongoClient mongo) {
    DB db = mongo.getDB("prueba");
    DBCollection col = db.getCollection("contactos");
    try {/*from   www  .  j  a v a 2  s.  c  o  m*/
        //Instruccin que va a eliminar los elementos especificados
        col.remove(new BasicDBObject("nombre", "Eddard"));
        System.out.println("Elemento elimado satisfactoriamente.");
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
}

From source file:pt.tiago.mongodbteste.MongoDB.java

private void emptyCollections() {
    for (DBCollection collectionDB : collection) {
        collectionDB.remove(new BasicDBObject());
    }//from  ww  w . j a v  a  2  s.  c  om
}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

public void removeAll(String sheetName) {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    BasicDBObject query = new BasicDBObject();
    query.put(KEY, sheetName);/*  www.  j a v  a  2s . co m*/
    collection.remove(query);
}

From source file:rapture.sheet.mongodb.MongoMetaSheetStore.java

License:Open Source License

private Boolean standardDelete(String name, String itemName, String type) {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    BasicDBObject query = new BasicDBObject();
    query.append(TYPE, type);//from ww w  . j a  v  a  2s  . com
    query.append(KEY, name);
    query.append(NAME, itemName);
    WriteResult res = collection.remove(query);
    return res.getN() != 0;
}

From source file:serwlety.usun.OsobaServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  ww  w  . j  a  v  a2  s  .co m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    DBCollection db = baza.FabrykaPolaczenia.getInstance().getConnection().getCollection("Osoba");
    String param = request.getParameter("id");
    BasicDBObject obiekt = new BasicDBObject();
    obiekt.put("_id", new ObjectId(param));
    db.remove(obiekt);
    response.sendRedirect(request.getContextPath() + "/lista/osoba");
}

From source file:stirling.fix.session.store.MongoSessionStore.java

License:Apache License

private void clear(DBCollection collection, BasicDBObject query) {
    DBCursor cursor = collection.find(query);
    while (cursor.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cursor.next();
        collection.remove(doc);
    }//www.  j a  v  a 2 s  .c o  m
}

From source file:twittermongodbapp.CollectTweets.java

static public void collectTweets(DB db, ConfigurationBuilder cf1, twitter4j.Twitter twitter) {

    TwitterStream twitterStream = new TwitterStreamFactory(cf1.build()).getInstance();

    DBCollection collection = db.getCollection("collection");
    DBCollection collection2 = db.getCollection("collection2");
    BasicDBObject document0 = new BasicDBObject();
    collection.remove(document0);
    collection2.remove(document0);/*  w  w w. j  a  v  a2s .  c  o  m*/
    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {
            if (collection2.getCount() == 15000)
                System.exit(0);
            String json = DataObjectFactory.getRawJSON(status);
            DBObject doc = (DBObject) JSON.parse(json);
            try {
                collection2.insert(doc);
            } catch (Exception e) {
                System.out.println("MongoDB Connection Error : " + e.getMessage());
            }

            System.out.println("Collected tweets :" + collection2.getCount());

        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {

        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub
            System.out.println(arg0);
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub
            System.out.println(arg0);
        }
    };
    getTopTrends(twitter, twitterStream, listener);
}

From source file:twittermongodbapp.SaveTweets.java

static public void saveTweets(DB db, twitter4j.Twitter twitter) throws TwitterException {
    DBCollection collection = db.getCollection("collection2");
    DBCollection savedCollection = db.getCollection("savedCollection");
    DBCollection ConnectionCollection = db.getCollection(" ConnectionCollection");
    BasicDBObject document0 = new BasicDBObject();
    savedCollection.remove(document0);
    ConnectionCollection.remove(document0);
    Status savedTweet;//  ww  w.  j  a v a2 s  .c  o  m

    //Read Tweets from Database
    System.out.println("Saved tweets: ");
    DBCursor cursor = collection.find();
    List<String> allUsers = new ArrayList<>();
    int count = 1;

    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        savedTweet = TwitterObjectFactory.createStatus(obj.toString());

        String name = savedTweet.getUser().getScreenName();
        String date = savedTweet.getCreatedAt().toString();
        boolean exist = false;

        BasicDBObject document = new BasicDBObject("User", name);
        BasicDBObject searchDocument = new BasicDBObject("User", name);
        DBObject theObj = null;

        if (!allUsers.contains(name)) {
            allUsers.add(name);
        } else {
            exist = true;
            DBCursor cursor2 = savedCollection.find(searchDocument);
            if (cursor2.hasNext()) {
                theObj = cursor2.next();
            }

        }

        // BasicDBList allHashtags = new BasicDBList();
        List<String> allHashtags = new ArrayList<>();
        List<String> allsortURLS = new ArrayList<>();
        List<String> allcompleteURLS = new ArrayList<>();
        //BasicDBList allURLS = new BasicDBList();
        List<String> allmentionedUsers = new ArrayList<>();
        List<String> allTweets = new ArrayList<>();

        if (exist) {
            allHashtags = (ArrayList) theObj.get("Hashtags");
        }
        HashtagEntity[] hashtagsEntities = savedTweet.getHashtagEntities();
        for (HashtagEntity hashtagsEntitie : hashtagsEntities) {
            if (!allHashtags.contains(hashtagsEntitie.getText())) {
                allHashtags.add(hashtagsEntitie.getText());
            }
            saveConnection(date, name, new BasicDBObject("hashtag", hashtagsEntitie.getText()), 1, count,
                    ConnectionCollection);
            count++;
        }
        document.append("Hashtags", allHashtags);

        if (exist) {
            allsortURLS = (ArrayList) theObj.get("sortUrls");
            allcompleteURLS = (ArrayList) theObj.get("completeUrls");
        }
        URLEntity[] urlEntities = savedTweet.getURLEntities();
        if (savedTweet.getURLEntities().length > 0) {
            for (int i = 0; i < savedTweet.getURLEntities().length; i++) {
                if (urlEntities[i].getStart() < urlEntities[i].getEnd()) {

                    BasicDBObject document1 = new BasicDBObject("url", urlEntities[i].getURL());
                    String completeURL = urlEntities[i].getExpandedURL();
                    document1.append("completeURL", completeURL);
                    if (!allsortURLS.contains(document1)) {
                        allsortURLS.add(urlEntities[i].getURL());
                        allcompleteURLS.add(urlEntities[i].getExpandedURL());
                    }
                    saveConnection(date, name, document1, 2, count, ConnectionCollection);
                    count++;
                }
            }
        }
        document.append("sortUrls", allsortURLS);
        document.append("completeUrls", allsortURLS);

        if (exist) {
            allmentionedUsers = (ArrayList) theObj.get("Mentions");
        }
        UserMentionEntity[] mentionEntities = savedTweet.getUserMentionEntities();
        for (UserMentionEntity mentionEntitie : mentionEntities) {
            BasicDBObject document2 = new BasicDBObject("mentioned_user", mentionEntitie.getText());
            if (!allmentionedUsers.contains(mentionEntitie.getText())) {
                allmentionedUsers.add(mentionEntitie.getText());
            }
            saveConnection(date, name, document2, 3, count, ConnectionCollection);
            count++;
        }
        document.append("Mentions", allmentionedUsers);

        if (exist) {
            allTweets = (ArrayList) theObj.get("Tweets");
        }
        String tweetText = " ";
        if (savedTweet.isRetweet()) {
            tweetText = savedTweet.getRetweetedStatus().getText();
        } else {
            tweetText = savedTweet.getText();
        }
        BasicDBObject document2 = new BasicDBObject("Tweet", tweetText);
        if (!allTweets.contains(tweetText)) {
            allTweets.add(tweetText);
        }
        saveConnection(date, name, document2, 4, count, ConnectionCollection);
        count++;
        document.append("Tweets", allTweets);

        if (exist) {
            savedCollection.remove(searchDocument);
        }
        savedCollection.insert(document);

    }

    JaccardSimilarity js = new JaccardSimilarity();
    List<Map<List<String>, List<Float>>> list = new ArrayList<>();//This is the final list you need
    Map<List<String>, List<Float>> map1 = new HashMap<>();//This is one instance of the   map you want to store in the above map
    List<String> usersNames = new ArrayList<>();
    List<Float> usersResults = new ArrayList<>();

    BasicDBObject doc1;
    BasicDBObject doc2;
    DBCursor cursor1;
    DBCursor cursor2;
    DBObject obj1;
    DBObject obj2;
    List<String> tl1 = new ArrayList();
    List<String> tl2 = new ArrayList();

    float countSimilarity = 0;

    for (int i = 0; i < allUsers.size(); i++) {
        for (int j = i + 1; j < allUsers.size(); j++) {
            //System.out.println(i+" "+j);
            System.out.print(allUsers.get(i) + " " + allUsers.get(j) + " ");
            usersNames.add(allUsers.get(i));
            usersNames.add(allUsers.get(j));
            doc1 = new BasicDBObject("User", allUsers.get(i));
            doc2 = new BasicDBObject("User", allUsers.get(j));
            cursor1 = savedCollection.find(doc1);
            cursor2 = savedCollection.find(doc2);
            if (cursor1.hasNext() && cursor2.hasNext()) {
                obj1 = cursor1.next();
                obj2 = cursor2.next();
                tl1 = (ArrayList) obj1.get("Hashtags");
                tl2 = (ArrayList) obj2.get("Hashtags");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                tl1 = (ArrayList) obj1.get("Mentions");
                tl2 = (ArrayList) obj2.get("Mentions");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                tl1 = (ArrayList) obj1.get("Tweets");
                tl2 = (ArrayList) obj2.get("Tweets");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                tl1 = (ArrayList) obj1.get("sortUrls");
                tl2 = (ArrayList) obj2.get("sortUrls");
                countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2);
                usersResults.add(js.findSimilarity(tl1, tl2));
                System.out.print(js.findSimilarity(tl1, tl2) + " ");
                usersResults.add((float) (countSimilarity / 4));
                System.out.print((float) (countSimilarity / 4) + " ");
                System.out.println();
                countSimilarity = 0;

            }

        }
        map1.put(usersNames, usersResults);
    }
    list.add(map1);

    //System.out.println(savedCollection.count());

    /*
    // Read every Element Example
    DBCursor cursor2 = savedCollection.find(searchDocument);
    if (cursor2.hasNext()) {
    theObj = cursor2.next();
    //String l =  ( String) cursor2.one().get("exist").toString();
    }
            
    BasicDBList list = new BasicDBList();
    list = (BasicDBList) theObj.get("Hashtags");
    BasicDBList l2 = new BasicDBList();
    for (int i = 0; i < list.size(); i++) {
    BasicDBObject bj = (BasicDBObject) list.get(i);
    System.out.println(bj.getString("hashtag"));
    }
            
    DBCursor cursor2 = savedCollection.find();
     List<String> list = new ArrayList();
               
    while (cursor2.hasNext()) {
    BasicDBObject document2 = (BasicDBObject) cursor2.next();
      String bj = (String) document2.get("User");
    System.out.println(bj);
     for (int i = 0; i < list.size(); i++) {
           // String bj = (String) list.get(i);
            // System.out.println(bj.getString("hashtag"));
              System.out.println(bj);
        }
     */
}

From source file:uk.ac.soton.itinnovation.sad.service.services.SchedulingService.java

License:Open Source License

/**
 * Deletes a job and all its data.//from  ww w  . j a  va 2  s  .co m
 *
 * @param job
 * @return
 * @throws SchedulerException
 */
public boolean deleteJob(SADJob job) throws SchedulerException {
    logger.debug("Removing job [" + job.getIdAsString() + "] and its data");

    // remove from scheduler first
    try {
        cancelJob(job);
    } catch (Throwable e) {
        logger.error("Failed to cancel job", e);
    }

    // remove all data
    DBCollection collection = getCoordinator(false).getDBCollection(SADCollections.SADJobData);
    DBCursor cursor = collection.find(new BasicDBObject("SADJobID", job.getId()));
    while (cursor.hasNext()) {
        collection.remove(cursor.next());
    }

    // remove database entry
    collection = getCoordinator(false).getDBCollection(SADCollections.SADJobs);
    cursor = collection.find(new BasicDBObject("_id", job.getId()));
    while (cursor.hasNext()) {
        collection.remove(cursor.next());
    }
    return true;
}

From source file:uk.ac.susx.tag.classificationframework.datastructures.CacheManager.java

License:Apache License

public void deleteCache(String databaseName, String collectionName, int configuration) {
    DB db = client.getDB(databaseName);//from   ww  w . j a  va2  s .  co m
    if (db.collectionExists(collectionName)) {
        DBCollection collection = db.getCollection(collectionName);
        collection.remove(new BasicDBObject("pipelineConfig", configuration));
    }
}