Example usage for com.mongodb BasicDBObject removeField

List of usage examples for com.mongodb BasicDBObject removeField

Introduction

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

Prototype

public Object removeField(final String key) 

Source Link

Document

Deletes a field from this object.

Usage

From source file:fr.gouv.vitam.mdbes.ElasticSearchAccess.java

License:Open Source License

/**
 * Should be called only once saved (last time), but for the moment let the object as it is, next should remove not indexable
 * entries//from  w ww .j ava 2s. c o m
 *
 * @param dbvitam
 * @param model
 * @param indexes
 * @param bson
 * @return the number of DAip incorporated (0 if none)
 */
public static final int addEsIndex(final MongoDbAccess dbvitam, final String model,
        final Map<String, String> indexes, final BSONObject bson) {
    BasicDBObject maip = getFiltered(bson);
    final String id = maip.getString(VitamType.ID);
    maip.removeField(VitamType.ID);
    // System.err.println(maip);
    // System.err.println(this);
    indexes.put(id, maip.toString());
    int nb = 0;
    if (indexes.size() > GlobalDatas.LIMIT_ES_NEW_INDEX) {
        nb = indexes.size();
        dbvitam.addEsEntryIndex(indexes, model);
        // dbvitam.flushOnDisk();
        indexes.clear();
        System.out.print("o");
    }
    maip.clear();
    maip = null;
    return nb;
}

From source file:fr.gouv.vitam.mdbes.VitamType.java

License:Open Source License

/**
 * Save the document if new, update it (keeping non set fields, replacing set fields)
 *
 * @param collection//from ww  w.  j  a v  a 2s. c  om
 */
