Noob question about aggregations and aggregation cursors

Hi there. First off, sorry for the very noob question here. I’m still trying to get my head round MongoDB coming from a SQL background.

I’ve got a NodeJS that’s pulling data from a MongoDB collection. My data looks a bit like this:

{
value: “something”,
otherValue: “something else”,
images: [{ array of objects }],
specs: [{ array of objects }]
}

I’m using an aggregation to unwind both the images and specs arrays to reorder them. This creates about 300 new documents.

In my Node application I then iterate through the returned aggregate cursor like this:

await aggCursor.forEach(o => {
vehObj.push(o) // Pushed into an array for later use
})

But this seems to me a bit of a waste because the “value:” and “otherValue:” will always be the same, but will be iterated 300 times. Whereas what I want to do is push the “value:” and “otherValue:” once, and only iterate the “images:” and “specs:” objects.

Is there a better way of doing this, or should I be regrouping the “images:” and “specs:” back into an array of objects as part of the aggregation process?

Hope this makes sense.

Thanks in advance.

Hi @Andy_Bryan,

Yes $unwind is a waste in this case because you want to sort an array of docs. You can also let the application layer do the job, but data manipulation is supposed to be done by the database by definition.

So there is 3 ways to sort an array of docs in MongoDB. The terrible way. The old school but OK way. And there is the pro way.

  1. The terrible way is $unwind + $sort + $group by _id with $push. It’s never a good idea to unwind an array and then reassemble the docs back again with a transformation. There is always a better way to avoid this mess in the memory. But in term of logic, it’s the easiest one to understand.
  2. The alternative solution to sort an array BEFORE v5.2 was this complicated pipeline that looks complex on paper but is a lot more memory efficient than the previous solution. You can also read more about it in @Paul_Done 's book here.
  3. Since 5.2, we have the pro version. Simple, elegant and optimized: $sortArray

Remember that the aggregation framework is Turing complete. You can literally do anything. I even wrote the Game of Life with it and @John_Page also mined bitcoins with it so… You can reduce your data manipulation in your back-end to the minimum if you can write the right query. :slight_smile:

Cheers,
Maxime.

1 Like

Brilliant.
Once again, many thanks for your help.

Andy.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.