Search in sources :

Example 81 with BasicDBList

use of com.mongodb.BasicDBList in project spring-data-document-examples by spring-projects.

the class MvcAnalyticsTest method groupQuery.

@Test
public void groupQuery() {
    // This circumvents exception translation
    DBCollection collection = operations.getCollection("mvc");
    // QueryBuilder qb = new QueryBuilder();
    // qb.start("date").greaterThan(object)
    Calendar startDate = Calendar.getInstance();
    startDate.clear();
    startDate.set(Calendar.YEAR, 2010);
    startDate.set(Calendar.MONTH, 5);
    Calendar endDate = Calendar.getInstance();
    endDate.clear();
    endDate.set(Calendar.YEAR, 2010);
    endDate.set(Calendar.MONTH, 12);
    /*
		 * QueryBuilder qb = new QueryBuilder(); Query q =
		 * qb.find("date").gte(startDate
		 * .getTime()).lt(endDate.getTime()).and("action"
		 * ).is("addFavoriteRestaurant").build(); DBObject cond2 =
		 * q.getQueryObject();
		 */
    DBObject cond = QueryBuilder.start("date").greaterThanEquals(startDate.getTime()).lessThan(endDate.getTime()).and("action").is("addFavoriteRestaurant").get();
    DBObject key = new BasicDBObject("parameters.p1", true);
    /*
		 * DBObject dateQ = new BasicDBObject(); dateQ.put("$gte",
		 * startDate.getTime()); dateQ.put("$lt", endDate.getTime()); DBObject
		 * cond = new BasicDBObject(); cond.put("action",
		 * "addFavoriteRestaurant"); cond.put("date", dateQ);
		 */
    DBObject intitial = new BasicDBObject("count", 0);
    DBObject result = collection.group(key, cond, intitial, "function(doc, out){ out.count++; }");
    if (result instanceof BasicDBList) {
        BasicDBList dbList = (BasicDBList) result;
        for (Object element : dbList) {
            DBObject dbo = (DBObject) element;
            System.out.println(dbo);
        }
    }
    System.out.println(result);
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) Calendar(java.util.Calendar) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 82 with BasicDBList

use of com.mongodb.BasicDBList in project Mycat-Server by MyCATApache.

the class MongoData method setGrouyBy.

public void setGrouyBy(DBObject gb) {
    this.groupby = gb;
    this.type = true;
    if (gb instanceof BasicDBList) {
        Object gb2 = ((BasicDBList) gb).get(0);
        if (gb2 instanceof DBObject) {
            for (String field : ((DBObject) gb2).keySet()) {
                Object val = ((DBObject) gb2).get(field);
                setField(field, getObjectToType(val));
            }
        }
    }
}
Also used : BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) DBObject(com.mongodb.DBObject)

Example 83 with BasicDBList

use of com.mongodb.BasicDBList in project mongomvcc by igd-geo.

the class MongoDBVBranch method makeQueryObject.

private Map<String, Object> makeQueryObject() {
    BasicDBList l = new BasicDBList();
    String lba = MongoDBConstants.LIFETIME + "." + getRootCid();
    String iba = MongoDBConstants.LIFETIME + ".i" + getRootCid();
    // (1) check if there is information about the document's insertion
    // available. if not just include this object.
    // TODO this condition must be removed once we know the whole branching history
    l.add(new BasicDBObject(iba, new BasicDBObject("$exists", false)));
    if (!CompatibilityHelper.supportsAnd(getDB())) {
        // (2) check if the object has been deleted after this commit.
        // we use a $not here, so the query will also return 'true' if
        // the attribute is not set.
        l.add(new BasicDBObject(lba, new BasicDBObject("$not", new BasicDBObject("$lte", getHead()))));
    } else {
        BasicDBList l2 = new BasicDBList();
        // (2) check if the object has been inserted in this commit or later
        l2.add(new BasicDBObject(iba, new BasicDBObject("$lte", getHead())));
        // (3) check if the object has been deleted after this commit.
        // we use a $not here, so the query will also return 'true' if
        // the attribute is not set.
        l2.add(new BasicDBObject(lba, new BasicDBObject("$not", new BasicDBObject("$lte", getHead()))));
        l.add(new BasicDBObject("$and", l2));
    }
    return Collections.unmodifiableMap(new BasicDBObject("$or", l));
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject)

Example 84 with BasicDBList

use of com.mongodb.BasicDBList in project spring-data-mongodb by spring-projects.

the class MappingMongoConverter method removeTypeInfo.

/**
 * Removes the type information from the entire conversion result.
 *
 * @param object
 * @param recursively whether to apply the removal recursively
 * @return
 */
@SuppressWarnings("unchecked")
private Object removeTypeInfo(Object object, boolean recursively) {
    if (!(object instanceof Document)) {
        return object;
    }
    Document document = (Document) object;
    String keyToRemove = null;
    for (String key : document.keySet()) {
        if (recursively) {
            Object value = document.get(key);
            if (value instanceof BasicDBList) {
                for (Object element : (BasicDBList) value) {
                    removeTypeInfo(element, recursively);
                }
            } else if (value instanceof List) {
                for (Object element : (List<Object>) value) {
                    removeTypeInfo(element, recursively);
                }
            } else {
                removeTypeInfo(value, recursively);
            }
        }
        if (getTypeMapper().isTypeKey(key)) {
            keyToRemove = key;
            if (!recursively) {
                break;
            }
        }
    }
    if (keyToRemove != null) {
        document.remove(keyToRemove);
    }
    return document;
}
Also used : BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document)

Example 85 with BasicDBList

use of com.mongodb.BasicDBList in project spring-data-mongodb by spring-projects.

the class DefaultMongoTypeMapper method writeTypeRestrictions.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.convert.MongoTypeMapper#writeTypeRestrictions(java.util.Set)
	 */
@Override
public void writeTypeRestrictions(Document result, @Nullable Set<Class<?>> restrictedTypes) {
    if (ObjectUtils.isEmpty(restrictedTypes)) {
        return;
    }
    BasicDBList restrictedMappedTypes = new BasicDBList();
    for (Class<?> restrictedType : restrictedTypes) {
        Alias typeAlias = getAliasFor(ClassTypeInformation.from(restrictedType));
        if (!ObjectUtils.nullSafeEquals(Alias.NONE, typeAlias) && typeAlias.isPresent()) {
            restrictedMappedTypes.add(typeAlias.getValue());
        }
    }
    accessor.writeTypeTo(result, new Document("$in", restrictedMappedTypes));
}
Also used : BasicDBList(com.mongodb.BasicDBList) Alias(org.springframework.data.mapping.Alias) Document(org.bson.Document)

Aggregations

BasicDBList (com.mongodb.BasicDBList)108 BasicDBObject (com.mongodb.BasicDBObject)69 DBObject (com.mongodb.DBObject)50 Test (org.junit.jupiter.api.Test)17 Document (org.springframework.data.mongodb.core.mapping.Document)14 DBCollection (com.mongodb.DBCollection)11 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)11 DBCursor (com.mongodb.DBCursor)8 DBRef (com.mongodb.DBRef)8 List (java.util.List)7 Document (org.bson.Document)7 HashMap (java.util.HashMap)6 ObjectId (org.bson.types.ObjectId)6 MongoClient (com.mongodb.MongoClient)4 Map (java.util.Map)4 NotFoundException (org.graylog2.database.NotFoundException)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)3 IOException (java.io.IOException)3