mongodb/docs-tools — pull request #53 by tychoish (2014-08-02 23:26:37+00:00)
Title: Examples Working Draft

This just has the backend infrastructure and models to read in example data and handle the inheritance in a depth-first way.

Probably spending too much effort allowing for `example-.yaml` files that would have a collection and then multiple examples (i.e. different kinds of docs in a single yaml file.) Should be able to iron some of that out.

Remaining work:
- Get reviews (@kay-kim @judah)
- Write rendering/rstcloth code to generate rst.
- test inheritance

I modified the example structure a bit to look like this:

```
collection: "sales"
pre: |
Consider a ``sales`` collection with the following documents:
documents:
- '{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") }'
- '{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }'
- '{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }'
- '{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }'
- '{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }'
---
ref: 'base'
pre: |
Grouping the documents by the ``item`` field, the following operation
uses the :group:`$first` accumulator to compute the first sales date for
each item:
operation:
- language: javascript
code: |
db.sales.aggregate(
[
{ $sort: { item: 1, date: 1 } },
{
$group:
{
_id: "$item",
firstSalesDate: { $first: "$date" }
}
}
]
)
post: "The operation returns the following results:"
results:
- '{ "_id" : "xyz", "firstSalesDate" : ISODate("2014-03-15T09:00:00Z") }'
- '{ "_id" : "jkl", "firstSalesDate" : ISODate("2014-03-01T09:00:00Z") }'
- '{ "_id" : "abc", "firstSalesDate" : ISODate("2014-03-01T08:00:00Z") }'
...
```