Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can apply mongodb <a href="http://docs.mongodb.org/manual/core/map-reduce/" rel="nofollow"><strong>Map/Reduce</strong></a> technique for this task.</p> <p>1) <strong><code>map</code></strong> function will determine nested objects from <code>relation</code> object, which contain <code>status</code> field equal to <code>"friends"</code> value:</p> <pre><code>var map = function () { for (var key in this.relation) { var relation = this.relation[key]; if (relation.status == "friends") emit(key, relation); } } </code></pre> <p>2) <strong><code>reduce</code></strong> function will actually do nothing, just return single emitted relation:</p> <pre><code>var reduce = function (key, values) { return values[0]; } </code></pre> <p>As a result you'll have collection with such objects:</p> <pre><code>{ _id: "2b2b2b2b2b2b2b2b2b2b2b", value: { status: "friends", status_changed_date: ISODate("2013-10-28T00:00:00.0000Z"), // etc... } } </code></pre> <hr> <p>If you decide to replace <code>relation</code> object with array, you can use <a href="http://docs.mongodb.org/manual/reference/operator/aggregation/" rel="nofollow">aggregation framework</a> to construct similar query.</p> <p><strong>User Data:</strong></p> <pre><code>{ _id: ObjectId("1a1a1a1a111a1a11a1a1a1a"), name: "Tester", relations: [ { id: "2b2b2b2b2b2b2b2b2b2b2b", status: "friends", status_changed_date: ISODate("2013-10-28T00:00:00.0000Z"), // etc... }, { id: "3c3c3c3c3c3c3c3c3c3c3c", status: "engaged", status_changed_date: ISODate("2013-10-28T00:00:00.0000Z"), // etc... } ] } </code></pre> <p><strong>Query:</strong></p> <pre><code>db.collection.aggregate([ { $unwind: "$relations" }, { $match: { "relations.status": "friends" } }, { $project: _id: 0, id: "$relations.id", status: "$relations.status", status_changed_date: "$relations.status_chaged_date" // etc... } ]) </code></pre> <p><strong>Output:</strong></p> <pre><code>{ id: "2b2b2b2b2b2b2b2b2b2b2b", status: "friends", status_changed_date: ISODate("2013-10-28T00:00:00.0000Z"), // etc... } </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. VO
      singulars
      1. This table or related slice is empty.
    2. 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