统计
Aggregate([ops])
Aggregate constructor used for building aggregation pipelines.
参数:
[ops]
参考:
MongoDB驱动器
例子:
new Aggregate();
new Aggregate({ $project: { a: 1, b: 1 } });
new Aggregate({ $project: { a: 1, b: 1 } }, { $skip: 5 });
new Aggregate([{ $project: { a: 1, b: 1 } }, { $skip: 5 }]);
Returned when calling Model.aggregate().
例子:
Model
.aggregate({ $match: { age: { $gte: 21 }}})
.unwind('tags')
.exec(callback)
注意:
- The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
- 需要 MongoDB >= 2.1
function Aggregate () {
this._pipeline = [];
this._model = undefined;
this.options = undefined;
if (1 === arguments.length && util.isArray(arguments[0])) {
this.append.apply(this, arguments[0]);
} else {
this.append.apply(this, arguments);
}
}
Aggregate#allowDiskUse(value, [tags])
为统计查询设置allowDiskUse选项 (忽略 < 2.6.0)
参数:
- value
<Boolean>
告诉服务器统计时可以用硬盘存储数据. - [tags]
<Array>
查询标签,可选
参考:
mongodb
例子:
Model.aggregate(..).allowDiskUse(true).exec(callback)
Aggregate#append(ops)
附加新的操作符到统计管道
参数:
- ops
<Object>
附加的操作符
返回:
<Aggregate>
例子:
aggregate.append({ $project: { field: 1 }}, { $limit: 2 });
// or pass an array
var pipeline = [{ $match: { daw: 'Logic Audio X' }} ];
aggregate.append(pipeline);
Aggregate#cursor(options)
为统计查询设置游标可选 选项 (忽略 < 2.6.0). 注意以下不同的语法: .exec() 返回游标对象且回调函数非必须
参数:
options <Object>
设置游标批量大小
参考:
mongodb
例子:
var cursor = Model.aggregate(..).cursor({ batchSize: 1000 }).exec();
cursor.each(function(error, doc) {
// use doc
});
Aggregate#exec([callback])
在当前绑定模型里执行统计管道
参数:
[callback] <Function>
返回:
<Promise>
参考:
Promise
例子:
aggregate.exec(callback);
// Because a promise is returned, the `callback` is optional.
var promise = aggregate.exec();
promise.then(..);
Aggregate#explain(callback)
附带解析执行统计
参数:
callback <Function>
返回:
<Promise>
例子:
Model.aggregate(..).explain(callback)
Aggregate#group(arg)
附加 $group
操作符到统计管道
参数:
arg <Object>
$group 操作符内容
返回:
<Aggregate>
参考:
$group
例子:
aggregate.group({ _id: "$department" });
Aggregate#limit(num)
附加 $limit 操作符到统计管道
参数:
num <Number>
传给下一步的记录最大数
返回:
<Aggregate>
参考:
$limit
例子:
aggregate.limit(10);
Aggregate#match(arg)
附加 $match
操作符到统计管道
参数:
arg <Object>
$match 操作符内容
返回:
<Aggregate>
参考:
$match
例子:
aggregate.match({ department: { $in: [ "sales", "engineering" } } });
Aggregate#model(model)
绑定统计到模型.
参数:
model <Model>
要绑定统计的模型
返回:
<Aggregate>
Aggregate#near(parameters)
附加 $geoNear
操作符到统计管道
参数:
parameters <Object>
返回:
<Aggregate>
参考:
$geoNear
注意:
必须在管道里用为第一个操作符
例子:
aggregate.near({
near: [40.724, -73.997],
distanceField: "dist.calculated", // required
maxDistance: 0.008,
query: { type: "public" },
includeLocs: "dist.location",
uniqueDocs: true,
num: 5
});
Aggregate#project(arg)
附加 $project
操作符到统计管道
参数:
arg <Object, String>
字段说明
返回:
<Aggregate>
参考:
projection
Mongoose query selection syntax is also supported.
例子:
// include a, include b, exclude _id
aggregate.project("a b -_id");
// or you may use object notation, useful when
// you have keys already prefixed with a "-"
aggregate.project({a: 1, b: 1, _id: 0});
// reshaping documents
aggregate.project({
newField: '$b.nested'
, plusTen: { $add: ['$val', 10]}
, sub: {
name: '$a'
}
})
// etc
aggregate.project({ salary_k: { $divide: [ "$salary", 1000 ] } });
Aggregate#read(pref, [tags])
Sets the readPreference option for the aggregation query.
参数:
pref <String>
one of the listed preference options or their aliases
[tags] <Array>
optional tags for this query
参考:
mongodb driver
例子:
Model.aggregate(..).read('primaryPreferred').exec(callback)
Aggregate#skip(num)
Appends a new $skip
operator to this aggregate pipeline.
参数:
num <Number>
number of records to skip before next stage
返回:
<Aggregate>
参考:
$skip
例子:
aggregate.skip(10);
Aggregate#sort(arg)
Appends a new $sort operator to this aggregate pipeline.
参数:
arg <Object, String>
返回:
<Aggregate>
this
参考:
$sort
If an object is passed, values allowed are asc, desc, ascending, descending, 1, and -1.
If a string is passed, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with - which will be treated as descending.
例子:
// these are equivalent
aggregate.sort({ field: 'asc', test: -1 });
aggregate.sort('field -test');
Aggregate#unwind(fields)
Appends new custom $unwind
operator(s) to this aggregate pipeline.
参数:
fields <String>
the field(s) to unwind
返回:
<Aggregate>
参考:
$unwind
Note that the $unwind operator requires the path name to start with '$'. Mongoose will prepend '$' if the specified field doesn't start '$'.
例子:
aggregate.unwind("tags");
aggregate.unwind("a", "b", "c");