【问题标题】:R/Mongolite: How to $unwind a dataframe?R / Mongolite:如何$展开数据框?
【发布时间】: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


【解决方案1】:

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

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

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

应该修改为

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 2017-07-20
    相关资源
    最近更新 更多