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
}
Related
I need a code in Express.Js and Mongodb where I can change an exact string inside an object through the id of the item, I need to change the string "colorSelected" to a new value, changing only what I put in the body, in my previous failed attempts every change I made would change the entire object, I don't want that, thank you in advance.
Router Cart.js
//update color cart
router.patch("/cart/:id", Auth, async (req, res) => {
});
Model Cart.js
const mongoose = require('mongoose')
const ObjectID = mongoose.Schema.Types.ObjectId
const cartSchema = new mongoose.Schema({
owner : {
type: ObjectID,
required: true,
ref: 'User'
},
items: [{
itemId: {
type: ObjectID,
ref: 'Item',
required: true
},
img: String,
name: String,
colorSelected: String,
colors: Array,
stock: Number,
quantity: {
type: Number,
required: true,
min: 1,
default: 1
},
price: Number
}],
bill: {
type: Number,
required: true,
default: 0
}
}, {
timestamps: true
})
const Cart = mongoose.model('Cart', cartSchema)
module.exports = Cart
Model Item.js
const mongoose = require('mongoose')
const ObjectID = mongoose.Schema.Types.ObjectId
const reviewSchema = mongoose.Schema(
{
name: { type: String, required: true },
rating: { type: Number, required: true },
comment: { type: String, required: true },
user: {
type: ObjectID,
required: true,
ref: 'User'
},
},
{
timestamps: true,
}
)
const itemSchema = new mongoose.Schema({
images: [{
name: {
type: String,
required: true
},
src: {
type: String,
required: true
},
color: {
type: String,
required: true
},
}],
owner : {
type: ObjectID,
required: true,
ref: 'User'
},
name: {
type: String,
required: true,
trim: true
},
description: {
type: String,
required: true
},
category: {
type: Number,
required: true
},
price: {
type: Number,
required: true
},
stock: {
type: Number,
required: true
},
visibility: {
type: Boolean,
required: true
},
reviews: [reviewSchema],
rating: {
type: Number,
required: true,
default: 0,
},
numReviews: {
type: Number,
required: true,
default: 0,
}
}, {
timestamps: true
})
const Item = mongoose.model('Item', itemSchema)
module.exports = Item
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
so i'm trying ref a array of daysOff (Ferias model) as a fiel in a User model,
i'm new to MongoDB and mongoose, and i'm trying to build a API that manages a company workers days off.
I apologyse in advance for the "format" of this question but it's the very first time that I post a question in StackOverflow :)
Thanks u all!
const mongoose = require("mongoose");
const moment = require("moment");
const feriasSchema = mongoose.Schema(
{
user: {
type: mongoose.Types.ObjectId,
required: true,
ref: "User",
},
firstName: {
type: String,
ref: "User",
},
lastName: {
type: String,
ref: "User",
},
workerNumber: {
type: Number,
ref: "User",
},
sectionOfWork: {
type: String,
ref: "User",
},
totalFerias: {
type: Number,
required: true,
},
//this is the field i want to add in User model as an array of (dates format DD/MM/YYYY)
ferias: {
type: [String],
//default: [Date],
required: true,
},
tipoFerias: {
type: String,
required: true,
},
modo: {
type: String,
required: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model("Ferias", feriasSchema);
// USER MODEL
const userSchema = mongoose.Schema(
{
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
email: {
type: String,
required: [true, "Please enter a valid email address"],
unique: true,
},
password: {
type: String,
required: [true, "Please enter a password"],
},
workerNumber: {
type: Number,
required: true,
unique: true,
},
sectionOfWork: {
type: String,
required: true,
},
chefia: {
type: String,
required: true,
},
//this is the field i want to be an array (from Ferias model)
ferias: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Ferias",
}],
role: {
type: String,
required: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model("User", userSchema);
what am i doing wrong?
i'm getting and empty array ...
in express I can send the questioned data separately but u want to send a json to some frontend with all the fields of a user, with the "ferias" array present.
Hi I want to implement this snippet of my class diagram using mongoose.
Here is my logic model in detail:
Here is the definition of my Mother model:
const mongoose = require('mongoose')
const motherSchema = mongoose.Schema({
cniMother: { type: Number, required: true },
name: { type: String, required: true },
surname: { type: String, required: true },
birthDay: { type: Date, required: true },
birthPlace: { type: String, required: true },
address: { type: String, required: true },
phone: { type: Number, required: true },
occupation: { type: String, required: true },
});
module.exports = mongoose.model('Mother', motherSchema);
Here is the definition of my Declared model:
const mongoose = require('mongoose')
const declaredSchema = mongoose.Schema({
name: { type: String, required: true },
surname: { type: String, required: true },
father: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Father',
//required: true,
},
mother: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Mother',
//required: true,
},
birthDay: { type: Date, required: true },
birthPlace: { type: String, required: true },
gender: { type: String, required: true },
nationality: { type: String, required: true }
});
module.exports = mongoose.model('Declared', declaredSchema);
I want to retrieve the name of the father and the mother corresponding to the declared.
Can anyone help me please?
I have a practice problem on mongoose.
I have two models, let's call them product and support.
I need to create a third model which is the result of a mathematical calculation between some fields of the first two models (product and support).
However, I don't want this third model to be saved in the database, unless the user wants it.
I would like to have some advice on the best way to do it.
Thanks in advance
as you can see I have two models (product and support) and a third model (calculation) which is the result of a calculation between the first two models.
const productSchema = new mongoose.Schema({
diametre : {
type: Number,
required: true
},
type: {
type: String,
enum : ['metal', 'wood']
},
weight : {
type: Number,
required: true
},
length: {
type: Number,
required: true
},
createdAt: {
type: Date,
default: Date.now()
}
})
const supportSchema = new mongoose.Schema({
reference: {
type: String,
required: true
},
type: {
type: String,
required: true
},
material: {
type: String,
enum : ['wood', 'metal']
},
internalDiameter : {
type: Number,
required: true
},
externalDiameter : {
type: Number,
required: true
},
internalWidth : {
type: Number,
required: true
},
externalWidth : {
type: Number,
required: true
},
createdAt: {
type: Date,
default: Date.now()
}
})
const calculSchema = new mongoose.Schema({
product: {
type: mongoose.Schema.ObjectId,
ref: 'Product',
required: true
},
support: {
type: mongoose.Schema.ObjectId,
ref: 'Support',
required: true
},
lengthOnSupport:{
type: Number,
required: true
},
numberOfSupport: {
type: Number,
required: true
},
createdAt: {
type : Date,
default : Date.now()
}
})