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