babalu
babalu

Reputation: 622

MongoDB query using .attrs attribute

Given I have the following json:

{
  "A" : {...},

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

I'd like to query using rmongodb package in R. I'm unable to query A.attrs field values. Any suggestions?

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)
}

I understand that (.) is not a valid field name in Mongo, however; it was generated using an xml to json converter.

"\uff0E" as escape character didn't help. It's probably best to rename .attrs to a valid convention, but there are several .attrs at various nested levels in json.

Upvotes: 0

Views: 298

Answers (1)

Ali Cirik
Ali Cirik

Reputation: 1572

The period in the key is a problem. If we assume it is good, I guess you should construct your query like:

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

Upvotes: 2

Related Questions