Take the 2-minute tour ×

I am playing around with the mongo aggregation framework, and I can not at all figure out some of the terminology. Most particularly, in some of the examples the reference $project being in "inclusive mode". I have also heard of _id being selected "implicity" in related contexts. Can anyone clarify?


    { $project : {
        title : 1 ,
        stats : {
            pv : "$pageViews",
            foo : "$other.foo",
            dpv : { $add:["$pageViews", 10] }

This projection includes the title field and places $project into “inclusive” mode. Then, it creates the stats documents with the following fields:
share|improve this question

1 Answer 1

up vote 1 down vote accepted

_id being selected "implicity"

When you making projections, you have to specify all field explicitly:

> db.a.find()
{ "_id" : ObjectId("51c8744a1c0a41d783d77431"), "a" : 1, "b" : 2, "c" : 3 }
> db.a.aggregate({$project:{a:1}})
    "result" : [
            "_id" : ObjectId("51c8744a1c0a41d783d77431"),
            "a" : 1
    "ok" : 1

Here you include only 'a' in result document, all other fields was dropped. The only difference is _id field, which is always included, but you can explicitly turn it off:

> db.a.aggregate({$project:{a:1, _id:0}})
{ "result" : [ { "a" : 1 } ], "ok" : 1 }

$project being in "inclusive mode"

This is simple: if you want some field to be included in result document as-is, you can just type somethig like {a:1}, it just shortcut to saying {a:'$a'}

share|improve this answer

Your Answer


