I have 2 collections one is user and other is college. I want a rating schema with aggregate query user 1 gives 2 rating to college 1
I made a rating schema like
rate = mongoose.Schema({
rating: number,
userId:
collegeid:})
college comes on the basis of score user gets. And then user is allowed to rate the colleges, he has been shown.
So how to write the query to fetch the rating. How can I achieve this?
2
Answers
You can use the built-in mongoose populate function if you define your Schema correctly.
Please check the link below
https://mongoosejs.com/docs/populate.html
The "mongoose" way would look something like
And then you can use
It is good however to read up and learn about the aggregate pipeline, but mongoose can do a lot of heavy lifting for you.
Let’s assume the
rate
collection has data like this:Here are some useful queries to get started. In general,
aggregate()
is The Newfind()
(since at least v3.2) and although slightly more complicated for the simplest expressions, it is vastly more powerful and it should be your starting point.