Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:edu.umass.cs.gnsserver.database.MongoRecords.java

License:Apache License

@Override
public void removeMapKeys(String collectionName, String name, ColumnField mapField,
        ArrayList<ColumnField> mapKeys) throws FailedDBOperationException {
    String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    DBCollection collection = db.getCollection(collectionName);
    BasicDBObject query = new BasicDBObject(primaryKey, name);

    BasicDBObject updates = new BasicDBObject();

    if (mapField != null && mapKeys != null) {
        for (int i = 0; i < mapKeys.size(); i++) {
            String fieldName = mapField.getName() + "." + mapKeys.get(i).getName();
            updates.append(fieldName, 1);
        }//from  w w  w  .  j  av a  2  s  . c  o  m
    }
    if (updates.keySet().size() > 0) {
        try {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} <============>unset{1}<============>",
                    new Object[] { dbName, updates.toString() });
            collection.update(query, new BasicDBObject("$unset", updates));
        } catch (MongoException e) {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} removeMapKeys failed: {1}",
                    new Object[] { dbName, e.getMessage() });
            throw new FailedDBOperationException(collectionName, updates.toString(),
                    "Original mongo exception:" + e.getMessage());
        }
    }
}

From source file:edu.upf.nets.mercury.mongo.DbObject.java

License:Open Source License

