Are comments supported in an aggregation command?

The aggregation commands can get large quickly. I noticed I can add comments…

db.solarSystem.aggregate(
[
	// LIMIT 8 IN NATURAL ORDER, WHICH INCLUDES SUN, THEN SKIP SUN
	{
		"$project":
		{
			_id: 0
			, name: 1
			, numberOfMoons: 1
		}
	}
	,
	{
		"$limit": 8
	}
	,
	{
		"$skip": 1
	}
]
)

Is this supported? Seems to work OK.

I used the following to make things more readable.

var projection = { _id: 0 , name: 1 , numberOfMoons: 1 }
projection
var project = { “$project” : projection }
project
var limit = { “$limit”: 8 }
limit
var skip = { “$skip”: 1 }
skip
var pipeline = [ project , limit , skip ]

db.myCollection.aggragate( pipeline )

The words projection, project, … skip by them self print the document so you get a trace of what your are building while executing it.

1 Like

@steevej-1495 - thanks for the input. I agree - good naming conventions can help. Still, the question remains - are comments supported?

Hi,

Have never tried that. It looks OK in the Mongo Shell as long as the comment is in a new line. Anyway, if you were creating a program or script it does make sense to include comments as usual. I cannot see why one would want to include comments directly in an aggregation stage. My suggestion is along the same lines as @steevej-1495’s post. I would do something like this and save the code in a js file:

// LIMIT 8 IN NATURAL ORDER, WHICH INCLUDES SUN, THEN SKIP SUN
var projection = { _id: 0 , name: 1 , numberOfMoons: 1 };
...

José Carlos