Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm still not very clear on what's the preferred way here on SO to progress once a question has been posted but one wishes to elaborate a bit more, possibly adding further questions and answer approaches.</p> <p>As I was often told not to blow up my original question with future edits, in this "answer" I'm simply taking the suggestions by Gerald Lindsly and try to put it into actual code (because it still didn't work out for me):</p> <h2>Preparations</h2> <pre><code>pkg &lt;- "rmongodb" if (!require(pkg, character.only=TRUE)) { install.packages(pkg) require(pkg, character.only=TRUE) } # Connect to DB db &lt;- "test" ns &lt;- "posts" mongo &lt;- mongo.create(db=db) # Make sure we start with an empty collection mongo.drop(mongo, paste(db, ns, sep=".")) </code></pre> <h2>Insert document</h2> <p>As Gerald has pointed out in his answer, <code>mongo.bson.from.list()</code> sometimes makes wrong guesses about the resulting BSON structure, so I tried to go ahead an explicitly create BSON array objects:</p> <pre><code>buf &lt;- mongo.bson.buffer.create() # 'REGULAR' APPENDING mongo.bson.buffer.append(buf, "_id", "abcd") mongo.bson.buffer.append(buf, "when", mongo.timestamp.create(strptime("2011-09-19 02:00:00", "%Y-%m-%d %H:%M:%s"), increment=1)) mongo.bson.buffer.append(buf, "author", "alex") mongo.bson.buffer.append(buf, "title", "Some title") mongo.bson.buffer.append(buf, "text", "Some text.") mongo.bson.buffer.append(buf, "tags", c("tag.1", "tag.2")) mongo.bson.buffer.append(buf, "votes", 5) # / # VOTERS ARRAY mongo.bson.buffer.start.array(buf, "voters") voters &lt;- c("jane", "joe", "spencer", "phyllis", "li") i=1 for (i in seq(along=voters)) { mongo.bson.buffer.append(buf, as.character(i), voters[i]) } mongo.bson.buffer.finish.object(buf) # / # COMMENTS ARRAY mongo.bson.buffer.start.array(buf, "comments") mongo.bson.buffer.start.object(buf, "1") mongo.bson.buffer.append(buf, "who", "jane") mongo.bson.buffer.append(buf, "when", mongo.timestamp.create(strptime("2011-09-19 04:00:00", "%Y-%m-%d %H:%M:%s"), increment=1)) mongo.bson.buffer.append(buf, "comment", "some comment.") mongo.bson.buffer.finish.object(buf) mongo.bson.buffer.start.object(buf, "2") mongo.bson.buffer.append(buf, "who", "meghan") mongo.bson.buffer.append(buf, "when", mongo.timestamp.create(strptime("2011-09-20 13:00:00", "%Y-%m-%d %H:%M:%s"), increment=1)) mongo.bson.buffer.append(buf, "comment", "some comment.") mongo.bson.buffer.finish.object(buf) # / # FINALIZE mongo.bson.buffer.finish.object(buf) b &lt;- mongo.bson.from.buffer(buf) &gt; b _id : 2 abcd when : 17 i: 1, t: 1316390400 author : 2 alex title : 2 Some title text : 2 Some text. tags : 4 0 : 2 tag.1 1 : 2 tag.2 votes : 1 5.000000 voters : 4 1 : 2 jane 2 : 2 joe 3 : 2 spencer 4 : 2 phyllis 5 : 2 li comments : 4 1 : 3 who : 2 jane when : 17 i: 1, t: 1316397600 comment : 2 some comment. 2 : 3 who : 2 meghan when : 17 i: 1, t: 1316516400 comment : 2 some comment. mongo.insert(mongo, "test.posts", b) </code></pre> <h2>Basic sub-level query</h2> <pre><code># Get all comments by 'meghan' from 'test.posts' #-------------------- # Approach 1) #-------------------- res &lt;- mongo.find(mongo, "test.posts", query=list(comments=list(who="meghan"))) out &lt;- NULL while (mongo.cursor.next(res)) out &lt;- c(out, list(mongo.bson.to.list(mongo.cursor.value(res)))) &gt; out NULL # Does not work #-------------------- # Approach 2) #-------------------- buf &lt;- mongo.bson.buffer.create() mongo.bson.buffer.start.object(buf, "comments") mongo.bson.buffer.append(buf, "who", "meghan") mongo.bson.buffer.finish.object(buf) query &lt;- mongo.bson.from.buffer(buf) res &lt;- mongo.find(mongo, "test.posts", query=query) out &lt;- NULL while (mongo.cursor.next(res)) out &lt;- c(out, list(mongo.bson.to.list(mongo.cursor.value(res)))) &gt; out NULL # Does not work </code></pre> <p>I still must be doing something wrong here when specifying the document ;-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload