2015年12月30日 星期三

mongodb embedded documents query for spring data


mongodb embedded documents query for spring data





1.Aggreation

db.user.aggregate(
    { $match : {'_id': 1}},
    { $project: {'friendUserList': 1 }},
    { $unwind: "$friendUserList" },
    { $match : {'friendUserList.state': 'INIT'}},
    { $group: { _id: "result", count: { $sum: 1 }}}
);


Aggregation aggregation = newAggregation(
          match(Criteria.where("_id").is(1)),
          project("friendUserList"),
          unwind("friendUserList"),
          match(Criteria.where("friendUserList.state").is("INIT")),
          group("friendUserList").count().as("count")
);
AggregationResults<User> result
    mongoOperations.aggregate(aggregation, "user", User.class);

return (long) result.getMappedResults().size();

0 意見: