文档示例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();

  

mongodb 对内嵌文档(数组) group分页查询,并设置查询条件的更多相关文章

  1. mongodb内嵌文档的javaapi,增删改查

    数据结构: {"_id" : "000000001",  //Mongodb默认主键 "UID" : "000000001&quo ...

  2. MongoDB 内嵌文档

    MongoDB是文档型的数据库系统,doc是MongoDB的数据单位,每个doc相当于关系型数据库的数据行(row),doc和row的区别在于field的原子性:row中的column是不和分割的原子 ...

  3. Mongo对内嵌文档的CRUD

    { "_id" : ObjectId("5706032acd0a6194868cf53e"), "list" : { "age&q ...

  4. mongodb内嵌文档的查询

    本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...

  5. MongoDB内嵌文档操作

    实体定义: [BsonIgnoreExtraElements] public class Person : BaseEntity { public string FirstName { get; se ...

  6. C# mongodb中内嵌文档数组条件查询

    样例数据: {      "_id" : "1064621564857",      "cNo" : "1064621564857 ...

  7. 学习MongoDB 五: MongoDB查询(数组、内嵌文档)(二)

    一.简介 我们上一篇介绍了db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段,我们今天介绍了对数组和内嵌文档的查询操作,尤其是 ...

  8. MongoDB-4: 查询(二-数组、内嵌文档)

    一.简介 我们上一篇介绍了db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段,我们今天介绍了对数组和内嵌文档的查询操作,尤其是 ...

  9. mongodb对数组元素及内嵌文档进行增删改查操作(转)

    from:https://my.oschina.net/132722/blog/168274 比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: &l ...

随机推荐

  1. 从C++转向Java的第一课

    序:计算机语言的学习,最快捷的方法是从一种语言映射到另一种语言.Java和C++作为最通用的面相对象高级语言,不论从语法或者编程上都极具相似性和可效仿行,这里的可效仿包括——语法理解可效仿和编程规范可 ...

  2. js zTree的用法

    代码如下: <script type="text/javascript">    var reginTree = {    setting: {        view ...

  3. Varnish介绍

    “Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang (http://www.vg.no) 使用3台Varnish代替了原来的12台squid,性能居然比以前 ...

  4. <转>你相信外星人的存在吗

    ​ 来自为知笔记(Wiz)

  5. CSS各种定位详解

    1.定位的专业解释 (1)语法 position:static|absolute|fixed|relative (2)说明 从上面语法可以看出,定位的方法有很多种,它们分别是静态(static),绝对 ...

  6. sql server 2008中清除数据库日志的sql语句

    第一步: Use  数据库名 Select NAME,size From sys.database_files 将“数据库名”改为需要清除日志的数据库名字,点击“执行”查询出需要清除的日志名称:**_ ...

  7. 联合(union)类型有哪些使用场景

    2013-08-22 - 18:17:50  by Gold_Lion 今天突然碰到“联合”这个数据类型了,平时用的不多,实在也想不出来它到底能帮我干嘛?于是google了一下,终于有一个是利用联合( ...

  8. LaTeX_fleqn参数时,多行公式对齐居中的同时选择性的加编号

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/10/20 一年多没写博文了.今天写一个短的,记录一下使用LaTeX的一些经验. 有些时候,我们的latex文 ...

  9. 用ajax提交form表单及乱码问题

    要求 1. form里是习题和选择的答案包含内容较多,直接用ajax获取页面元素较多,麻烦. 2. 也不能直接用form提交,form提交后会跳转页面,如果出错想在本页面获取错误信息(ajax提交)且 ...

  10. Nginx转发地址解决跨域问题

    什么是跨域问题 在一个服务器A里放置了json文件,另一个服务器B想向A发送ajax请求,获取此文件,会发生错误. Chrome提示: XMLHttpRequest cannot load ***** ...