各位前辈,数据库中有这样一个结构的数据:
现在想按日期来筛选,比如筛选 2019-10-1到2019-10-31 的数据,使用云开发该怎么写呢?我看文档好像都是说的怎么按键值筛选,现在要对key进行筛选该怎么操作呢?
你硬是要那样设计也可以
统计2019 - 10
var _ = db.command,
$ = _.aggregate
db.collection(‘xxx’).aggregate()
.project({
myNewField: $.objectToArray(’$number_available’)
})
.unwind(’$myNewField’)
.match({
‘myNewField.k’: /^2019-10/
})
.group({
_id: $.substr([’$myNewField.k’, 0, 7]),
count: $.sum(’$myNewField.v’)
})
.end().then(res => {
console.log(res)
})
==============
统计2019
db.collection(‘xxx’).aggregate()
.project({
myNewField: $.objectToArray(’$number_available’)
})
.unwind(’$myNewField’)
.match({
‘myNewField.k’: /^2019/
})
.group({
_id: $.substr([’$myNewField.k’, 0, 4]),
count: $.sum(’$myNewField.v’)
})
.end().then(res => {
console.log(res)
})
==============
按年统计
db.collection(‘xxx’).aggregate()
.project({
myNewField: $.objectToArray(’$number_available’)
})
.unwind(’$myNewField’)
.group({
_id: $.substr([’$myNewField.k’, 0, 4]),
count: $.sum(’$myNewField.v’)
})
.end().then(res => {
console.log(res)
})