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(final String key, final Object value) 

Source Link

Document

Creates an object with the given key/value

Usage

From source file:buysell.sign.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 a  2s  .  c  om*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("snehal");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("user");
        System.out.println("Collection created successfully");
        String fullname = jTextField2.getText().toString();

        String email = jTextField3.getText().toString();
        String passwd = jTextField4.getText().toString();
        System.out.println("finalproject.sign.jButton1ActionPerformed()" + fullname);
        if (jTextField2.getText().length() != 0 && jTextField3.getText().length() != 0
                && jTextField4.getText().length() != 0) {
            BasicDBObject doc = new BasicDBObject("name", fullname).

                    append("passwd", passwd);

            coll.insert(doc);

            System.out.println("Document inserted successfully");
            home h1 = new home();
            h1.setVisible(true);
            dispose();
        } else {
            JOptionPane.showMessageDialog(null, "Fill every field");
        }
    } catch (NumberFormatException e) {
        System.out.println();
    }
}

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

License:Open Source License

DBObject getThreeFieldQuery(String field1, String value1, String field2, String value2, String field3,
        String value3) {/*from  w w  w .j  av  a2 s.c om*/
    DBObject query;
    if (field1.equals(JSONKeys._ID)) {
        ObjectId objId = new ObjectId(value1);
        query = new BasicDBObject(field1, objId);
    } else
        query = new BasicDBObject(field1, value1);
    if (field2.equals(JSONKeys._ID)) {
        ObjectId objId = new ObjectId(value2);
        query.put(field2, objId);
    } else
        query.put(field2, value2);
    if (field3.equals(JSONKeys._ID)) {
        ObjectId objId = new ObjectId(value3);
        query.put(field3, objId);
    } else
        query.put(field3, value3);
    return query;
}

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

License:Open Source License

/**
 * PUT a json file to the database//from   w w w .j  a  v  a  2s .c o m
 * @param collName the name of the collection
 * @param docID the docid of the resource 
 * @param json the json to put there
 * @return the server response
 */
@Override
public String putToDb(String collName, String docID, String json) throws DbException {
    try {
        DBObject doc = (DBObject) JSON.parse(json);
        doc.put(JSONKeys.DOCID, docID);
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(JSONKeys.DOCID, docID);
        WriteResult result = coll.update(query, doc, true, false);
        //return removeFromDb( path );
        return result.toString();
    } catch (Exception e) {
        throw new DbException(e);
    }
}

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

License:Open Source License

/**
 * PUT a new json file to the database//from   w  w  w .j a  va 2 s . co  m
 * @param collName the name of the collection
 * @param json the json to put there
 * @return the server response
 */
@Override
public String addToDb(String collName, String json) throws DbException {
    try {
        DBObject doc = (DBObject) JSON.parse(json);
        connect();
        DBCollection coll = getCollectionFromName(collName);
        if (doc.containsField(JSONKeys._ID)) {
            Object id = doc.get(JSONKeys._ID);
            DBObject query = new BasicDBObject(JSONKeys._ID, id);
            if (query != null) {
                WriteResult result = coll.update(query, doc, true, false);
                return result.toString();
            } else
                throw new Exception("Failed to update object " + id);
        } else {
            WriteResult result = coll.insert(doc, WriteConcern.ACKNOWLEDGED);
            // return the new document's id
            ObjectId id = (ObjectId) doc.get("_id");
            JSONObject jDoc = (JSONObject) JSONValue.parse(result.toString());
            jDoc.put("_id", id.toString());
            return jDoc.toJSONString();
        }
    } catch (Exception e) {
        throw new DbException(e);
    }
}

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

License:Open Source License

/**
 * Remove a document from the database by a unique field value
 * @param collName name of the collection
 * @param field the name of the field /* ww  w. ja  va 2s. com*/
 * @param value the value of the field
 * @return the server response
 */
@Override
public String removeFromDbByField(String collName, String field, String value) throws DbException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        //new BasicDBObject("_id", new ObjectId(idString));
        Object obj = value;
        if (field.equals(JSONKeys._ID))
            obj = new ObjectId(value);
        DBObject query = new BasicDBObject(field, obj);
        WriteResult result = coll.remove(query);
        return result.toString();
    } catch (Exception e) {
        throw new DbException(e);
    }
}

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

License:Open Source License

/**
 * Get the metadata as a string/*  w  ww .  j  ava 2 s  . c  o m*/
 * @param docid its filename or docid
 * @return the metadata of the document as a string
 */
public String getMetadata(String docid) {
    try {
        connect();
        DBCollection collection = getCollectionFromName(Database.METADATA);
        DBObject query;
        query = new BasicDBObject(JSONKeys.DOCID, docid);
        DBObject obj = collection.findOne(query);
        if (obj != null) {
            obj.removeField(JSONKeys._ID);
            return obj.toString();
        } else
            return null;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}

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

License:Open Source License

/**
 * Update one field of a database document
 * @param coll the collection the document is in
 * @param findField the field to look for
 * @param findValue the field value to search for
 * @param setField the field to set//  w w  w  . j  a v  a  2 s .c  o m
 * @param setValue the new field value
 * @throws DbException 
 */
public void updateByField(String coll, String findField, Object findValue, String setField, Object setValue)
        throws DbException {
    try {
        connect();
        DBCollection collection = getCollectionFromName(coll);
        if (findField.equals(JSONKeys._ID))
            findValue = new ObjectId((String) findValue);
        BasicDBObject update = new BasicDBObject("$set", new BasicDBObject(setField, setValue));
        BasicDBObject query = new BasicDBObject();
        query.put(findField, findValue);
        WriteResult res = collection.update(query, update);
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Fetch a resource from the server, or try to.
 * @param collName the collection or database name
 * @param docID the path to the resource in the collection
 * @return the response as a string or null if not found
 *//*from w  ww.jav  a  2 s.co m*/
@Override
public String getFromDb(String collName, String docID) throws AeseException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(JSONKeys.DOCID, docID);
        DBObject obj = coll.findOne(query);
        if (obj != null)
            return obj.toString();
        else
            throw new FileNotFoundException("failed to find " + collName + "/" + docID);
    } catch (Exception e) {
        throw new AeseException(e);
    }
}

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * PUT a json file to the database/*  ww  w .  java2 s .com*/
 * @param collName the name of the collection
 * @param docID the docid of the resource 
 * @param json the json to put there
 * @return the server response
 */
@Override
public String putToDb(String collName, String docID, String json) throws AeseException {
    try {
        docIDCheck(collName, docID);
        DBObject doc = (DBObject) JSON.parse(json);
        doc.put(JSONKeys.DOCID, docID);
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(JSONKeys.DOCID, docID);
        WriteResult result = coll.update(query, doc, true, false);
        //return removeFromDb( path );
        return result.toString();
    } catch (Exception e) {
        throw new AeseException(e);
    }
}

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Remove a document from the database//from w  ww.  j  a  v  a 2s. co  m
 * @param collName name of the collection
 * @param docID the docid of the resource 
 * @param json the json to put there
 * @return the server response
 */
@Override
public String removeFromDb(String collName, String docID) throws AeseException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(JSONKeys.DOCID, docID);
        WriteResult result = coll.remove(query);
        return result.toString();
    } catch (Exception e) {
        throw new AeseException(e);
    }
}