Filtering and selecting an Array object within a Collection

Apologies first of all. I’m migrating from a SQL platform to MongoDB and still finding it strange going from a SQL relational querying to the MongoDB querying.

I’m using NodeJS to write an application to search and select records from my migrated database. I have a collection called Dealers which contains within it an array of objects called Vehicles.

I’m using an aggregation to search for vehicles with specific values. My aggregate matches the specific Dealer document, then unwinds the vehicles array and then matches this with things like Vehicles.Model: ‘FORD’, Vehicles.FuleType: ‘Petrol’ etc.

And this is where my logic hits a bit of a wall with my SQL way of thinking. My aggregate returns a number of records, and I’m not sure how to then select one of those returned records to return the vehicles full data.

Where as before the Dealers would by in a table of their own all with IDs. And the Vehicles would be in a separate table with its own ID linking to the Dealers ID.

Like I say, sorry if it’s a stupid question, but the none relational database environment is totally different a SQL one. I like it a lot. Just need to adjust my thinking.

Thanks in advance.

I think I’ve figured it out.
I added a ‘includeArrayIndex’: ‘vehicleIndex’ to my $unwind stage.

Not sure if this is the best way to do it or not.

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