文档示例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. linux下crontab命令的使用

    1.crond crond是Linux下的一个守护进程,与windows下的计划任务类似,当操作系统安装完成后,默认会安装并启动进程,Linux下的任务调度分为两类,系统任务调度和用户任务调度. 系统 ...

  2. win7 32 bit VS2012 OpenCV3.0配置

    今天看CPP基础,想起来之前在vs2012配置opencv3未成功,就忍不住再次配置一... 环境:win7 32bit vs2012 opencv3.0 主要参考这几篇博文:1,2,3 上面的博文已 ...

  3. CDH5X 安装oozie报错To enable Oozie web console install the Ext JS library.

    最近在CDH5.X 安装oozie 服务,服务安装完毕,访问oozie server ui,报如下错误: 页面提示: Oozie web console is disabled.To enable O ...

  4. matlab中的数据结构

    一.cell 1. function: num2cell(A,n) n表示如何把A中的数据转换为cell. n=1表示把每列的所有行转换为cell:n=2表示把每行的所有列转换为cell. >& ...

  5. JAVA(3)

    接口注意事项: 1.接口不能被实例化 2.接口中所有的方法都不能有主体  (不能有{ }) 3.一个类可以实现多个接口 4.接口中可以有变量<但变量不能用private和protected修饰& ...

  6. WPF中未将对象引用设置到对象的实例

    前几天,我开始了WPF的基础学习,一上来我就遇到了一个令我头痛的问题,按照书上的例子我写了一段属于自己的代码,一个简单的色调器.满心期待的编译运行,就出现了未将对象引用设置到对象的实例.我在网上查阅了 ...

  7. Linux初学---->WinSCP+Putty

    十二是个初学者,就觉得有意思.所以学下Linux.如果有啥不对的情路过大牛多多指点.心中也开始学着写博客,因为觉得博客对于学习技术不仅是个记录,还是对学的东西一个复习,一个反思,一个交流.另如果有好的 ...

  8. Python 基礎 - 元組與簡易購物車實做

    tuple(元組) 其實跟列表差不多,也是存一組數,只不過是它一旦建立了,就不能修改了,只能做 切片 跟 查詢,所以只叫 只讀列表 語法: name = ("Rogers", &q ...

  9. Linux自己安装redis扩展

    注意:目录的权限   chomd 777 -R 注意:下面涉及的目录只是我自己安装的目录,要找自己对应的目录文件 1.安装redis 下载:https://github.com/nicolasff/p ...

  10. C语言拾遗

    1. 没C++那么恶心的const C语言中的const修饰符用于修饰一个变量是const属性的.被C语言的const修饰的变量具有只读属性,并且不能被修改. const修饰的变量 != 常量,con ...