Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By "taxonomy", are you hoping to store arbitrary key/value pairs for each document and later query by those values? You may find <a href="http://hexane.org/blog/2011/05/16/mongodb-content-taxonomy-schema/" rel="nofollow">MongoDB Content Taxonomy Schema</a> a useful resource.</p> <p>The gist is that top-level document will have a field containing an array of zero or more objects. Each of those objects will contain the key and value in their own fields. You can then create a <a href="http://docs.mongodb.org/manual/core/indexes/#compound-indexes" rel="nofollow">compound</a>, <a href="http://docs.mongodb.org/manual/core/indexes/#multikey-indexes" rel="nofollow">multi-key index</a> on the key/value fields within the array. Although compound, multi-key indexes cannot include multiple array fields (things could quickly get out of hand with indexing the <a href="http://en.wikipedia.org/wiki/Cartesian_product" rel="nofollow">Cartesian product</a> of elements from each array), in this case we're indexing two fields within the same array.</p> <p>To use your example data, you might have the following:</p> <pre><code>&gt; db.foo.insert({ filename: "sample.htm", taxonomy: [ { k: "author", v: "Bob Smith" }, { k: "title", v: "Java Syllabus" } ] }); &gt; db.foo.ensureIndex({ "taxonomy.k": 1, "taxonomy.v": 1 }); </code></pre> <p>Per the examples in the linked blog post, you could query by key/value criteria, using <a href="http://docs.mongodb.org/manual/reference/operator/elemMatch/" rel="nofollow"><code>$elemMatch</code></a> to ensure the key/value are both contained in the same array element.</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. 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