mongodb 对内嵌文档(数组) group分页查询,并设置查询条件
文档示例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分页查询,并设置查询条件的更多相关文章
- mongodb内嵌文档的javaapi,增删改查
数据结构: {"_id" : "000000001", //Mongodb默认主键 "UID" : "000000001&quo ...
- MongoDB 内嵌文档
MongoDB是文档型的数据库系统,doc是MongoDB的数据单位,每个doc相当于关系型数据库的数据行(row),doc和row的区别在于field的原子性:row中的column是不和分割的原子 ...
- Mongo对内嵌文档的CRUD
{ "_id" : ObjectId("5706032acd0a6194868cf53e"), "list" : { "age&q ...
- mongodb内嵌文档的查询
本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...
- MongoDB内嵌文档操作
实体定义: [BsonIgnoreExtraElements] public class Person : BaseEntity { public string FirstName { get; se ...
- C# mongodb中内嵌文档数组条件查询
样例数据: { "_id" : "1064621564857", "cNo" : "1064621564857 ...
- 学习MongoDB 五: MongoDB查询(数组、内嵌文档)(二)
一.简介 我们上一篇介绍了db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段,我们今天介绍了对数组和内嵌文档的查询操作,尤其是 ...
- MongoDB-4: 查询(二-数组、内嵌文档)
一.简介 我们上一篇介绍了db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段,我们今天介绍了对数组和内嵌文档的查询操作,尤其是 ...
- mongodb对数组元素及内嵌文档进行增删改查操作(转)
from:https://my.oschina.net/132722/blog/168274 比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: &l ...
随机推荐
- Ubuntu 14.10 创建虚拟网卡实现桥接网络
目标需求:在Ubuntu主机创建一张虚拟网卡,使得搭建在VirtualBox中的其他主机能借助虚拟网卡构建的局域网进行相互间的通讯 步骤一:准备工具包 安装 uml-utilities 以及 brid ...
- xlwt写入中文操作不成功,提示UnicodeDecodeError: ascii codec can't decode byte ...
打开xlwt包里的Workbook.py文件,修改Workbook类的__init__方法 将 def __init__(self, encoding='ascii', style_compressi ...
- handlebars,each循环里面套each循环
handlebars可以用each自动进行循环,下面介绍一下each循环里面套循环来着. html代码 !DOCTYPE html> <html> <head> < ...
- 使用Spring 发送 Email !!!!
邮件服务器: 邮件服务器提供了邮件系统的基本结构,包括邮件传输,邮件分发,邮件存储等功能,以确保邮件能够发送到互联网中的任意地方. 邮件服务器就像我们生活中的邮局,为人们提供邮件服务.要保证邮件被正确 ...
- ThinkPHP 自动验证相关注意
1.假如加入了表单令牌的话,表单的各种名与对应Model字段一致,不然报坑爹的令牌错误. 2.假如加入了表单令牌的话,Create只能采用默认的POST数据创建数据对象,不然又是坑爹的令牌错误. 3. ...
- Excel中如何过滤复选框 How to filter checkbox column in Excel
有个笨笨的小伙伴最近老跟Excel过不去. 她新加了一列checkbox,还想filter别人有没有选.写代码又不会,怎么办. 1. 首先,加这个checkbox 2. 然后新建一列,把这个check ...
- hash连接
简单回顾嵌套循环: 两个表关联,较小的表(指使用了过滤条件后结果集较小的表)称为驱动表或者外表(,另一个称为内表.在嵌套连接过程中,oracle首先读取驱动表的第一条数据,然后和内表进行比对,所以匹配 ...
- SILVERLIGHT 应急卫生模拟演练项目之loading界面实现
第一次在博客园写文章 俺是菜鸟 有不足之处还请大佬们多多指教 第一次也不知道该写啥 俺就拿自己最近做的一个项目 来细说吧 俺们公司是做医疗卫生方面的 其中有一块涉及到应急卫生模拟演练方面 这块分到我 ...
- IOS OC 多任务定时器 NSRunLoop 管理 NSTimer
下面有两种做法 1.使用日期组件 NSDateComponents 2.使用NSString 生成一个日期 // 创建一个日历对象 NSCalendar *calendar = [NSCalenda ...
- PHP生成唯一会员卡号
我们将0-Z(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ)分别代表数值0-35,如字母Z代表35.这样的话我要得到一个5位的编号,最大信息量就是36的5次方了,36^5 ...