0:00 All right, so let's go back into our messages 0:03 service, where 0:04 we're gonna go ahead and actually fix this 0:08 type error where the messages resolver is complaining because 0:12 currently we're returning an array of message documents. 0:15 We need to transform these message documents into message 0:19 entities that contain the actual user on them. 0:23 We know, of course, we don't store the user object in the 0:25 database, we store the 0:26 user ID. So we need to map the user ID to the user 0:30 on this message document. So we can take 0:33 advantage of MongoDB aggregation for this. 0:38 And this is gonna allow us to make one single database operation 0:42 and perform complex operations like lookups, for example, to 0:46 map our user ID to the user in the users 0:50 collection, all in a single database operation, 0:53 which is gonna 0:54 be much more efficient because we're only making one network call to the database, 0:58 and then we're returning back the message entity in its final 1:02 form. 1:03 So firstly, to do this, we need to go back to our abstract 1:06 repository 1:08 and make the model that we're passing into the constructor, 1:12 which we know 1:12 we're actually using to e- issue the Mongoose commands. 1:16 I wanna make this a public property, and that's because I wanna be able to use 1:19 it and execute aggregations on given 1:23 collections. So now we can go back to the messages 1:26 service, and 1:27 let's remove our existing function. 1:29 Instead, we're gonna now call 1:31 this.chatsRepository.model so we can access 1:35 the underlying model, and then call the aggregate function, 1:38 which 1:39 is gonna allow us to pass in any number of MongoDB aggregation 1:43 pipelines, which are essentially just different 1:46 stages of 1:47 this aggregation, which is gonna allow us to transform our 1:50 documents 1:51 into any shape we want with the help of aggregation functions.
The rest of the transcript is for members.
Sign in