/**
 * Creates a MongoDB object corresponding to the current object. 
 * @return/*from www.j  a va 2s .co m*/
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
public final BasicDBObject get() throws IllegalArgumentException, IllegalAccessException {
    // Create an empty database object.
    BasicDBObject object = new BasicDBObject();
    // Set the object ID field, if the current field.
    if (null != this._id) {
        object.append("_id", this._id);
    }
    // Get the class of the current object.
    Class<?> c = this.getClass();
    // Get all field for the class of the current object.
    Field[] fields = c.getDeclaredFields();
    // Add all field values using reflection.
    for (Field field : fields) {
        // Get the modifiers of the current field.
        int modifiers = field.getModifiers();
        // If the field is public, not static and not final.
        if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers)) {
            // Get the field name.
            String name = field.getName();
            // Get the field value for the current object.
            Object value = field.get(this);
            // Add the field information to the database object.
            object = object.append(name, value);
        }
    }
    // Return the object.
    return object;
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void Insert(FlightInfo info, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection flight = db.getCollection("flight");
    BasicDBObject query = new BasicDBObject("FL_DATE", info.getDate()).append("UNIQUE_CARRIER",
            info.getCarrier());//from  w  ww . jav  a 2s  .  c  o  m
    query.append("FL_NUM", info.getTailNum()).append("ORIGIN_CITY_NAME", info.getDepCity());
    query.append("DEST_CITY_NAME", info.getArrCity()).append("CRS_DEP_TIME", info.getCrsDepTime());
    query.append("CRS_ARR_TIME", info.getCrsArrTime()).append("DISTANCE", info.getDistance());
    query.append("CANCELLED", info.getCancel());
    if (info.getCancel() == 0) {
        query.append("DEP_TIME", info.getDepTime()).append("DEP_DELAY", info.getDepDelay());
        query.append("ARR_TIME", info.getArrTime()).append("ARR_DELAY", info.getArrDelay());
    }
    System.out.println(query);
    flight.insert(query);
    //mongoClient.close();

}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void Update(String id, FlightInfo newInfo, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection flight = db.getCollection("flight");
    ObjectId oId = new ObjectId(id);
    BasicDBObject query0 = new BasicDBObject("_id", oId);
    BasicDBObject query = new BasicDBObject("FL_DATE", newInfo.getDate()).append("UNIQUE_CARRIER",
            newInfo.getCarrier());/*from   www  . j a v a2s  .  c o m*/
    query.append("FL_NUM", newInfo.getTailNum()).append("ORIGIN_CITY_NAME", newInfo.getDepCity());
    query.append("DEST_CITY_NAME", newInfo.getArrCity()).append("CRS_DEP_TIME", newInfo.getCrsDepTime());
    query.append("CRS_ARR_TIME", newInfo.getCrsArrTime()).append("DISTANCE", newInfo.getDistance());
    query.append("CANCELLED", newInfo.getCancel());
    if (newInfo.getCancel() == 0) {
        query.append("DEP_TIME", newInfo.getDepTime()).append("DEP_DELAY", newInfo.getDepDelay());
        query.append("ARR_TIME", newInfo.getArrTime()).append("ARR_DELAY", newInfo.getArrDelay());
    }
    System.out.println(query0);
    System.out.println(query);
    flight.update(query0, query);
    //mongoClient.close();
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static DBCursor Search(SearchInfo sInfo, String colName, MongoClient mongoClient) {

    BasicDBObject query = new BasicDBObject();

    //Time Range/*from w  w  w  .  ja v  a2s .  c  o  m*/
    if (sInfo.getUpDate() != null && sInfo.getDownDate() != null) {
        String upDateStr = sInfo.getUpDate().toString();
        String downDateStr = sInfo.getDownDate().toString();
        DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd");
        LocalDate upDate = df.parseLocalDate(upDateStr);
        LocalDate downDate = df.parseLocalDate(downDateStr);
        ArrayList dates = new ArrayList();
        for (LocalDate date = downDate; date.isBefore(upDate.plusDays(1)); date = date.plusDays(1)) {
            dates.add(date.toString());
        }
        query.append("FL_DATE", new BasicDBObject("$in", dates));
    }
    //Carrier
    if (sInfo.getCarrier() != null) {
        query.append("UNIQUE_CARRIER", sInfo.getCarrier());
    }
    //TailNum
    if (sInfo.getTailNum() != 0) {
        query.append("FL_NUM", sInfo.getTailNum());
    }
    //DepCity
    if (sInfo.getDepCity() != null) {
        query.append("ORIGIN_CITY_NAME", sInfo.getDepCity());
    }
    //ArrCity
    if (sInfo.getArrCity() != null) {
        query.append("DEST_CITY_NAME", sInfo.getArrCity());
    }
    //DistanceRange
    query.append("DISTANCE", new BasicDBObject("$gt", sInfo.getDownDis()).append("$lt", sInfo.getUpDis()));
    //DepTimeRange
    query.append("CRS_DEP_TIME",
            new BasicDBObject("$gt", sInfo.getDownDepTime()).append("$lt", sInfo.getUpDepTime()));
    //ArrTimeRange
    query.append("CRS_ARR_TIME",
            new BasicDBObject("$gt", sInfo.getDownArrTime()).append("$lt", sInfo.getUpArrTime()));
    //Cancel
    if (sInfo.getCancel() != -1) {
        query.append("CANCELLED", sInfo.getCancel());
    }

    System.out.println(query);
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection flight = db.getCollection(colName);
    DBCursor cursor = flight.find(query);
    return cursor;
}

From source file:entitiesConverters.AlbumConverter.java

public static DBObject convertAlbumToDBObject(Album album) {
    BasicDBObject dbAlbum = new BasicDBObject("title", album.getTitle()).append("artist", album.getArtist())
            .append("genre", album.getGenre()).append("label", album.getLabel())
            .append("coverPath", album.getCoverPath()).append("year", album.getYear())
            .append("collection", album.getCollection());

    BasicDBList dbTags = new BasicDBList();
    if (album.getTags() != null && !album.getTags().isEmpty()) {
        for (String tag : album.getTags()) {
            dbTags.add(tag);//from www.j ava2  s.c  o m
        }
    }
    dbAlbum.append("tags", dbTags);

    BasicDBList dbTracks = new BasicDBList();
    if (album.getTracks() != null && !album.getTracks().isEmpty()) {
        for (Track track : album.getTracks()) {
            dbTracks.add(TrackConverter.convertTrackToDBObject(track));
        }
    }
    dbAlbum.append("tracks", dbTracks);

    return dbAlbum;
}

From source file:Entity.Devoir.java

public BasicDBList mapBddRubriques() {
    BasicDBList result = new BasicDBList();
    for (Rubrique rubrique : lstRubrique) {
        BasicDBObject dbRubriques = new BasicDBObject();
        //dbRubriques.append("_id", rubrique.getId());
        dbRubriques.append("libelle", rubrique.getLibelle());
        dbRubriques.append("lstCritere", rubrique.mapBddCritere());
        result.add(dbRubriques);//  w ww . ja va  2 s  . c  o  m
    }

    return result;
}

From source file:Entity.Devoir.java

public BasicDBList mapBddGroupes() {
    BasicDBList result = new BasicDBList();
    for (Groupe groupe : lstGroupe) {
        BasicDBObject dbGroupes = new BasicDBObject();
        //dbRubriques.append("_id", groupe.getId());
        dbGroupes.append("libelle", groupe.getLibelle());
        dbGroupes.append("lstEtudiant", groupe.mapBddEtudiants());
        dbGroupes.append("lstNote", groupe.mapBddNotes());
        result.add(dbGroupes);//from  ww  w .  j ava  2  s. com
    }

    return result;
}

From source file:Entity.Groupe.java

public BasicDBList mapBddNotes() {
    BasicDBList result = new BasicDBList();
    for (Note note : lstNote) {
        BasicDBObject dbNotes = new BasicDBObject();
        //dbRubriques.append("_id", etudiant.getId());
        dbNotes.append("libelle", note.getLibelle());
        dbNotes.append("poid", note.getPoid());
        dbNotes.append("note", note.getNote());
        result.add(dbNotes);//w  w  w .j  a  v  a 2  s . c o m
    }

    return result;
}

From source file:Entity.Groupe.java

public BasicDBList mapBddEtudiants() {
    BasicDBList result = new BasicDBList();
    for (Etudiant etudiant : lstEtudiant) {
        BasicDBObject dbEtudiants = new BasicDBObject();
        //dbRubriques.append("_id", etudiant.getId());
        dbEtudiants.append("nom", etudiant.getNom());
        dbEtudiants.append("prenom", etudiant.getPrenom());
        dbEtudiants.append("classe", etudiant.getClasse());
        result.add(dbEtudiants);// ww w .j av a2  s  .  co m
    }

    return result;
}