Search in sources :

Example 46 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 47 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 48 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 49 with BasicDBList

use of com.mongodb.BasicDBList in project GNS by MobilityFirst.

the class MongoRecords method selectRecordsWithin.

private MongoRecordCursor selectRecordsWithin(String collectionName, ColumnField valuesMapField, String key, String value, boolean explain) throws FailedDBOperationException {
    db.requestEnsureConnection();
    DBCollection collection = db.getCollection(collectionName);
    BasicDBList box = parseJSONArrayLocationStringIntoDBList(value);
    String fieldName = valuesMapField.getName() + "." + key;
    BasicDBObject shapeClause = new BasicDBObject("$box", box);
    BasicDBObject withinClause = new BasicDBObject("$geoWithin", shapeClause);
    BasicDBObject query = new BasicDBObject(fieldName, withinClause);
    DBCursor cursor = null;
    try {
        cursor = collection.find(query);
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecordsWithin failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, fieldName, "Original mongo exception:" + e.getMessage());
    }
    if (explain) {
        System.out.println(cursor.explain().toString());
    }
    return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 50 with BasicDBList

use of com.mongodb.BasicDBList in project GNS by MobilityFirst.

the class MongoRecords method selectRecordsNear.

private MongoRecordCursor selectRecordsNear(String collectionName, ColumnField valuesMapField, String key, String value, Double maxDistance, boolean explain) throws FailedDBOperationException {
    db.requestEnsureConnection();
    DBCollection collection = db.getCollection(collectionName);
    double maxDistanceInRadians = maxDistance / METERS_PER_DEGREE;
    BasicDBList tuple = new BasicDBList();
    try {
        JSONArray json = new JSONArray(value);
        tuple.add(json.getDouble(0));
        tuple.add(json.getDouble(1));
    } catch (JSONException e) {
        DatabaseConfig.getLogger().log(Level.SEVERE, "{0} Unable to parse JSON: {1}", new Object[] { dbName, e.getMessage() });
    }
    String fieldName = valuesMapField.getName() + "." + key;
    BasicDBObject nearClause = new BasicDBObject("$near", tuple).append("$maxDistance", maxDistanceInRadians);
    BasicDBObject query = new BasicDBObject(fieldName, nearClause);
    DBCursor cursor = null;
    try {
        cursor = collection.find(query);
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} selectNear failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, fieldName, "Original mongo exception:" + e.getMessage());
    }
    if (explain) {
        System.out.println(cursor.explain().toString());
    }
    return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Aggregations

BasicDBList (com.mongodb.BasicDBList)69 BasicDBObject (com.mongodb.BasicDBObject)40 DBObject (com.mongodb.DBObject)33 Test (org.junit.Test)27 Document (org.springframework.data.mongodb.core.mapping.Document)16 ArrayList (java.util.ArrayList)13 List (java.util.List)9 DBCollection (com.mongodb.DBCollection)8 Document (org.bson.Document)7 DBCursor (com.mongodb.DBCursor)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 DBRef (com.mongodb.DBRef)5 Collection (java.util.Collection)4 NotFoundException (org.graylog2.database.NotFoundException)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 LinkedHashMap (java.util.LinkedHashMap)3