我有一个运行良好的 MongoDB shell 命令。命令是:
db.sessions.aggregate({$unwind: "$Sessions List"},
{$match: { "Sessions List.Parent Session":"My Session"}},
{$group:{_id:"$Sessions List.Parent Session", avg_val:{$avg:"$Sessions List.Length"}}})
结果是:“结果”:
[{
"_id" : "My Session",
"avg_val" : 8.846666666666666
}],
"ok" : 1
但是当尝试通过 Mongoid 运行它(使用最新的可用 Mongoid、Origin、Moped)时,它会因 NoMethodError (undefined method
__ bson_load__' for nil:NilClass)` 异常而失败。
Ruby代码如下:
Session.collection.aggregate({
'$unwind' => '$Sessions List',
'$match' => { 'Sessions List.Parent Session' => 'My Session'},
'$group' => {
'_id' => '$Sessions List.Parent Session',
'avg_val' => {
'$avg' => '$Sessions List.Length'
}}});