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;
}
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()));
}
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);
}
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());
}
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());
}
Aggregations