Example usage for com.mongodb BasicDBObject get

List of usage examples for com.mongodb BasicDBObject get

Introduction

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

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value from this object

Usage

From source file:nl.vu.psy.relic.persistence.mongo.MongoMapper.java

License:Open Source License

public static ResolverDescriptor DBObjectToResolverDescriptor(DBObject dbo) {
    DBObject innerFields = (DBObject) dbo.get("resolver");
    if (innerFields == null || "".equals(innerFields)) {
        return null;
    } else {//from  ww  w . ja va  2s . com
        String identifier = (String) innerFields.get("identifier");
        String environment = (String) innerFields.get("environment");
        ResolverDescriptor result = new ResolverDescriptor(identifier, environment);
        BasicDBList properties = (BasicDBList) innerFields.get("properties");
        for (Object property : properties) {
            BasicDBObject prop = ((BasicDBObject) property);
            // Skip elements that are not simple key-value pairs
            if (prop.keySet().size() == 1) {
                for (String s2 : prop.keySet()) {
                    result.setProperty(s2, (String) prop.get(s2));
                }
            }
        }
        return result;
    }
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * //  w  w w .j  a  va  2 s  .  co m
 * @param parachutistsFromMelwin
 */
public ArrayList<Parachutist> getParachutistsFromDB() {
    // HjelpeLister
    ArrayList<Club> clubs = getClubsFromDb();
    ArrayList<License> licenses = getLicensesFromDb();

    ArrayList<Parachutist> parachutistsInMongoDB = new ArrayList<>();

    DBCollection dbCollectionParachutists = db.getCollection("jumpers");

    DBCursor cursor = dbCollectionParachutists.find();

    BasicDBObject mongoObject = new BasicDBObject();

    try {
        int hoppteller = 0;
        while (cursor.hasNext()) {
            mongoObject = (BasicDBObject) cursor.next();
            hoppteller++;
            ArrayList<Club> memberClubs = new ArrayList<>();
            ArrayList<License> memberLicenses = new ArrayList<>();

            BasicDBList referenceClubs = (BasicDBList) mongoObject.get("memberclubs");
            BasicDBList referenceLicenses = (BasicDBList) mongoObject.get("licenses");

            for (Object clubReference : referenceClubs) {
                for (Club club : clubs) {
                    if ((int) clubReference == club.getId()) {
                        memberClubs.add(club);
                    }
                }
            }

            for (Object licenseReference : referenceLicenses) {
                for (License license : licenses) {
                    if ((int) licenseReference == license.getId()) {
                        memberLicenses.add(license);
                    }
                }

            }

            Parachutist parachutist = new Parachutist(mongoObject.getObjectId("_id"), mongoObject.getInt("id"),
                    mongoObject.getString("melwinId"), null, // nakKey
                    new ArrayList<Club>(), // clubs
                    new ArrayList<License>(), // licenses
                    mongoObject.getString("firstname"), mongoObject.getString("lastname"),
                    mongoObject.getDate("birthdate"), mongoObject.getString("gender"),
                    mongoObject.getString("street"), mongoObject.getString("postnumber"),
                    mongoObject.getString("postplace"), mongoObject.getString("mail"),
                    mongoObject.getString("phone"), mongoObject.getString("password"));

            parachutist.setMemberclubs(memberClubs);
            parachutist.setLicenses(memberLicenses);

            parachutistsInMongoDB.add(parachutist);

        }
    } finally {
        cursor.close();
    }
    return parachutistsInMongoDB;
}

From source file:org.apache.chemistry.opencmis.mongodb.MongodbUtils.java

License:Apache License

public void moveNode(BasicDBObject node, BasicDBObject newParent, DBCollection collection) {

    // Get the left and right values
    Long originalLeft = node.getLong("left");
    Long originalRight = node.getLong("right");
    Long subtreeWidth = originalRight - originalLeft;

    // Compute the new left and right values for the nodeToMove
    Long newLeft = newParent.getLong("right");
    Long newRight = newParent.getLong("right") + subtreeWidth;

    // Make space for the new subtree under the new parent
    collection.update(/*  ww w  .  ja v a 2s .c  o  m*/
            new BasicDBObject().append("right", new BasicDBObject().append("$gte", newParent.get("right"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("right", subtreeWidth + 1)), false,
            true);

    collection.update(
            new BasicDBObject().append("left", new BasicDBObject().append("$gte", newParent.get("right"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("left", subtreeWidth + 1)), false,
            true);

    // Re-fetch the node to move, since the left and right values may have changed
    node = (BasicDBObject) collection.findOne(new BasicDBObject().append("_id", node.get("_id")));

    Long difference = node.getLong("left") - newLeft;
    // Move the old subtree into a new location
    collection.update(
            new BasicDBObject().append("left", new BasicDBObject().append("$gte", node.getLong("left")))
                    .append("right", new BasicDBObject().append("$lte", node.getLong("right"))),
            new BasicDBObject().append("$inc",
                    new BasicDBObject().append("left", 0 - difference).append("right", 0 - difference)),
            false, true);

    // Remove empty space from the parent
    //db.test.update({left:nodeToMove.left-1, right:nodeToMove.right+1}, {right:nodeToMove.left});
    collection.update(new BasicDBObject().append("right", new BasicDBObject().append("$gte", node.get("left"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("right", 0 - subtreeWidth - 1)),
            false, true);
    collection.update(new BasicDBObject().append("left", new BasicDBObject().append("$gte", node.get("left"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("left", 0 - subtreeWidth - 1)), false,
            true);
}

From source file:org.apache.flink.Job.java

License:Apache License

public static DataSet<Point> convertToPointSet(DataSet<Tuple2<BSONWritable, BSONWritable>> in) {
    return in.map(new MapFunction<Tuple2<BSONWritable, BSONWritable>, Point>() {
        @Override//from  w  ww  .java 2s.c o  m
        public Point map(Tuple2<BSONWritable, BSONWritable> bsonWritableBSONWritableTuple2) throws Exception {
            BSONWritable bvalue = bsonWritableBSONWritableTuple2.getField(1);
            Object value = bvalue.getDoc();
            BasicDBObject point = (BasicDBObject) value;
            String id = point.getObjectId("_id").toString();
            Double x = (Double) point.get("x");
            Double y = (Double) point.get("y");
            //System.out.println("Point: " + id + " : " + x.toString() + " " + y.toString());
            return new Point(x, y);
        }
    });
}

From source file:org.apache.flink.Job.java

License:Apache License

public static DataSet<Centroid> convertToCentroidSet(DataSet<Tuple2<BSONWritable, BSONWritable>> in) {
    return in.map(new MapFunction<Tuple2<BSONWritable, BSONWritable>, Centroid>() {
        @Override/* w  w w  .  j  a  v a  2  s. co  m*/
        public Centroid map(Tuple2<BSONWritable, BSONWritable> bsonWritableBSONWritableTuple2)
                throws Exception {
            BSONWritable bvalue = bsonWritableBSONWritableTuple2.getField(1);
            Object value = bvalue.getDoc();
            BasicDBObject centroid = (BasicDBObject) value;
            Integer id = (Integer) centroid.get("_id");
            Double x = (Double) centroid.get("x");
            Double y = (Double) centroid.get("y");
            //System.out.println("Centroid: " + id.toString() + " : " + x.toString() + " " + y.toString());
            return new Centroid(id, x, y);
        }
    });
}

From source file:org.apache.gora.mongodb.utils.BSONDecorator.java

License:Apache License

/**
 * Access field as an object, no casting.
 *
 * @param fieldName//from   ww w .  j  av a 2 s . c  o m
 *          fully qualified name of the field to be accessed
 * @return value of the field
 */
public Object get(String fieldName) {
    BasicDBObject parent = getFieldParent(fieldName);
    return parent.get(getLeafName(fieldName));
}

From source file:org.apache.gora.mongodb.utils.BSONDecorator.java

License:Apache License

/**
 * Retrieve the parent of a field.//from  www . j av  a  2s . c o m
 *
 * @param fieldName
 *          fully qualified name of the field
 * @param createIfMissing
 *          create the intermediate fields if necessary
 * @return the parent of the field
 * @throws IllegalAccessError
 *           if the field does not exist
 */
private BasicDBObject getFieldParent(String fieldName, boolean createIfMissing) {
    String[] fields = fieldName.split("\\.");
    int i = 0;
    BasicDBObject intermediate = (BasicDBObject) myBson;

    // Set intermediate parents
    while (i < (fields.length - 1)) {
        if (!intermediate.containsField(fields[i]))
            if (createIfMissing)
                intermediate.put(fields[i], new BasicDBObject());
            else
                throw new IllegalAccessError(
                        "The field '" + fieldName + "' does not exist: '" + fields[i] + "' is missing.");
        intermediate = (BasicDBObject) intermediate.get(fields[i]);
        i++;
    }

    return intermediate;
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.replica.ReplicaSetInfo.java

License:Apache License

void updateReplicaStatus() {
    BasicDBObject result;
    try {//from w  w  w.j  a v  a2  s .co  m
        result = getReplicaStatus();
    } catch (MongoException e) {
        LOG.error("Can't get replica status", e);
        rootRevisions = null;
        secondariesSafeTimestamp = 0;
        return;
    }

    @SuppressWarnings("unchecked")
    Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
    if (members == null) {
        members = Collections.emptyList();
    }
    updateRevisions(members);
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.replica.ReplicaSetInfo.java

License:Apache License

List<String> getHiddenMembers() {
    BasicDBObject result;
    try {/*w w w  .  j  a  v  a2 s.co  m*/
        result = getReplicaConfig();
    } catch (MongoException e) {
        LOG.error("Can't get replica configuration", e);
        return null;
    }

    @SuppressWarnings("unchecked")
    Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
    if (members == null) {
        members = Collections.emptyList();
    }

    List<String> hiddenMembers = new ArrayList<String>();
    for (BasicBSONObject member : members) {
        if (member.getBoolean("hidden")) {
            hiddenMembers.add(member.getString("host"));
        }
    }
    return hiddenMembers;
}

From source file:org.apache.metamodel.mongodb.mongo2.MongoDbDataContext.java

License:Apache License

private void convertToCursorObject(BasicDBObject query, FilterItem item) {
    if (item.isCompoundFilter()) {

        BasicDBList orList = new BasicDBList();

        final FilterItem[] childItems = item.getChildItems();
        for (FilterItem childItem : childItems) {
            BasicDBObject childObject = new BasicDBObject();
            convertToCursorObject(childObject, childItem);
            orList.add(childObject);// ww  w  .j  ava  2 s.  c om
        }

        query.put("$or", orList);

    } else {

        final Column column = item.getSelectItem().getColumn();
        final String columnName = column.getName();
        final String operatorName = getOperatorName(item);

        Object operand = item.getOperand();
        if (ObjectId.isValid(String.valueOf(operand))) {
            operand = new ObjectId(String.valueOf(operand));
        }

        final BasicDBObject existingFilterObject = (BasicDBObject) query.get(columnName);
        if (existingFilterObject == null) {
            if (operatorName == null) {
                if (OperatorType.LIKE.equals(item.getOperator())) {
                    query.put(columnName, turnOperandIntoRegExp(operand));
                } else {
                    query.put(columnName, operand);
                }
            } else {
                query.put(columnName, new BasicDBObject(operatorName, operand));
            }
        } else {
            if (operatorName == null) {
                throw new IllegalStateException(
                        "Cannot retrieve records for a column with two EQUALS_TO operators");
            } else {
                existingFilterObject.append(operatorName, operand);
            }
        }
    }
}