R / Mongolite:如何$展开数据帧?

问题描述 投票:0回答:1

我正在使用mongolite。我的数据集中有一个数组,我想使用$ 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 aggregate pipeline mongolite
1个回答
0
投票

如评论所述,R的语法不符合Mongo的查询调用,特别是花括号和冒号不同于其他可以在这些位置支持这些符号的语言。

因此,请务必将带有所有必需符号(如方括号)的带引号的字符串中的整个调用包装起来。具体如下:

pUsers <- pUsers$aggregate(pipeline = {"$unwind" : "$profile.hobbies"})

应该修改为

pUsers <- pUsers$aggregate(pipeline = '[{"$unwind" : "$profile.hobbies"}]')
© www.soinside.com 2019 - 2024. All rights reserved.