文档示例Account的其中一条记录:
// collection: Account { "_id" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a"), "accountType" : "MEMBER", "money" : NumberLong("10355"), "lockMoney" : NumberLong("0"), "periodMoney" : NumberLong("255"), "payableMoney" : NumberLong("0"), "incomeMoney" : NumberLong("10355"), "cost" : NumberLong("1500"), "costForMonth" : NumberLong("1500"), "version" : NumberLong("31"), "periodStartTime" : NumberLong("1480867200000"), "periodEndTime" : NumberLong("1480953599000"), "accountRecords" : [ { "ID" : ObjectId("5843e3e4535f3709061c2141"), "serial" : ObjectId("5843e3d1535f370902e8dc99"), "title" : "测试:年跹858啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "PAIDITEMBYNETBANK", "statementsMessageId" : ObjectId("5843e3e4535f3709061c213f"), "orderTime" : NumberLong("1480844241849"), "Money" : NumberLong("800"), "beforeMoney" : NumberLong("1825"), "afterMoney" : NumberLong("1825"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("5843e736535f37092a236725"), "serial" : ObjectId("5843e6a9535f370927198973"), "title" : "测试:阳颞愆221啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "PAIDITEMBYNETBANK", "statementsMessageId" : ObjectId("5843e736535f37092a236723"), "orderTime" : NumberLong("1480844969902"), "Money" : NumberLong("300"), "beforeMoney" : NumberLong("4480"), "afterMoney" : NumberLong("4480"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("5843e952535f3709393318c3"), "serial" : ObjectId("5843e78a535f3709321bce9d"), "title" : "测试:申铑乔888啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "PAIDITEMBYNETBANK", "statementsMessageId" : ObjectId("5843e952535f3709393318c1"), "orderTime" : NumberLong("1480845194664"), "Money" : NumberLong("400"), "beforeMoney" : NumberLong("7565"), "afterMoney" : NumberLong("7565"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("5843ee2a535f370a49c827d2"), "serial" : ObjectId("5843ee0d535f370a469c2dde"), "title" : "测试:燕抒奎85啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "SELL", "statementsMessageId" : ObjectId("5843ee2a535f370a49c827cd"), "orderTime" : NumberLong("1480846861000"), "Money" : NumberLong("255"), "beforeMoney" : NumberLong("8040"), "afterMoney" : NumberLong("8295"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("5843ee2c535f370a49c82880"), "serial" : ObjectId("5843ee0c535f370a469c2d66"), "title" : "测试:简鼓禚127啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "SELL", "statementsMessageId" : ObjectId("5843ee2c535f370a49c8287b"), "orderTime" : NumberLong("1480846860000"), "Money" : NumberLong("475"), "beforeMoney" : NumberLong("8295"), "afterMoney" : NumberLong("8770"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("5843ee2c535f370a49c828b2"), "serial" : ObjectId("5843ee0c535f370a469c2d48"), "title" : "测试:厍假913啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "SELL", "statementsMessageId" : ObjectId("5843ee2c535f370a49c828ad"), "orderTime" : NumberLong("1480846860000"), "Money" : NumberLong("570"), "beforeMoney" : NumberLong("8770"), "afterMoney" : NumberLong("9340"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("5843ee2d535f370a49c82934"), "serial" : ObjectId("5843ee0b535f370a469c2ce8"), "title" : "测试:赖象645啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "SELL", "statementsMessageId" : ObjectId("5843ee2d535f370a49c8292f"), "orderTime" : NumberLong("1480846859000"), "Money" : NumberLong("475"), "beforeMoney" : NumberLong("9340"), "afterMoney" : NumberLong("9815"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("5843ee2e535f370a49c82952"), "serial" : ObjectId("5843ee0b535f370a469c2cd6"), "title" : "测试:胡滴贻967啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "SELL", "statementsMessageId" : ObjectId("5843ee2e535f370a49c8294d"), "orderTime" : NumberLong("1480846859000"), "Money" : NumberLong("285"), "beforeMoney" : NumberLong("9815"), "afterMoney" : NumberLong("10100"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") }, { "ID" : ObjectId("584542a23da1270a62424b8c"), "serial" : ObjectId("584542293da1270a5b4a5640"), "title" : "测试:燕抒奎85啥地方记录卡时间是东风科技为额外认为二位水电费是打发是否是的发生大发上课IE2", "opertionType" : "SELL", "statementsMessageId" : ObjectId("584542a23da1270a62424b87"), "orderTime" : NumberLong("1480933929000"), "Money" : NumberLong("255"), "beforeMoney" : NumberLong("10100"), "afterMoney" : NumberLong("10355"), "accountId" : ObjectId("5843e38e535f3708f759b25b"), "memberId" : ObjectId("5843e38e535f3708f759b25a") } ], "costMonthFistTimel" : NumberLong("1480521600000") }
要查询指定Account账号,并且accountRecords.opertionType="SELL",并且需要按id倒叙,需要分页
mongodb查询语句
db.getCollection("f.publicAccount").aggregate({"$match" : { "memberId" :ObjectId("5843e38e535f3708f759b25a")}}) //.project("gender _id") .unwind("$accountRecords") .match({"accountRecords.opertionType" : "SELL"}) .sort({"accountRecords.ID":-1}) .skip(0).limit(3) .group({ _id:"$_id","accountRecords" : { "$push" : "$accountRecords"}}) ;
java morphia代码:
//指定会员 Query<Accounts> queryMember = this.createQuery().field("memberId").equal(memberId); Query<Accounts> queryOpertion=queryOpertion =this.createQuery().field("accountRecords.opertionType").in(getIncomeType()); AggregationPipeline pipeline =this.getDs().createAggregation(Accounts.class); pipeline.match(queryMember); pipeline.unwind("accountRecords"); //按照入账记录Id排序 pipeline.match(queryOpertion).sort(Sort.descending("accountRecords.ID")).limit(pager.getPageSize()).skip(pager.getSkip()); Accumulator pushFied = Group.push("accountRecords"); pipeline.group("_id",Group.grouping("accountRecords",pushFied)); Iterator<Accounts> iterator = pipeline.aggregate(Accounts.class); if (iterator.hasNext()) { Accounts account = iterator.next();