【发布时间】:2018-09-04 14:55:51
【问题描述】:
我正在使用蒙古石。我的数据集中有一个数组,我想使用 $unwind 解构它。我做了以下事情:
pUsers <- pUsers$aggregate(pipeline = {"$unwind" : "$profile.hobbies"})
结果:
Error in "$unwind":"$profile.hobbies" : NA/NaN argument
In addition: Warning messages:
1: In inherits(x, "bson") : NAs introduced by coercion
2: In inherits(x, "bson") : NAs introduced by coercion
看到错误信息,我尝试排除具有 NA 值的数据,代码如下:
pUsers <- pUsers$aggregate(pipeline = '[
{"$match" : {"$profile.hobbies" : {"$exists" : true}}},
{"$unwind" : "$profile.hobbies"}]')
结果:
Error: unknown top level operator: $profile.hobbies
谁能解释我犯的错误?此外,我怎样才能正确展开我的数据框? 谢谢!
【问题讨论】:
-
尝试引用整个表达式,因为 R 在其语法中不使用花括号和冒号:
pUsers$aggregate(pipeline = '{"$unwind" : "$profile.hobbies"}') -
嗨!我试过把它写成一个字符串(你在这里说的),但结果是这样的:
bson_append_array(): invalid array detected. first element of array parameter is not "0". Error: Each element of the 'pipeline' array must be an object -
尝试将调用放在括号中:
pUsers$aggregate(pipeline = '[{"$unwind" : "$profile.hobbies"}]')as docs show。 -
成功了!谢谢!
标签: r aggregate pipeline mongolite