protected final void updateOrSave(final VitamCollection collection) {
    final String id = (String) this.get(ID);
    if (id == null) {
        setNewId();
        collection.collection.save(this);
    } else {
        final BasicDBObject upd = new BasicDBObject(this);
        upd.removeField(ID);
        collection.collection.update(new BasicDBObject(ID, id), new BasicDBObject("$set", upd));
    }
}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int listFriends(int requesterID, int profileOwnerID, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result, boolean insertImage, boolean testMode) {
    int retVal = 0;
    if (requesterID < 0 || profileOwnerID < 0)
        return -1;

    // first get all confirmed friendids for profileOwnerID
    com.mongodb.DB db = null;/*from  ww w .jav  a 2 s .  c  om*/
    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("users");
        DBObject q = new BasicDBObject().append("_id", profileOwnerID);
        q.removeField("pic");
        BasicDBObject attrs = new BasicDBObject("pic", 0);

        DBObject queryResult = null;
        queryResult = collection.findOne(q, attrs);

        String x = queryResult.get("ConfFriends").toString();

        if (!x.equals("")) {
            x = x.substring(2, x.length() - 1);
            if (!x.equals("")) {
                String friendIds[] = x.split(",");
                BasicDBObject query = new BasicDBObject();
                if (!friendListReq) {
                    List<Integer> list = new ArrayList<Integer>();
                    for (int i = 0; i < friendIds.length; i++) {
                        // add to list
                        list.add(Integer.parseInt(friendIds[i].trim()));
                        int cnt = 0;
                        if (i == friendIds.length - 1 || ((i + 1) % 10) == 0) {
                            // query
                            query.put("_id", new BasicDBObject("$in", list));
                            query.removeField("pic");
                            // DBCursor cursor = collection.find(query,
                            // fieldsObj);
                            DBCursor cursor = collection.find(query, attrs);
                            while (cursor.hasNext()) {
                                cnt++;
                                // System.out.println(cursor.next());
                                HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                                DBObject curs = cursor.next();
                                if (curs.get("tpic") != null) {
                                    byte[] myPic = (byte[]) curs.get("tpic");
                                    vals.put("tpic", new ObjectByteIterator(myPic));
                                    if (testMode) {
                                        // Save loaded image from database
                                        // into new image file
                                        FileOutputStream fos = new FileOutputStream(
                                                profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                        fos.write(myPic);
                                        fos.close();
                                    }
                                    curs.removeField("tpic");
                                }
                                vals.putAll(curs.toMap());
                                vals.remove("ConfFriends");
                                vals.remove("PendFriends");
                                vals.remove("wallResources");
                                // needed to do this so the coreworkload
                                // will not need to know the datastore typr
                                if (vals.get("_id") != null) {
                                    String tmp = vals.get("_id") + "";
                                    vals.remove("_id");
                                    vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                                }
                                result.add(vals);
                            }
                            cursor.close();
                            // empty list
                            list.clear();
                        }
                    }
                } else if (friendListReq) {// retrive one list
                    List<Integer> list = new ArrayList<Integer>();
                    for (int i = 0; i < friendIds.length; i++) {
                        // put all in one list and retrieve instead of
                        // retrieving one by one
                        list.add(Integer.parseInt(friendIds[i].trim()));
                    }
                    query.put("_id", new BasicDBObject("$in", list));
                    query.removeField("pic");
                    // DBCursor cursor = collection.find(query, fieldsObj);
                    DBCursor cursor = collection.find(query, attrs);

                    int cnt = 0;
                    while (cursor.hasNext()) {
                        cnt++;
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject curs = cursor.next();
                        if (curs.get("tpic") != null) {
                            byte[] myPic = (byte[]) curs.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            curs.removeField("tpic");
                        }

                        vals.putAll(curs.toMap());
                        vals.remove("ConfFriends");
                        vals.remove("PendFriends");
                        vals.remove("wallResurces");
                        // needed to do this so the coreworkload will not
                        // need to know the datastore typr
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        result.add(vals);
                    }
                    cursor.close();
                }
            }
        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int viewFriendReq(int profileOwnerID, Vector<HashMap<String, ByteIterator>> values, boolean insertImage,
        boolean testMode) {
    int retVal = 0;
    if (profileOwnerID < 0)
        return -1;

    // first get all pending friendids for profileOwnerID
    com.mongodb.DB db = null;// w  w  w .  ja  v a  2 s  . c om

    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("users");
        DBObject q = new BasicDBObject().append("_id", profileOwnerID);
        q.removeField("pic");
        BasicDBObject fields = new BasicDBObject("pic", 0);

        DBObject queryResult = null;
        queryResult = collection.findOne(q, fields);

        String x = queryResult.get("PendFriends").toString();
        if (!x.equals("")) {
            x = x.substring(2, x.length() - 1);
            if (!x.equals("")) {
                String friendIds[] = x.split(",");
                BasicDBObject query = new BasicDBObject();

                List<Integer> list = new ArrayList<Integer>();
                if (!friendListReq) {
                    int cnt = 0;
                    for (int i = 0; i < friendIds.length; i++) {
                        cnt++;
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject frnd = new BasicDBObject().append("_id",
                                Integer.parseInt(friendIds[i].trim()));
                        frnd.removeField("pic");
                        DBObject frndQueryResult = null;
                        frndQueryResult = collection.findOne(frnd, fields);
                        if (frndQueryResult.get("tpic") != null) {
                            byte[] myPic = (byte[]) frndQueryResult.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + i + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            frndQueryResult.removeField("tpic");
                        }
                        if (frndQueryResult != null) {
                            frndQueryResult.removeField("ConfFriends");
                            frndQueryResult.removeField("PendFriends");
                            frndQueryResult.removeField("wallResources");
                            vals.putAll(frndQueryResult.toMap());
                        }
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        values.add(vals);
                    }
                } else if (friendListReq) {// retrive one list
                    for (int i = 0; i < friendIds.length; i++) {
                        // put all in one list and retrieve instead of
                        // retrieving one by one
                        list.add(Integer.parseInt(friendIds[i].trim()));
                    }
                    query.put("_id", new BasicDBObject("$in", list));
                    query.removeField("pic");
                    DBCursor cursor = collection.find(query, fields);
                    int cnt = 0;
                    while (cursor.hasNext()) {
                        cnt++;
                        // System.out.println(cursor.next());
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject curs = cursor.next();
                        if (curs.get("tpic") != null) {
                            byte[] myPic = (byte[]) curs.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            curs.removeField("tpic");
                        }
                        vals.putAll(curs.toMap());
                        vals.remove("PendFriends");
                        vals.remove("ConfFriends");
                        vals.remove("wallResources");
                        // needed to do this so the coreworkload will not
                        // need to know the datastore typr
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        values.add(vals);
                    }
                    cursor.close();
                }

            }

        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}

From source file:net.jurre.edutil.persistence.MongoDB.java

License:Open Source License

public void saveEDDNData(String data) {
    BasicDBObject dataObj = (BasicDBObject) JSON.parse(data);

    String schema = dataObj.getString("$schemaRef");
    dataObj.removeField("$schemaRef");
    dataObj.append("schemaRef", schema);

    if (dataObj.getString("schemaRef").equalsIgnoreCase("http://schemas.elite-markets.net/eddn/commodity/2")) {
        BasicDBObject message = (BasicDBObject) dataObj.get("message");
        boolean stationupdated = updateStationMarketData(message.getString("stationName"),
                message.getString("systemName"), message.get("commodities"));
        if (stationupdated)
            dataObj.append("stationUpdated", true);
    }/* www . j  a  v a  2 s. co  m*/

    DBCollection collection = this.db.getCollection(MongoDB.EDDN_COLLECTION);
    collection.insert(dataObj);
}

From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java

License:Apache License

@Override
public void setSurveyParameters(String survey_id, SurveyParameters newParameters) throws DataStoreException {
    DBCollection col = getSurveyStateCollection();

    final BasicDBObject stateObj = new BasicDBObject(FIELD_SURVEY_ID, survey_id);

    switch (newParameters.state) {
    case NOT_INITIALISED:
        stateObj.append(FIELD_SURVEY_STATE, STATE_NOT_INITIALISED);
        break;//from   w w w  . j av a 2s  .co  m
    case SUSPENDED:
        stateObj.append(FIELD_SURVEY_STATE, STATE_SUSPENDED);
        break;
    case ACTIVE:
        stateObj.append(FIELD_SURVEY_STATE, STATE_ACTIVE);
        break;
    }

    stateObj.append(FIELD_START_DATE, newParameters.startDate);
    stateObj.append(FIELD_END_DATE, newParameters.endDate);
    stateObj.append(FIELD_SCHEME_NAME, newParameters.schemeName);
    stateObj.append(FIELD_LOCALE_ID, newParameters.locale);
    stateObj.append(FIELD_ALLOW_GEN_USERS, newParameters.allowGenUsers);
    stateObj.append(FIELD_SUSPENSION_REASON, newParameters.suspensionReason);

    newParameters.surveyMonkeyUrl.accept(new Option.SideEffectVisitor<String>() {
        @Override
        public void visitSome(String item) {
            stateObj.append(FIELD_SURVEY_MONKEY_URL, item);
        }

        @Override
        public void visitNone() {
            if (stateObj.containsField(FIELD_SURVEY_MONKEY_URL))
                stateObj.removeField(FIELD_SURVEY_MONKEY_URL);
        }
    });

    BasicDBObject query = new BasicDBObject(FIELD_SURVEY_ID, survey_id);

    try {
        col.update(query, stateObj, true, false);
    } catch (MongoException e) {
        throw new DataStoreException(e);
    }
}

From source file:org.keycloak.connections.mongo.updater.impl.updates.Update1_7_0.java

License:Apache License

@Override
public void update(KeycloakSession session) throws ClassNotFoundException {
    DBCollection clients = db.getCollection("clients");
    DBCursor clientsCursor = clients.find();

    try {//from   w  w w. j  a  v a 2s.c o  m
        while (clientsCursor.hasNext()) {
            BasicDBObject client = (BasicDBObject) clientsCursor.next();

            boolean directGrantsOnly = client.getBoolean("directGrantsOnly", false);
            client.append("standardFlowEnabled", !directGrantsOnly);
            client.append("implicitFlowEnabled", false);
            client.append("directAccessGrantsEnabled", directGrantsOnly);
            client.removeField("directGrantsOnly");

            clients.save(client);
        }
    } finally {
        clientsCursor.close();
    }
}

From source file:org.kiaan.Main.java

private static void getBuy() {
    try {//from   ww  w .j av a  2s  . c  om
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("kiaan");
        DBCollection coll = db.getCollection("buy");
        //aggregate
        DBObject unwind = new BasicDBObject("$unwind", "$items");
        //$group            
        DBObject group_id = new BasicDBObject("_id", "$_id");
        group_id.put("num", "$num");
        group_id.put("person_id", "$person_id");
        group_id.put("discount", "$discount");
        group_id.put("increase", "$increase");
        //$group -> $multiply
        BasicDBList args = new BasicDBList();
        args.add("$items.value");
        args.add("$items.price");
        DBObject multiply = new BasicDBObject("$multiply", args);
        //$group -> $sum
        //            DBObject group_sum = new BasicDBObject("$sum", multiply);
        DBObject group_field = new BasicDBObject();
        group_field.put("_id", group_id);
        group_field.put("total", new BasicDBObject("$sum", multiply));
        DBObject group = new BasicDBObject("$group", group_field);
        //$project
        DBObject project_field = new BasicDBObject("_id", "$_id._id");
        project_field.put("person_id", "$_id.person_id");
        project_field.put("num", "$_id.num");
        BasicDBList arr = new BasicDBList();
        arr.add("$total");
        arr.add("$_id.discount");
        arr.add("$_id.increase");
        DBObject field_add = new BasicDBObject("$add", arr);
        project_field.put("sum", field_add);
        DBObject project = new BasicDBObject("$project", project_field);
        DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
        List<DBObject> pipeline = Arrays.asList(unwind, group, project, sort);

        //            AggregationOutput output = coll.aggregate(pipeline);
        //            for (DBObject result : output.results()) {
        //                System.out.println(result);
        //            }

        AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
                .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

        BasicDBObject dbo = new BasicDBObject();
        BasicDBList dbl = new BasicDBList();
        Cursor cursor = coll.aggregate(pipeline, aggregationOptions);
        //
        DBCollection person_col = db.getCollection("persons");

        //            BasicDBObject query = new BasicDBObject("items.personId",1);             
        BasicDBObject fields = new BasicDBObject("items.$", 1).append("_id", false);

        //            BasicDBList l_per = (BasicDBList) person_col.findOne(query, fields).get("items");
        //            BasicDBObject[] lightArr = l_per.toArray(new BasicDBObject[0]);            
        //            System.out.println(lightArr[0].get("_id"));
        //            System.out.println(lightArr[0].get("first_name"));  

        BasicDBList result = new BasicDBList();
        while (cursor.hasNext()) {
            dbo = (BasicDBObject) cursor.next();
            //                System.out.println(dbo.toString());  
            DBObject query = new BasicDBObject("items._id", (ObjectId) dbo.get("person_id"));
            BasicDBList lst_person = (BasicDBList) person_col.findOne(query, fields).get("items");
            BasicDBObject[] lightArr = lst_person.toArray(new BasicDBObject[0]);
            //                System.out.println(lightArr[0].get("first_name"));

            Date date = ((ObjectId) lightArr[0].get("_id")).getDate();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            persianCalendar persianCalendar = new persianCalendar(calendar);

            dbo.put("date", persianCalendar.getNumericDateFormatWithTime());
            dbo.put("personId", lightArr[0].get("personId").toString());
            dbo.put("first_name", lightArr[0].get("first_name").toString());
            dbo.put("last_name", lightArr[0].get("last_name").toString());

            dbo.removeField("person_id");
            result.add(dbo);
            //                System.out.println(dbo.get("num"));                  
        }
        System.out.println(result.toString());

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}