use of com.mongodb.AggregationOutput in project teiid by teiid.
the class TestMongoDBDirectQueryExecution method testDirect.
@Test
public void testDirect() throws Exception {
Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
DBCollection dbCollection = Mockito.mock(DBCollection.class);
DB db = Mockito.mock(DB.class);
Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
Mockito.stub(connection.getDatabase()).toReturn(db);
AggregationOutput output = Mockito.mock(AggregationOutput.class);
Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class), Mockito.any(DBObject.class))).toReturn(output);
Argument arg = new Argument(Direction.IN, null, String.class, null);
arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class));
Argument arg2 = new Argument(Direction.IN, null, String.class, null);
arg2.setArgumentValue(new Literal("foo", String.class));
ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection);
execution.execute();
List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user")));
Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
use of com.mongodb.AggregationOutput in project sissi by KimShen.
the class MongoMucRelationContext method affiliation.
/**
* 获取岗位
*
* @param from
* @param to
* @param def 默认值
* @return
*/
private ItemAffiliation affiliation(JID from, JID to, ItemAffiliation def) {
// {"$match":{"jid":to.bare}},{"$unwind":"$affiliations"},{"$match":{"affiliations.jid":from.bare}},{"$project":{"affiliation":"$affiliations.affiliation"}}
AggregationOutput output = this.config.collection().aggregate(BasicDBObjectBuilder.start("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_JID, to.asStringWithBare()).get()).get(), this.unwindAffiliation, BasicDBObjectBuilder.start("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_AFFILIATIONS + "." + Dictionary.FIELD_JID, from.asStringWithBare()).get()).get(), this.projectAffiliations);
@SuppressWarnings("deprecation") List<?> result = MongoUtils.asList(output.getCommandResult(), Dictionary.FIELD_RESULT);
return result.isEmpty() ? def : ItemAffiliation.parse(MongoUtils.asString(DBObject.class.cast(result.get(0)), Dictionary.FIELD_AFFILIATION).toString());
}
use of com.mongodb.AggregationOutput in project incubator-skywalking by apache.
the class MongoDBCollectionMethodInterceptor method afterMethod.
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
AbstractSpan activeSpan = ContextManager.activeSpan();
CommandResult cresult = null;
if (ret instanceof WriteResult) {
WriteResult wresult = (WriteResult) ret;
cresult = wresult.getCachedLastError();
} else if (ret instanceof AggregationOutput) {
AggregationOutput aresult = (AggregationOutput) ret;
cresult = aresult.getCommandResult();
}
if (null != cresult && !cresult.ok()) {
activeSpan.log(cresult.getException());
}
ContextManager.stopSpan();
return ret;
}
use of com.mongodb.AggregationOutput in project sissi by KimShen.
the class MongoMucRelation4AffiliationContext method myRelations.
/*
* 岗位相符的订阅关系
*
* @see com.sissi.ucenter.relation.muc.MucRelationContext#myRelations(com.sissi.context.JID, java.lang.String)
*/
public Set<Relation> myRelations(JID from, String affiliation) {
// {"$match":{"jid":group.bare}}, {"$unwind":"$affiliations"}, {"$match":{"affiliations.affiliation":Xxx}}, {"$project":{"affiliation":"$affiliations"}}
AggregationOutput output = super.config.collection().aggregate(super.buildMatcher(from), super.unwindAffiliation, BasicDBObjectBuilder.start("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_AFFILIATIONS + "." + Dictionary.FIELD_AFFILIATION, affiliation).get()).get(), this.projectAffiliation);
@SuppressWarnings("deprecation") List<?> result = MongoUtils.asList(output.getCommandResult(), Dictionary.FIELD_RESULT);
return result.isEmpty() ? super.relations : new AffiliationRelations(result);
}
use of com.mongodb.AggregationOutput in project sissi by KimShen.
the class MongoMucRelationContext method mapping.
@Override
public JIDs mapping(JID group) {
// {"$match":{"jid":group.bare}}, {"$unwind":"$roles"}, {"$match":{"roles.nick":Xxx}}, {"$project":{"roles":"$roles"}}, {"$group":{"_id":"$roles.jid","resource":{"$push":"$roles.resource"}}}
AggregationOutput output = this.config.collection().aggregate(this.buildMatcher(group), this.unwindRoles, BasicDBObjectBuilder.start().add("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_ROLES + "." + Dictionary.FIELD_NICK, group.resource()).get()).get(), this.projectRoles, this.groupMapping);
@SuppressWarnings("deprecation") List<?> result = MongoUtils.asList(output.getCommandResult(), Dictionary.FIELD_RESULT);
return result.isEmpty() ? this.jids : this.extract(DBObject.class.cast(result.get(0)));
}
Aggregations