How sum each values of relationship objectId with mongoose - node.js

i want to sum each values of ObjectId field in relationship with moongose..
I have this:
`var wineSchema = new Schema({
name: { type: String },
code: { type:String},
type: {
type: String,
enum: ['Red','Rose','White']
},
winery: { type: String },
grape_type: { type: String },
year: { type: Number },
alcohol: { type: Number },
rates: [{ type: Schema.ObjectId, ref: "Rate" }],
Comments: [{ type: Schema.ObjectId, ref: "Comment" }],
});`
And this:
`var rate = new Schema({
user : { type: Schema.ObjectId, ref: "User" },
userName : {type: String},
wineName : { type: String},
rating : { type: Number}
});`
I want sum total values of each rate.rating inside wineSchema.rates..
How can I do it?

Related

mongoose query, find where array of tags id includes at least one id from query tags array

I have article schema :
const schema = new Schema({
title: {
type: String,
unique: true,
required: [true, 'Title is required'],
},
slug: {
type: String,
slug: "title",
slugPaddingSize: 2,
unique: true
},
editor: {
type: String
},
duration: {
type: String
},
image: Object,
category: { type: Schema.Types.ObjectId, ref: 'Category' },
language: { type: Schema.Types.ObjectId, ref: 'Language' },
level: { type: Schema.Types.ObjectId, ref: 'Level' },
tag: [{ type: Schema.Types.ObjectId, ref: 'Tag' }]
},{
timestamps: true
})
Need to find every articles which tag contains at least one of tag id from req.query params (it can be also array of id's or just single id).
Thanks

How to populate a field from an array of objects of another a collection that it's not a model in mongoose

for example I have two collections,
1st collection schema (test):
const testSchema = new mongoose.Schema(
{
_id: mongoose.Schema.Types.ObjectId,
name: {
type: String,
required: [true, "le nom d'examen est obligatoire!"],
},
year: {
type: Number,
required: [true, "l'année est obligatoire"],
},
questions: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Question",
},
],
clinicalCase: [
{
_id: {
type: mongoose.Schema.Types.ObjectId,
index: true,
required: true,
auto: true,
},
text: String,
},
],
},
{
timestamps: true,
}
);
2nd schema:
const questionSchema = new mongoose.Schema({
test: {
type: mongoose.Schema.Types.ObjectId,
ref: "Test",
},
questionString: {
type: String,
required: [true, "la question est obligatoire"],
},
explanation: String,
options: [option],
tag: {
type: mongoose.Schema.Types.ObjectId,
ref: "Tag",
},
clinicalCase: {
type: mongoose.Schema.Types.ObjectId,
}
});
so my problem here is when I query 2nd schema, I want to populate the clinicalCase field with 1st schema, is it possible to do it with populate method or not ? can you help me with it please

How to multi populate in field in mongodb

Here is my query
const {a_id, b_id, rate} = req.body
const rate = await Rate.create({
a_id,// In my model schema of Rate I've referenced it to another table which is `user`
b_id,// In my model schema of Rate I've referenced it to another table which is `class`
rate// this is the random rate I wanna pass into body
})
Now I want to rate on the basis of a_id and b_id, How can i pass a_id and b_id
This is my Rate Schema:
a_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
b_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "class",
},
rate: {
type: Number,
required:true
},
Here is the class schema
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
rate: {
type: Number,
required: true
},
This is user Schema :
email: {
type: String,
required: true,
},
username: {
type: String,
required: true
},
role:
{
type: mongoose.Schema.Types.ObjectId,
ref: "Role",
},
This is role Schema.
role: {
type: String,
enum: ['Super Admin', 'Jury', 'Selection'],
required: true
}

Building Mongoose models for an Express Nodejs API with refpath

I'm new to building rest api's with mongoose and express and have a question on how to use refPath correctly on my Models files and allowing for an array of items.
Below I have included the code for a model (built thus far) and would love any input on if I'm even close to building this correctly.
I will also include a screenshot that visually depicts the relationships I'm trying to create.
Those who answer questions here are GODS and I appreciate all the help this community has given me over the years!
const mongoose = require("mongoose");
const slugify = require("slugify");
const AlertSchema = new mongoose.Schema({
parentId: {
type: mongoose.Schema.ObjectId,
required: true,
refPath: "parentModel",
},
parentModel: {
type: String,
required: true,
enum: ["orgs", "clients"],
},
status: { type: String, default: "no-status" },
departments: [{ type: mongoose.Schema.Types.ObjectId, ref: "orgs" }],
createdAt: { type: Date, default: Date.now },
createdByType: [{ type: mongoose.Schema.Types.ObjectId, ref: "users" }],
createdById: [{ type: mongoose.Schema.Types.ObjectId, ref: "users" }],
groups: [{ type: String, default: "unGrouped" }],
stage: [{ type: mongoose.Schema.Types.ObjectId, ref: "stages" }],
children: { type: String },
resource: {
type: String,
match: [
/https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*)/,
"Please use a valid URL with HTTP or HTTPS",
],
},
notes: [{ type: mongoose.Schema.Types.ObjectId, ref: "notes" }],
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: "comments" }],
priority: { type: String },
assignedTo: [{ type: mongoose.Schema.Types.ObjectId, ref: "users" }],
title: {
type: String,
required: [true, "Please add a title"],
maxlength: [50, "Title cannot be more than 50 characters"],
},
message: {
type: String,
required: [true, "Please add a message"],
maxlength: [500, "Message cannot be more than 500 characters"],
},
slug: String,
});
//create alert slug from the title
AlertSchema.pre("save", function (next) {
console.log("Slugify Ran", this.name);
this.slug = slugify(this.title, { lower: true });
next();
});
module.exports = mongoose.model("Testalert", AlertSchema);
Desired relationships diagram:

nested ref to populate mongoose 5.0 nodejs

I have two models. The first one is UserSchema and the second one is CategorySchema
var UserSchema = Schema({
firstName: {
type: String,
required: true
},
secondName: String,
lastName: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
status: {
type: String,
required: true
},
roles: [{
type: Schema.ObjectId,
ref: 'Role'
}],
publications: [{
title: {
type: String,
},
description: String,
status: {
type: String,
},
createdAt: {
type: Date
},
updatedAt: {
type: Date,
default: Date.now()
},
pictures: [{
name: String
}],
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}],...
the model category is
var CategorySchema = Schema({
name: String,
subcategories: [{
name: String
}]
});
UserSchema has publications. Publications contains an array. Within of publications is categories that contains an array of id of subcategory(subcategory is whithin of CategorySchema)
the problem is when I need to populate categories of UserSchema. Categories of UserSchema have an array of _id of subcategory that belongs to CategorySchema.

Resources