之前我的mongodb文档如下所示
{
"_id" : ObjectId("5880f010706358aab7a290af"),
"user_id" : "2",
"timezone" : "5.5"
}
我的以下代码工作正常
private void getData(String id, String name) throws Exception {
Aggregation aggregation = newAggregation(
match(Criteria.where("user_id").is(id)),
group("timezone").count().as("total"));
AggregationResults<MongoResultCount> groupResults = mongoTemplate.aggregate(aggregation, "user_log", MongoResultCount.class);
List<MongoResultCount> resultList = groupResults.getMappedResults();
}
private class MongoResultCount {
private String _id;
private long total;
}
由于要求,我的mongo文档现在如下所示
{
"_id" : ObjectId("5880f010706358aab7a290af"),
"user_id" : "2",
"meta_data" : {
"user_data" : {
"timezone" : "5.5",
}
}
}
我如何实现早先取得的成果。我搜索了一个解决方案,但还没找到。
答案 0 :(得分:1)
使用dot notation访问字段。
像
这样的东西Pundit::module_eval do
def policy_scope!(user, scope)
model = scope.is_a?(Array) ? scope.last : scope
PolicyFinder.new(scope).scope!.new(user, model).resolve
end
end