Search in sources :

Example 26 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class CriteriaContainerImpl method addTo.

@Override
public void addTo(final DBObject obj) {
    if (joinMethod == CriteriaJoin.AND) {
        final Set<String> fields = new HashSet<String>();
        int nonNullFieldNames = 0;
        for (final Criteria child : children) {
            if (null != child.getFieldName()) {
                fields.add(child.getFieldName());
                nonNullFieldNames++;
            }
        }
        if (fields.size() < nonNullFieldNames) {
            //use $and
            final BasicDBList and = new BasicDBList();
            for (final Criteria child : children) {
                final BasicDBObject container = new BasicDBObject();
                child.addTo(container);
                and.add(container);
            }
            obj.put("$and", and);
        } else {
            //no dup field names, don't use $and
            for (final Criteria child : children) {
                child.addTo(obj);
            }
        }
    } else if (joinMethod == CriteriaJoin.OR) {
        final BasicDBList or = new BasicDBList();
        for (final Criteria child : children) {
            final BasicDBObject container = new BasicDBObject();
            child.addTo(container);
            or.add(container);
        }
        obj.put("$or", or);
    }
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) HashSet(java.util.HashSet)

Example 27 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class MappedFieldTest method dbList.

private BasicDBList dbList(final String... values) {
    final BasicDBList list = new BasicDBList();
    Collections.addAll(list, values);
    return list;
}
Also used : BasicDBList(com.mongodb.BasicDBList)

Example 28 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class ReferenceTest method testIdOnlyReferences.

@Test
public void testIdOnlyReferences() {
    final List<Ref> refs = asList(new Ref("foo"), new Ref("bar"), new Ref("baz"));
    final Container c = new Container(refs);
    // test that we can save it
    final Key<Container> key = getDs().save(c);
    getDs().save(refs);
    // ensure that we're not using DBRef
    final DBCollection collection = getDs().getCollection(Container.class);
    final DBObject persisted = collection.findOne(key.getId());
    assertNotNull(persisted);
    assertEquals("foo", persisted.get("singleRef"));
    assertEquals("foo", persisted.get("lazySingleRef"));
    final BasicDBList expectedList = new BasicDBList();
    expectedList.add("foo");
    expectedList.add("bar");
    expectedList.add("baz");
    assertEquals(expectedList, persisted.get("collectionRef"));
    assertEquals(expectedList, persisted.get("lazyCollectionRef"));
    final DBObject expectedMap = new BasicDBObject();
    expectedMap.put("0", "foo");
    expectedMap.put("1", "bar");
    expectedMap.put("2", "baz");
    assertEquals(expectedMap, persisted.get("mapRef"));
    assertEquals(expectedMap, persisted.get("lazyMapRef"));
    // ensure that we can retrieve it
    final Container retrieved = getDs().getByKey(Container.class, key);
    assertEquals(refs.get(0), retrieved.getSingleRef());
    if (testDependencyFullFilled()) {
        assertIsProxy(retrieved.getLazySingleRef());
    }
    assertEquals(refs.get(0), unwrap(retrieved.getLazySingleRef()));
    final List<Ref> expectedRefList = new ArrayList<Ref>();
    final Map<Integer, Ref> expectedRefMap = new LinkedHashMap<Integer, Ref>();
    for (int i = 0; i < refs.size(); i++) {
        expectedRefList.add(refs.get(i));
        expectedRefMap.put(i, refs.get(i));
    }
    assertEquals(expectedRefList, retrieved.getCollectionRef());
    assertEquals(expectedRefList, unwrapList(retrieved.getLazyCollectionRef()));
    assertEquals(expectedRefMap, retrieved.getMapRef());
    assertEquals(expectedRefMap, unwrapMap(retrieved.getLazyMapRef()));
}
Also used : ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) LinkedHashMap(java.util.LinkedHashMap) DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 29 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 30 with BasicDBList

use of com.mongodb.BasicDBList in project graylog2-server by Graylog2.

the class LegacyMongoIndexRangeService method findAll.

@Override
public SortedSet<IndexRange> findAll() {
    final BasicDBList subQueries = new BasicDBList();
    subQueries.add(new BasicDBObject(FIELD_INDEX, new BasicDBObject("$exists", true)));
    subQueries.add(new BasicDBObject(FIELD_START, new BasicDBObject("$exists", true)));
    final DBObject query = new BasicDBObject("$and", subQueries);
    final List<DBObject> result = query(query, COLLECTION_NAME);
    final ImmutableSortedSet.Builder<IndexRange> indexRanges = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR);
    for (DBObject dbo : result) {
        try {
            indexRanges.add(buildIndexRange(dbo));
        } catch (Exception e) {
            LOG.debug("Couldn't add index range to result set: " + dbo, e);
        }
    }
    return indexRanges.build();
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) NotFoundException(org.graylog2.database.NotFoundException)

Aggregations

BasicDBList (com.mongodb.BasicDBList)41 BasicDBObject (com.mongodb.BasicDBObject)33 DBObject (com.mongodb.DBObject)26 DBCollection (com.mongodb.DBCollection)8 Test (org.junit.Test)8 DBCursor (com.mongodb.DBCursor)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 NotFoundException (org.graylog2.database.NotFoundException)5 Map (java.util.Map)4 ImmutableList (com.google.common.collect.ImmutableList)3 MongoClient (com.mongodb.MongoClient)3 MongoException (com.mongodb.MongoException)3 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)3 List (java.util.List)3 DataBag (org.apache.pig.data.DataBag)3 Tuple (org.apache.pig.data.Tuple)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 NoSuchInputTypeException (org.graylog2.shared.inputs.NoSuchInputTypeException)3 JSONObject (org.json.JSONObject)3