const commentSchema = new mongoose.Schema({
name: {
type: String,
default: "anonymous",
},
comment: {
type: String,
required: true,
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
required: true,
},
}, {
timestamps: true
});
const trending = await Comment.aggregate([{
$group: {
_id: "$post",
total: {
$sum: 1
},
},
}, ]);
Result of aggregation
[{
"_id": "61eb55808551961dc737c00c",
"total": 5
},
{
"_id": "61eb4a490a894ac62bd833ab",
"total": 2
}
]
hello, please is there a way i can populate the field _id after using aggregation, i want to get all the fields of post not just the _id?
Related
I am new to MongoDB/Mongoose. I am trying to do a search based on a key string for a 'Resource' which will return a list of resources based on average of ratings for that resource. I am having a hard time calculating and returning the average. This is my schema.
Resource Schema:
const ResourceSchema = mongoose.Schema({
title: {
type: String,
required: true,
},
type: {
type: String,
required: true,
},
url: {
type: String,
required: true,
},
createdDate: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Resource", ResourceSchema);
Rating Schema:
const RatingSchema = mongoose.Schema({
resourceId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Resource",
},
createdDate: {
type: Date,
default: Date.now,
},
rating: {
type: Number,
required: true,
min: 1,
max: 5,
},
review: {
type: String,
required: true,
},
});
module.exports = mongoose.model("Rating", RatingSchema);
Each Resource will have multiple Ratings. I am trying to calculate the average of ratings in my list of fetched Resources and add it to the search results.
This is what I have for my search:
Resource.find({
$or: [
{ title: { $regex: req.params.searchStr.toLowerCase(), $options: "i" } },
{ url: { $regex: req.params.searchStr.toLowerCase(), $options: "i" } },
],
})
Here's one way you could do it.
db.resources.aggregate([
{ // filter resources
"$match": {
"title": {
"$regex": "passenger",
"$options": "i"
},
"url": {
"$regex": "https",
"$options": "i"
}
}
},
{ // get ratings for resource
"$lookup": {
"from": "ratings",
"localField": "_id",
"foreignField": "resourceId",
"pipeline": [
{
"$project": {
"_id": 0,
"rating": 1
}
}
],
"as": "ratings"
}
},
{ // calculate average
"$set": {
"avgRating": { "$avg": "$ratings.rating" }
}
},
{ // don't need ratings array anymore
"$unset": "ratings"
}
])
Try it on mongoplayground.net.
I want to get the total amount of comments from Place models and I couldn't find a way to get it because I don't want to populate the comment with GET /places.
This is my place model
const placeSchema = mongoose.Schema(
{
type: { type: String },
english: { type: String },
province_code: { type: String },
district_code: { type: String, required: true },
commune_code: { type: String },
village_code: { type: String },
lat: { type: Number },
lon: { type: Number },
body: { type: String },
images: [{ type: String }],
comments: [{ type: mongoose.Types.ObjectId, ref: Comment }],
},
{
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at',
},
}
)
I use this query
data = await Place
.find()
.limit(5)
.skip(0)
.populate('comments')
.exec()
I want to get the response like this
{
"data": [
{
"images": [],
"comments": 6,
"type": "place",
"english": "99Boko",
"province_code": "23",
"district_code": "2302",
"commune_code": "230202",
"village_code": "23020202",
"lat": 135.2039,
"lon": 104.01734762756038,
"body": "<b>This place is really good</b>",
"created_at": "2021-07-20T17:41:52.202Z",
"updated_at": "2021-07-20T17:41:52.202Z",
"id": "60f70ae08e54941530d14c4c"
},
]}
Does anybody know the solution to get this kind of response ?
I have figured out to get the comment length is to use virtual count
placeSchema.virtual('comment_length', {
ref: Comment, // model to use for matching
localField: 'comments', // from `localField` i.e., Place
foreignField: '_id', // is equal to `foreignField` of Comment schema
count: true, //only get the number of docs
})
placeSchema.set('toObject', { virtuals: true })
placeSchema.set('toJSON', { virtuals: true })
and use this query
data = await Place
.find().populate({ path: 'comment_length', count: true })
.exec()
I'm getting a list of 'spot' with mongoose filtered by location and other stuff, with the code below which works fine.
But I want the value of 'rate' to be a $avg (average) of all reviews and not the list of the reviews. It's an aggregation of another collection.
this is what I get:
{
"_id":"5f0ade7d1f84460434524d3d",
"name":"Fly",
...
"rate":[
{"_id":"5f0bfca64ca1cc02ffe48faf","spot_id":"5f0ade7d1f84460434524d3d","rate":3},
{"_id":"5f0bfdb44ca1cc02ffe48fb0","spot_id":"5f0ade7d1f84460434524d3d","rate":2},
{"_id":"5f0bfdb44ca1cc02ffe48fb1","spot_id":"5f0ade7d1f84460434524d3d","rate":1}
]
},
but I would like this kind of result:
{
"_id":"5f0ade7d1f84460434524d3d",
"name":"Fly",
...
"rate": 2
},
I tried many different things, and I guess I need to use $group but can't figure out how to get the right output.
the reviews schema:
const reviewsSchema = new mongoose.Schema({
_id: {
type: ObjectId,
required: true,
},
spot_id: {
type: ObjectId,
required: true,
},
rate: {
type: Number,
required: true,
},
})
the spot Schema
const spotsSchema = new mongoose.Schema({
_id: {
type: ObjectId,
required: true,
},
name: {
type: String,
required: true,
},
...
})
The code:
Spots.aggregate
([
{
$geoNear: {
near: { type: "Point", coordinates: [ parseFloat(longitude), parseFloat(latitude) ] },
distanceField: "location",
maxDistance: parseInt(distance) * 1000,
query: {
$and: [
{ $or : filter },
{ $or : closed },
{ published: true }
]
}
}
},
{ $match: {} },
{
$lookup:{
from: 'reviews',
localField: '_id',
foreignField: 'spot_id',
as: 'rate'
}
},
])
You're really close, you just have to actually calculate the avg value which can be done using $map and $avg like so:
{
$addFields: {
rate: {
$avg: {
$map: {
input: "$rate",
as: "datum",
in: "$$datum.rate"
}
}
}
}
}
MongoPlayground
I'm using mongodb.
And I have collection Treatments and collection Patients.
I want to find all treatments that their patient.createdBy is equal to the user who asking the data.
So I tried this
const reminders = await Treatment.aggregate([
{
$lookup: {
from: 'patients',
localField: 'patientId',
foreignField: '_id',
as: 'patient'
}
},
{ $project: { reminders: 1, reminderDate: 1 } },
{ $match: { 'patient.createdBy': { $eq: req.user._id } } }
]);
According to some examples that i saw, it's should work like this.
But it's return me an empty array
If I remove the $match its return me object like this
{
"_id": "5d1e64bdc1506a00045c6a6f",
"date": "2019-07-04T00:00:00.000Z",
"visitReason": "wewwe",
"treatmentNumber": 2,
"referredBy": "wewew",
"findings": "ewewe",
"recommendations": "ewew",
"remarks": "wwewewe",
"patientId": "5cc9a50fd915120004bf2f4e",
"__v": 0,
"patient": [
{
"_id": "5cc9a50fd915120004bf2f4e",
"lastName": "לאון",
"momName": "רןת",
"age": "11",
"phone": "",
"email": "",
"createdAt": "2019-05-01T13:54:23.261Z",
"createdBy": "5cc579d71c9d44000018151f",
"__v": 0,
"firstName": "שרה",
"lastTreatment": "2019-08-02T14:20:08.957Z",
"lastTreatmentCall": true,
"lastTreatmentCallDate": "2019-08-04T15:17:35.000Z"
}
]
}
this is patient schema
const patientSchema = new mongoose.Schema({
firstName: { type: String, trim: true, required: true },
lastName: { type: String, trim: true, required: true },
momName: { type: String, trim: true },
birthday: { type: Date },
age: { type: String, trim: true },
lastAgeUpdate: { type: Date },
phone: { type: String, trim: true },
email: { type: String, trim: true },
createdAt: { type: Date, default: Date.now },
createdBy: { type: mongoose.Schema.Types.ObjectId, required: true },
lastTreatment: { type: Date },
lastTreatmentCall: { type: Boolean },
lastTreatmentCallDate: { type: Date }
});
And this is treatment schema
const treatmentSchema = new mongoose.Schema({
date: { type: Date, default: new Date().toISOString().split('T')[0] },
visitReason: { type: String, trim: true },
treatmentNumber: { type: Number, required: true },
referredBy: { type: String, trim: true },
findings: { type: String, trim: true },
recommendations: { type: String, trim: true },
remarks: { type: String, trim: true },
reminders: { type: String, trim: true },
reminderDate: { type: Date },
patientId: { type: mongoose.Schema.Types.ObjectId }
});
what I'm missing
You have vanished your patient field in the second last $project stage. So instead use it at the end of the pipeline. Also you need to cast your req.user._id to mongoose objectId
import mongoose from 'mongoose'
const reminders = await Treatment.aggregate([
{
$lookup: {
from: 'patients',
localField: 'patientId',
foreignField: '_id',
as: 'patient'
}
},
{ $match: { 'patient.createdBy': { $eq: mongoose.Types.ObjectId(req.user._id) } } },
{ $project: { reminders: 1, reminderDate: 1 } }
])
I think you can add using the pipeline, like below
const reminders = await Treatment.aggregate([
{
$lookup: {
from: 'patients',
localField: 'patientId',
foreignField: '_id',
as: 'patient',
pipeline: [{ $match: { 'age': { $eq: "100" } } }]
}
},
]);
I am trying to make a like/unlike system. and for unlike i need to remove the user from array of likes. but the mongodb $pull doesn't work.
the $inc command works fine but $pull doesn't.
here is my schema
postedBy: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "User"
},
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
],
likeCount: {
type: Number,
default: 0
},
commentBody: {
type: String
},
contentId: {
type: mongoose.Schema.Types.ObjectId,
refPath: "contentType",
required: true
},
contentType: {
type: String,
required: true,
enum: ["Video", "Comment"]
},
isApproved: {
type: Boolean,
default: true
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
],
postedOn: {
type: Date,
default: Date.now()
}
mongodb query
await Video.updateOne(
{ _id: contentId, likeCount: { $gt: 0 } },
{
$pull: {
likes: decoded.id
},
$inc: { likeCount: -1 }
}
);
You should update your query to look like this:
await Video.updateOne(
{ _id: contentId, likeCount: { $gt: 0 } },
{
$pull: {
"likes": { "_id": ObjectId(decoded.id)}
},
$inc: { likeCount: -1 }
}
);
I assume that in devede.id you have string, and string is not the same as ObjectId. Because of that we need to transform it to ObjectId.