Mongo 开发笔记

时间

程序的时间是本地时间 ,数据库中的时间是 ISO 标准时间 。 ISO时间 + 8 小时 = 本地时间(北京时间 )

Java驱动会自动做转化。

语法

数组查询

  • 数据查询使用 elemMatch

删除指定位置的数组值.

目前还没找到方法 . 类似的 js: splice (1 , 1)

https://stackoverflow.com/questions/4588303/in-mongodb-how-do-you-remove-an-array-element-by-its-index

分组

qrCode有details集合。 按details 第一条的 tag 分组计数。

db.qRCodeInfo.aggregate(
[ 
  { $project: { "_id":0, "de" : { "$slice":[ "$details.tag", 0,1 ] } } } 
  ,{ $unwind : "$de"} 
  ,{ $group : { "_id": "$de" , count: { $sum: 1 } } }
]
)

firstElementAt

如果表达式里有数组,那么会返回完整的数组。

如果表达式中有数组,再取数组下面的属性,则会返回下面属性的数组。

如果表达式里没有数组,则会返回属性值。

db.qRCodeInfo.aggregate(
[
{ $match: { "productId": "58ff0f5b2b3fe455a3e2944d" , } }
, {
$project : { 
abc: { $slice: [ "$corpRecords.corp", 2 ] } ,
createAt: { $arrayElemAt: [ "$corpRecords.userRecords.createAt" , 1] } 
}} 
] 
)

aggregate 中使用 addFields

解决 project 不足, 可以方便地添加字段. 减少字段 设置 filed: -1 即可.

问题

如何 aggregate 查询团长订单的子订单数

posted @ 2017-08-09 16:04  NewSea  阅读(219)  评论(0编辑  收藏  举报