【发布时间】:2013-04-18 04:14:04
【问题描述】:
我正在尝试使用聚合框架(使用 ruby)并像这样投影日期:
db['requests'].aggregate([
{"$project" => {
_id: 0,
method: '$method',
user: '$user',
year: {'$year' => '$timestamp'}
}}])
文件是这样的:
{
_id: ObjectId("5177d7d7df26358289da7dfd"),
timestamp: ISODate("2013-04-12T03:58:05+00:00"),
method: "POST",
status: "200",
inputsize: "874",
outputsize: "4981",
user: "131"
}
但我收到以下错误:
Mongo::OperationFailure: Database command 'aggregate' failed: (errmsg: 'exception: can't convert from BSON type EOO to Date'; code: '16006'; ok: '0.0').
这很奇怪,因为如果我在使用 mongorestore 导入的完全相同的数据库上运行它,它可以正常工作。
【问题讨论】:
-
显然传递给 aggregate() 的数组的顺序很重要。如果您有省略某些内容的字段,则需要将其添加到 $match 中,并使 $match 成为数组中的第一个元素。 IE。 > db.user_account.aggregate( [ { $match : { "uts" : { $exists : true }, "chan_key" : "333261c7a72650a95c68d30cd70" } } , { $project : { "period_month": { $month: "$uts " } } }, { $group: { _id : { "period_month" : "$period_month" }, "number" : { $sum : 1 } } } ])
标签: ruby mongodb aggregation-framework database nosql