Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject() 

Source Link

Document

Creates an empty object.

Usage

From source file:buysell.shopping.java

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:

    try {//from w  w  w.  j a  v a  2s  .  c o  m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("buysale");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("assetes");
        System.out.println("Collection created successfully");
        //String name = jTextField1.getText().toString();
        //String email = jTextField2.getText().toString();
        //String phoneno = jTextField3.getText().toString();
        // String price= jTextField4.getText().toString();
        //String photourl = jTextField5.getText().toString();
        String check = jCheckBox1.getText().toString();
        System.out.println(check);
        String type = jComboBox1.getSelectedItem().toString();
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("type", type);
        DBCursor cursor = coll.find(searchQuery);
        int i = 0;
        while (cursor.hasNext() && i != 1) {
            i++;
            DBObject obj = cursor.next();
            String name = (String) obj.get("name");
            String email = (String) obj.get("email");
            String phoneno = (String) obj.get("phoneno");
            String price = (String) obj.get("price");
            String url = (String) obj.get("photourl");
            jTextField1.setText(name);
            jTextField2.setText(email);
            jTextField3.setText(phoneno);
            jTextField4.setText(price);
            jLabel3.setIcon(new javax.swing.ImageIcon(url));
        }

        System.out.println("Document inserted successfully");
    } catch (NumberFormatException e) {
        System.out.println();
    }
}

