0

鉴于我有以下json:

{
  "A" : {...},

  ".attrs" : {"A1": "1" }
}

我想在 R 中使用 rmongodb 包进行查询。我无法查询 A.attrs 字段值。有什么建议么?

mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
  buf <- mongo.bson.buffer.create()
  mongo.bson.buffer.append(buf, "A.attrs", "1")
  query <- mongo.bson.from.buffer(buf)

  # assume "db.collection" is correct
  cursor <- mongo.find(mongo, "db.collection", query, limit=1000L)
  # Step though the matching records and display them
  while (mongo.cursor.next(cursor))
    print(mongo.cursor.value(cursor))
    mongo.cursor.destroy(cursor)
}

但是,我了解 (.) 在 Mongo 中不是有效的字段名称;它是使用 xml 到 json 转换器生成的。

"\uff0E" 作为转义字符没有帮助。最好将 .attrs 重命名为有效的约定,但在 json 中的不同嵌套级别有多个 .attrs。

4

1 回答 1

2

键中的句号是个问题。如果我们假设它很好,我想你应该像这样构建你的查询:

mongo.bson.buffer.append(buf, ".attrs.A1", "1")

于 2013-09-19T21:12:59.807 回答