Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think what you are looking for is dot notation: <a href="http://docs.mongodb.org/manual/reference/glossary/#term-dot-notation" rel="nofollow">http://docs.mongodb.org/manual/reference/glossary/#term-dot-notation</a></p> <p>Question 1:</p> <blockquote> <ul> <li>How many blocks exist with a non-null/non-empty codes array?</li> </ul> </blockquote> <pre><code>db.videos.find( { 'video.blocks.codes.0' : { $exists : true } } ) </code></pre> <p>Effectively does the zero-th element of the array exist. For speed you create an index on <code>video.blocks.codes</code>. Also note that you will get back all of the video documents with at least 1 non-empty codes array within a block. To count the blocks you will have to do client side processing to remove the extra blocks.</p> <p>Question 2:</p> <blockquote> <ul> <li>How many blocks exist where the codes array matches a certain string in a given position?</li> </ul> </blockquote> <p>Very similar answer. For a position 3:</p> <pre><code>db.videos.find( { 'video.blocks.codes.3' : 'the magic code' } ) </code></pre> <p>Sorry I don't know Mongoid but hopefully you can translate the above.</p> <p>HTH - Rob.</p> <p>Edit:</p> <blockquote> <p>This doesn't work, because blocks is embedded and codes is an array within blocks.</p> </blockquote> <p>I don't think I understand the question then. The shell returns What I expect.</p> <p>Example from the shell (reformatted) - First the data:</p> <pre><code>&gt; db.test.find() { "_id" : ObjectId("51b7cfff0ccc6eb8b11c82b1"), "blocks" : [ { "index" : 1, "codes" : [ "a", "g", "c" ] }, { "index" : 2, "codes" : [ ] } ] } { "_id" : ObjectId("51b7d0300ccc6eb8b11c82b2"), "blocks" : [ { "index" : 1, "codes" : [ "x", "b", "d", "e" ] }, { "index" : 2, "codes" : [ "x", "b" ] } ] } { "_id" : ObjectId("51b7d0a50ccc6eb8b11c82b3"), "blocks" : [ { "index" : 1, "codes" : [ ] } ] } </code></pre> <p>First Query: Find all documents with a block with at least 1 code:</p> <pre><code>&gt; db.test.find( { 'blocks.codes.0' : { $exists : true } } ) { "_id" : ObjectId("51b7cfff0ccc6eb8b11c82b1"), "blocks" : [ { "index" : 1, "codes" : [ "a", "g", "c" ] }, { "index" : 2, "codes" : [ ] } ] } { "_id" : ObjectId("51b7d0300ccc6eb8b11c82b2"), "blocks" : [ { "index" : 1, "codes" : [ "x", "b", "d", "e" ] }, { "index" : 2, "codes" : [ "x", "b" ] } ] } </code></pre> <p>Second Query: Find all documents where the n'th code is a specific value. In this case I chose the second (index 1) is 'g'.</p> <pre><code>&gt; db.test.find( { 'blocks.codes.1' : "g" } ) { "_id" : ObjectId("51b7cfff0ccc6eb8b11c82b1"), "blocks" : [ { "index" : 1, "codes" : [ "a", "g", "c" ] }, { "index" : 2, "codes" : [ ] } ] } </code></pre>
    singulars
    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. 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