Example usage for com.mongodb DBCollection update

List of usage examples for com.mongodb DBCollection update

Introduction

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

Prototype

public WriteResult update(final DBObject query, final DBObject update) 

Source Link

Document

Modify an existing document.

Usage

From source file:com.foodtruckdata.mongodb.UsersInput.java

@Override
public void FollowTruck(String truck_id, String user_id) {
    BasicDBObject filter = new BasicDBObject("_id", new ObjectId(truck_id));
    DBCollection coll = mongoDB.getCollection("Trucks");
    Cursor cursor = coll.find(filter,
            (new BasicDBObject("$elemMatch", new BasicDBObject("Followers", new ObjectId(user_id)))));
    if (!cursor.hasNext()) {
        coll.update(filter, (new BasicDBObject("$addToSet",
                (new BasicDBObject("Trucks.Followers", new BasicDBObject("_id", new ObjectId(user_id)))))));
    }/*from w w  w  .  j av  a  2 s . c  o m*/
}

From source file:com.foodtruckdata.mongodb.UsersInput.java

@Override
public void UnFollowTruck(String truck_id, String user_id) {
    BasicDBObject filter = new BasicDBObject("_id", new ObjectId(truck_id));
    DBCollection coll = mongoDB.getCollection("Trucks");
    coll.update(filter, (new BasicDBObject("$pull",
            (new BasicDBObject("Trucks.Followers", new BasicDBObject("_id", new ObjectId(user_id)))))));
}

From source file:com.foodtruckdata.mongodb.UsersInput.java

@Override
public void RateTruck(String truck_id, String user_id, int rating) {
    //remove original rating
    BasicDBObject filter = new BasicDBObject("_id", new ObjectId(truck_id));
    DBCollection coll = mongoDB.getCollection("Trucks");
    coll.update(filter, (new BasicDBObject("$pull",
            (new BasicDBObject("Trucks.Ratings.UserID", new BasicDBObject("_id", new ObjectId(user_id)))))));
    coll.update(filter, (new BasicDBObject("$addToSet",
            (new BasicDBObject("Trucks.Ratings.UserID", new BasicDBObject("_id", new ObjectId(user_id)))))));
}

From source file:com.gigaspaces.persistency.MongoClientConnector.java

License:Open Source License

public void performBatch(List<BatchUnit> rows) {
    if (logger.isTraceEnabled()) {
        logger.trace("MongoClientWrapper.performBatch(" + rows + ")");
        logger.trace("Batch size to be performed is " + rows.size());
    }//from www  . j  av  a2 s.  c o  m
    //List<Future<? extends Number>> pending = new ArrayList<Future<? extends Number>>();

    for (BatchUnit row : rows) {
        SpaceDocument spaceDoc = row.getSpaceDocument();
        SpaceTypeDescriptor typeDescriptor = types.get(row.getTypeName()).getTypeDescriptor();
        SpaceDocumentMapper<DBObject> mapper = getMapper(typeDescriptor);

        DBObject obj = mapper.toDBObject(spaceDoc);

        DBCollection col = getCollection(row.getTypeName());
        switch (row.getDataSyncOperationType()) {

        case WRITE:
        case UPDATE:
            col.save(obj);
            break;
        case PARTIAL_UPDATE:
            DBObject query = BasicDBObjectBuilder.start()
                    .add(Constants.ID_PROPERTY, obj.get(Constants.ID_PROPERTY)).get();

            DBObject update = normalize(obj);
            col.update(query, update);
            break;
        // case REMOVE_BY_UID: // Not supported by this implementation
        case REMOVE:
            col.remove(obj);
            break;
        default:
            throw new IllegalStateException(
                    "Unsupported data sync operation type: " + row.getDataSyncOperationType());
        }
    }

    /*long totalCount = waitFor(pending);
            
    if (logger.isTraceEnabled()) {
       logger.trace("total accepted replies is: " + totalCount);
    }*/
}

From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java

License:Open Source License

/**
 * update document// w w  w  . j av  a2 s  . co  m
 * 
 * @param userDB
 * @param colName
 * @param dBObject
 * @param key
 * @param value
 * @throws Exception
 */
public static void updateDocument(UserDBDAO userDB, String colName, DBObject dBObject, String key, String value)
        throws Exception {
    DBCollection collection = findCollection(userDB, colName);

    if (logger.isDebugEnabled()) {
        logger.debug("[dBObject] \t " + dBObject);
        logger.debug("===============================[key]\t " + key + "\t [value]\t" + value);
    }
    BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append(key, value));
    //      BasicDBObject newDocument3 = new BasicDBObject().append("$set", new BasicDBObject().append("allPlans.0.cursor", "t2est"));
    collection.update(dBObject, updateQuery);
}

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

License:Open Source License

/**
 * @param args/* w ww.j a va2  s  .com*/
 */
public static void main(String[] args) throws Exception {

    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");
    DBCollection collAddress = db.getCollection("test555");

    BasicDBObject findObj = new BasicDBObject().append("n", 2);
    DBCursor cur = collAddress.find(findObj);
    DBObject dbObj = cur.next();
    System.out.println(dbObj);
    System.out.println("================================================================================");

    if (dbObj != null) {
        BasicDBObject newDocument3 = new BasicDBObject().append("$set",
                new BasicDBObject().append("allPlans.0.cursor", "t2est"));
        //  allPlans.0.cursor
        WriteResult wr = collAddress.update(dbObj, newDocument3);
    }
    //
    //      System.out.println(wr.toString());
    //      
    mongo.close();
}

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

License:Open Source License

public static void exam01(DBCollection collection) throws Exception {
    BasicDBObject newDocument = new BasicDBObject();
    newDocument.put("hosting", "hostB");
    newDocument.put("type", "shared host");
    newDocument.put("clients", 111);

    collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
}

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

License:Open Source License

public static void exam02(DBCollection collection) throws Exception {
    BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("clients", 99));

    collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
}

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

License:Open Source License

public static void exam03(DBCollection collection) throws Exception {
    BasicDBObject newDocument3 = new BasicDBObject().append("$set",
            new BasicDBObject().append("type", "dedicated server"));

    collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3);
}

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

License:Open Source License

public static void exam05(DBCollection collection) throws Exception {
    // find type = vps , update all matched documents , "clients" value to
    // 888/*from   w w  w .  j a v  a2  s. c  o m*/
    BasicDBObject updateQuery = new BasicDBObject().append("$set",
            new BasicDBObject().append("clients", "11111"));

    BasicDBObject findObj = new BasicDBObject().append("hosting", "hostA");
    DBObject dbObj = collection.find(findObj).next();
    System.out.println(dbObj);

    // both methods are doing the same thing.
    // collection.updateMulti(new BasicDBObject().append("type", "vps"),
    // updateQuery);
    collection.update(dbObj, updateQuery);
}