Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, note that your sort example is malformed: the aggregate method takes an array as input, where each element in the array specifies a stage in an aggregation pipeline. Also, note that the <a href="http://docs.mongodb.org/manual/reference/operator/elemMatch/">$elemMatch</a> operator cannot be used as part of a $sort stage.</p> <p>One way to achieve what you're trying to do with your sort example is use the aggregation framework's <a href="http://docs.mongodb.org/manual/reference/aggregation/unwind/">$unwind</a> pipeline operator. Unwinding an array will peel the array elements off one-by-one into separate documents. For example, the following query</p> <pre><code>db.my_collection.aggregate([ {$unwind: "$answers"} ]); </code></pre> <p>returns something like the following:</p> <pre><code>[ { "_id" : ObjectId("5237157f3fac8e36fdb0b96e"), "user" : "bruno", "answers" : { "id" : 0, "value" : 3.5 } }, { "_id" : ObjectId("5237157f3fac8e36fdb0b96e"), "user" : "bruno", "answers" : { "id" : 1, "value" : "hello" } }, { "_id" : ObjectId("523715813fac8e36fdb0b96f"), "user" : "bruno2", "answers" : { "id" : 0, "value" : 0.5 } }, { "_id" : ObjectId("523715813fac8e36fdb0b96f"), "user" : "bruno2", "answers" : { "id" : 1, "value" : "world" } } ] </code></pre> <p>Adding a $match phase will allow you to grab only the documents where answers.id is zero. Finally, a $sort phase allows you to sort by answers.value. All together the aggregation query is:</p> <pre><code>db.my_collection.aggregate([ {$unwind: "$answers"}, {$match: {"answers.id": 0}}, {$sort: {"answers.value": -1}} ]); </code></pre> <p>And the output:</p> <pre><code>[ { "_id" : ObjectId("5237157f3fac8e36fdb0b96e"), "user" : "bruno", "answers" : { "id" : 0, "value" : 3.5 } }, { "_id" : ObjectId("523715813fac8e36fdb0b96f"), "user" : "bruno2", "answers" : { "id" : 0, "value" : 0.5 } } ] </code></pre> <p>Based on what your asking, it doesn't sound like you'll always need $unwind or even the aggregation framework. If instead you wanted to find the document with answers.id equal to 0 and answers.value equal to 3.5, and then change answers.value to 4 you could use find with $elemMatch followed by db.collection.save():</p> <pre><code>doc = db.my_collection.findOne({"answers": {$elemMatch: {"id": 0, "value": 3.5}}}); for (i=0; i&lt;doc.answers.length; i++) { if (doc.answers[i].id === 0) { doc.answers[i].value = 4; db.my_collection.save(doc); break; } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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