From source file:buysell.subads.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    try {/*from  ww  w  .j  a v a2s .c o  m*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("buysale");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("assetes");
        System.out.println("Collection created successfully");
        String name = jTextField1.getText().toString();
        String email = jTextField2.getText().toString();
        String phoneno = jTextField3.getText().toString();
        String price = jTextField4.getText().toString();
        String photourl = jTextField5.getText().toString();
        String type = jComboBox1.getSelectedItem().toString();
        DB dB = mongoClient.getDB("snehal");
        System.out.println("Connect to database successfully");
        DBCollection colle = dB.getCollection("user");
        System.out.println("Collection created successfully");
        String uname = jTextField6.getText().toString();
        String passwd = jPasswordField1.getText().toString();
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", uname);
        searchQuery.put("passwd", passwd);

        DBCursor cursor = colle.find(searchQuery);
        int i = 0;
        while (cursor.hasNext()) {
            i = 1;
            System.out.println(cursor.next());
        }
        if (i == 0) {
            JOptionPane.showMessageDialog(null, "user name or passwod is wrong");

        } else {
            if (jTextField2.getText().length() != 0 && jTextField3.getText().length() != 0
                    && jTextField4.getText().length() != 0 && jTextField1.getText().length() != 0
                    && jTextField5.getText().length() != 0) {

                BasicDBObject doc = new BasicDBObject("name", name).append("username", uname)
                        .append("passwd", passwd).append("email", email).append("phoneno", phoneno)
                        .append("price", price).append("type", type).append("photourl", photourl);

                coll.insert(doc);
                home h2 = new home();
                h2.setVisible(true);
                dispose();
                System.out.println("Document inserted successfully");
            } else {
                JOptionPane.showMessageDialog(null, "enter the every field");
            }
        }

    } catch (NumberFormatException e) {
        System.out.println();
    }
}

From source file:byusentiment.Byusentiment.java

public static void loadTweets() throws IOException {
    try {/* w ww . ja  va 2s  .  c  om*/
        CSVReader reader = new CSVReader(new FileReader("tweets-2.csv"));
        //reader.readNext();
        List myEntries = reader.readAll();
        String[] headers = (String[]) myEntries.get(0);

        //Adding data to the DbObject
        List<DBObject> toInsert = new ArrayList<DBObject>();
        for (int i = 1; i < myEntries.size(); i++) {
            BasicDBObject doc = new BasicDBObject();
            String[] row = (String[]) myEntries.get(i);

            for (int j = 0; j < row.length; j++) {
                doc.append(headers[j], row[j]);
            }
            toInsert.add(doc);
        }

        //Inserting data into the database             
        Byusentiment.coll.insert(toInsert);

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Byusentiment.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * Remove an entire set of documents that match a regular expression.
 * @param collName the collection to remove from
 * @param key the key field to match/*from   w w  w . j av a  2  s . com*/
 * @param expr the regular expression for key's values
 * @return the result
 * @throws DbException 
 */
public String removeFromDbByExpr(String collName, String key, String expr) throws DbException {
    try {
        DBCollection coll = getCollectionFromName(collName);
        if (coll != null) {
            BasicDBObject q = new BasicDBObject();
            q.put(key, Pattern.compile(expr));
            WriteResult result = coll.remove(q);
            return result.toString();
        } else
            throw new Exception("Collection " + collName + " not found");
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * Get a list of docIDs or file names corresponding to the regex expr
 * @param collName the collection to query
 * @param expr the regular expression to match against docid
 * @param key the key to retrieve for each matching document
 * @return an array of matching docids, which may be empty
 *//*from  w ww  .  j a v  a  2s  .c o  m*/
@Override
public String[] listDocuments(String collName, String expr, String key) throws DbException {
    try {
        try {
            connect();
        } catch (Exception e) {
            throw new DbException(e);
        }
        DBCollection coll = getCollectionFromName(collName);
        if (coll != null) {
            BasicDBObject q = new BasicDBObject();
            q.put(JSONKeys.DOCID, Pattern.compile(expr));
            DBCursor curs = coll.find(q);
            ArrayList<String> docids = new ArrayList<String>();
            Iterator<DBObject> iter = curs.iterator();
            int i = 0;
            while (iter.hasNext()) {
                Object kId = iter.next().get(key);
                if (kId != null)
                    docids.add(kId.toString());
            }
            String[] array = new String[docids.size()];
            docids.toArray(array);
            return array;
        } else
            throw new DbException("collection " + collName + " not found");
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * List all the documents in a Mongo collection
 * @param collName the name of the collection
 * @param key the document key to retrieve by
 * @return a String array of document keys
 * @throws DbException // w  w w .  j ava 2  s.c o  m
 */
@Override
public String[] listCollectionByKey(String collName, String key) throws DbException {
    try {
        connect();
    } catch (Exception e) {
        throw new DbException(e);
    }
    DBCollection coll = getCollectionFromName(collName);
    BasicDBObject keys = new BasicDBObject();
    keys.put(key, 1);
    DBCursor cursor = coll.find(new BasicDBObject(), keys);
    if (cursor.length() > 0) {
        String[] docs = new String[cursor.length()];
        Iterator<DBObject> iter = cursor.iterator();
        int i = 0;
        while (iter.hasNext()) {
            DBObject dbObj = iter.next();
            Object obj = dbObj.get(key);
            if (key.equals(JSONKeys._ID)) {
                ObjectId id = (ObjectId) dbObj.get(JSONKeys._ID);
                obj = id.toStringMongod();
                docs[i++] = (String) obj;
            } else
                docs[i++] = obj.toString();
        }
        return docs;
    } else
        return new String[0];
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * List all the documents in a Mongo collection
 * @param collName the name of the collection
 * @return a String array of document keys
 * @throws DbException //from  w  ww .  j  ava  2 s .co m
 */
@Override
public String[] listCollection(String collName) throws DbException {
    try {
        connect();
    } catch (Exception e) {
        throw new DbException(e);
    }
    DBCollection coll = getCollectionFromName(collName);
    BasicDBObject keys = new BasicDBObject();
    keys.put(JSONKeys.DOCID, 1);
    DBCursor cursor = coll.find(new BasicDBObject(), keys);
    if (cursor.length() > 0) {
        String[] docs = new String[cursor.length()];
        Iterator<DBObject> iter = cursor.iterator();
        int i = 0;
        while (iter.hasNext())
            docs[i++] = (String) iter.next().get(JSONKeys.DOCID);
        return docs;
    } else
        return new String[0];
}

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Get a list of docIDs or file names corresponding to the regex expr
 * @param collName the collection to query
 * @param expr the regular expression to match against docid
 * @return an array of matching docids, which may be empty
 *//* w ww. j  a  va2 s  .  c  o  m*/
@Override
public String[] listDocuments(String collName, String expr) throws AeseException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        if (coll != null) {
            BasicDBObject q = new BasicDBObject();
            q.put(JSONKeys.DOCID, Pattern.compile(expr));
            DBCursor curs = coll.find(q);
            ArrayList<String> docids = new ArrayList<String>();
            Iterator<DBObject> iter = curs.iterator();
            int i = 0;
            while (iter.hasNext()) {
                String dId = (String) iter.next().get(JSONKeys.DOCID);
                if (dId.matches(expr))
                    docids.add(dId);
            }
            String[] array = new String[docids.size()];
            docids.toArray(array);
            return array;
        } else
            throw new AeseException("collection " + collName + " not found");
    } catch (Exception e) {
        throw new AeseException(e);
    }
}

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * List all the documents in a Mongo collection
 * @param collName the name of the collection
 * @return a String array of document keys
 * @throws AeseException //  w ww .j  av a  2s.  c o m
 */
@Override
public String[] listCollection(String collName) throws AeseException {
    if (!collName.equals(Database.CORPIX)) {
        try {
            connect();
        } catch (Exception e) {
            throw new AeseException(e);
        }
        DBCollection coll = getCollectionFromName(collName);
        BasicDBObject keys = new BasicDBObject();
        keys.put(JSONKeys.DOCID, 1);
        DBCursor cursor = coll.find(new BasicDBObject(), keys);
        System.out.println("Found " + cursor.count() + " documents");
        cursor.count();
        if (cursor.length() > 0) {
            String[] docs = new String[cursor.length()];
            Iterator<DBObject> iter = cursor.iterator();
            int i = 0;
            while (iter.hasNext())
                docs[i++] = (String) iter.next().get(JSONKeys.DOCID);
            return docs;
        } else {
            return new String[0];
        }
    } else {
        GridFS gfs = new GridFS(db, collName);
        DBCursor curs = gfs.getFileList();
        int i = 0;
        List<DBObject> list = curs.toArray();
        HashSet<String> set = new HashSet<String>();
        Iterator<DBObject> iter = list.iterator();
        while (iter.hasNext()) {
            String name = (String) iter.next().get("filename");
            set.add(name);
        }
        String[] docs = new String[set.size()];
        set.toArray(docs);
        return docs;
    }
}

From source file:Candidate.Candidate.java

public BasicDBObject toDBObject() {
    BasicDBObject doc = new BasicDBObject();
    ArrayList<BasicDBObject> elemCandidates = new ArrayList<>();
    for (Entry<Source, OntologicalElement> e : this.uriImplicate.entrySet()) {
        Source s = e.getKey();//from   ww  w  .  jav a 2s .  c  o  m
        String elem = e.getValue().getUri();
        if (!elem.isEmpty()) {
            BasicDBObject elemC = new BasicDBObject();
            elemC.append("source", s.toDBObject());
            elemC.append("elem", elem);
            elemCandidates.add(elemC);
        }
    }
    doc.append("elemCandidates", elemCandidates);
    doc.append("trustScoreChoquet", this.getTrustScore());
    doc.append("trustScoreSimple", this.trustSimple);

    return doc;
}