Hey, guys
I’m trying to work on a solution with Mongoose to update a field in a given collection based on the occurrences count from an array in another collection (both in the same db).
To make myself clear, consider the two following schema:
const userSchema = new mongoose.Schema({
email: String,
password: String,
watched_movies: [
{
title: String,
movieId: Number,
date: Date,
},
],
});
const movieSchema = new mongoose.Schema({
user: String,
movies: [
{
title: String,
movieId: Number,
director: String,
genres: [String],
runtime: Number,
rating: Number,
review: String,
view_count: Number,
},
],
});
I want to update view_count
on movieSchema
based on how many times the same movieId
appears on watched_movies
array from userSchema
, so that I can keep the view_count
updated each time an entry is added or removed from the array. I know I can probably use reduce
function at the watched_movies
to count the occurrences, but since I’m still fairly new to MongoDB/Mongoose, I have no idea on how to put it together and work with both collections at the same time to achieve this.
If there’s a better way to approach this, I’m all ears! After all, I’m still learning =)