How to push an item to a nested array? - mongodb

I am trying to make an api with express.js and mongoDB (using mongoose) and don't know how to properly add an item to my existing array (nextUp). For example I have an existing User in my database with templates: { issues: { nextUp: [], inProgress: [], completed: [] } } and want to add e.g. {issueName: 'MyName', issueDate: '20/5/2020'} to nextUp field.
const mongoose = require('mongoose');
const { Schema } = mongoose;
const UserSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
minlength: 3,
sparse: true,
},
email: {
type: String,
required: true,
unique: true,
minlength: 5,
maxlength: 255,
sparse: true,
},
password: {
type: String,
required: true,
minlength: 8,
sparse: true,
},
newsletterSubscribed: {
type: Boolean,
},
termsAndPolicyAgreement: {
type: Boolean,
required: true,
},
templates: {
title: {
type: String,
},
description: {
type: String,
},
issues: {
nextUp: [
{
issueName: {
type: String,
},
issueDate: {
type: String,
},
},
],
inProgress: [
{
issueName: {
type: String,
},
issueDate: {
type: String,
},
},
],
completed: [
{
issueName: {
type: String,
},
issueDate: {
type: String,
},
},
],
},
},
});
const UserModel = mongoose.model('user', UserSchema);
module.exports = UserModel;
I've tried to do that but my item hasn't added. (username Kacper exists there is no problem with that).
UserModel.findOneAndUpdate(
{ username: 'Kacper' },
{
$push: {
nextUp: [
{
issueName: 'MY NEW ISSUE',
issueDate: 'MY ISSUE DATE',
},
],
},
},
);

Related

How to generate auto increment count field using Mongodb Schema

I have a database related to the interview process which consists of multiple fields.
basically, there are 5 APIs
POST - (Candidate info) - for entering candidate data
PATCH - for updating candidate info
PATCH - (for Short Listing and reviewing) - updates in existing candidate
PATCH - (for Scheduling the candidate interview) - entering the interview field which is an array object.
GET Method
I want auto increment for the count field under Interview Round Count
whenever the PATCH Method is updated for the same candidate (data updated successfully)
How can I do that in Mongodb
Complete Schema:
const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');
const { Schema } = mongoose;
const { EMAIL } = require('../../config/patterns.js');
const InterviewSchema = new Schema(
{
firstName: {
type: String,
required: true,
trim: true,
maxlength: 30,
},
lastName: {
type: String,
trim: true,
maxlength: 30,
},
email: {
type: String,
required: true,
trim: true,
match: EMAIL,
},
gender: {
type: String,
required: true,
enum: ['male', 'female', 'other'],
},
contactNumber: {
type: Number,
unique: true,
required: true,
},
alternateContactNumber: {
type: Number,
},
resume: {
type: String,
required: true,
},
designation: {
type: String,
required: true,
enum: ['trainee', 'se', 'sse', 'tl', 'systemEngineer'],
},
profile: {
type: String,
required: true,
enum: [
'react',
'reactNative',
'node',
'fullstack',
'php',
'ios',
'android',
'python',
],
},
experience: {
years: {
type: Number,
required: true,
},
months: {
type: Number,
required: true,
},
},
ctc: {
current: {
type: Number,
required: [true, 'In LPA'],
},
expected: {
type: Number,
required: [true, 'In LPA'],
},
offered: {
type: Number,
required: [true, 'In LPA'],
},
},
noticePeriod: {
type: Number,
default: 0,
},
referrer: {
type: {
type: String,
enum: ['consultant', 'employee', 'website', 'social'],
},
name: {
type: String,
trim: true,
required: true,
},
},
status: {
type: String,
enum: [
'shortlisting',
'shortlisted',
'interviewing',
'selected',
'rejected',
'onHold',
'denied',
'offerSent',
'joined',
'cancel',
],
},
// Shortling the Candidate - PATCH Method
reviewer: {
name: {
type: String,
trim: true,
required: true,
},
email: {
type: String,
trim: true,
required: true,
},
id: {
type: Number,
required: true,
},
},
date: {
type: Date,
default: Date.now,
},
// Scheduling the interview (this can be repeated no.of times for the interview round)
// represented in Array object
interview: [
{
interviewerName: {
type: String,
trim: true,
},
date: {
type: Date,
default: Date.now,
},
mode: {
type: String,
enum: ['telephonic', 'video', 'f2f'],
default: 'telephonic',
},
meeting: {
link: {
type: String,
trim: true,
},
platform: {
type: String,
trim: true,
},
},
round: {
count: { // want auto-increment count
type: Number,
},
type: {
type: String,
enum: ['written', 'technical', 'hr'],
},
},
interviewStatus: {
type: String,
enum: ['rejected', 'onHold', 'selected', 'schedule'],
},
feedback: {
technical: {
type: Number,
required: true,
min: 1,
max: 5,
},
logical: {
type: Number,
required: true,
min: 1,
max: 5,
},
communication: {
type: Number,
required: true,
min: 1,
max: 5,
},
comment: {
type: String,
min: 10,
max: 200,
},
},
recommendation: {
type: String,
enum: ['yes', 'no'],
},
},
],
},
{
timestamps: true,
},
);
InterviewSchema.plugin(mongoosePaginate);
const InterviewProcess = mongoose.model('interviewprocess', InterviewSchema);
module.exports = InterviewProcess;

how to use nested object as a ref in mongoose

I have two schemas user and note-
user schema
import mongoose from 'mongoose';
const userSchema = mongoose.Schema({
id: {
type: String,
},
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
notes: [
{
type: mongoose.Types.ObjectId,
ref: 'Note',
},
],
folders: {
type: [
{
name: {
type: String,
},
notes: [
{
type: mongoose.Types.ObjectId,
ref: 'Note',
},
],
},
],
default: [
{ name: 'My Notes', notes: [] },
{ name: 'Todos', notes: [] },
{ name: 'Projects', notes: [] },
{ name: 'Journals', notes: [] },
{ name: 'Reading list', notes: [] },
],
},
});
const User = mongoose.model('User', userSchema);
export default User;
note schema
import mongoose from 'mongoose';
const noteSchema = new mongoose.Schema({
id: {
type: String,
required: true,
},
modify_date: {
type: String,
required: true,
},
modify_time: {
type: String,
required: true,
},
tags: [
{
id: {
type: String,
required: true,
},
text: {
type: String,
required: true,
},
},
],
created_by: {
type: mongoose.Types.ObjectId,
ref: 'User',
required: true,
},
folder: {
type: String, //can I use ref from user?
},
title: {
type: String,
},
body: {
type: String,
required: true,
},
time_stamp: {
type: Date,
required: true,
},
});
const Note = mongoose.model('Note', noteSchema);
export default Note;
I have searched but could not find anything. I want to use the nested folder object from user schema as a ref in note schema; so, whenever I change or update a folders name all the notes get the updated value. Is it possible?
If not should I make a separate folder schema and add ref of folder schema in both note and user? How can I achieve that, any suggestion might be very helpful?

How to fetch data from multiple collections in MongoDB?

How to fetch data from multiple collections in MongoDB by using a common field and sending data (in those fetched collections) using one response?
Employee Scheema:
const mongoose = require("mongoose");
const employeeSchema = new mongoose.Schema(
{
profilePic: {
type: String,
},
employeeID: {
type: String,
required: true,
},
employeeFirstName: {
type: String,
required: true,
},
employeeLastName: {
type: String,
required: true,
},
birthday: {
type: String,
},
streetNo: {
type: String,
},
city: {
type: String,
},
phoneNumber: {
type: String,
},
jobRole: {
type: String,
required: true,
},
NIC: {
type: String,
required: true,
unique: true,
},
companyEmail: {
type: String,
required: true,
unique: true,
},
status: {
type: String,
required: true,
},
resignDate: {
type: String,
},
jobType: {
type: String,
required: true,
},
candidateID: {
type: String,
required: true,
},
teamID: {
type: String,
},
lastSeen: {
type: String,
},
token: {
type: String,
},
},
{ timestamps: true }
);
module.exports = mongoose.model("employee", employeeSchema);
Acadamic Qualification Scheema:
const mongoose = require("mongoose");
const academicQualificaationSchema = new mongoose.Schema(
{
employeeID: {
type: String,
required: true,
},
ordinaryLevelResult: {
type: Array,
required: true,
},
advancedLevelResults: {
type: Array,
required: true,
},
achievements: {
type: Array,
required: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model(
"academicQualification",
academicQualificaationSchema
);
Professional Qualification Scheema:
const mongoose = require("mongoose");
const proffesionalQualificaationSchema = new mongoose.Schema(
{
employeeID: {
type: String,
required: true,
},
degree: {
type: Array,
required: true,
},
language: {
type: Array,
required: true,
},
course: {
type: Array,
required: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model(
"proffesionalQualification",
proffesionalQualificaationSchema
);
Controller:
exports.viewEmployees = async (req, res) => {
try {
let accQuali, profQuali;
const employees = await employeeSchema.find();
accQuali = await academicQualificaationSchema.find();
profQuali = await ProffesionalQualificationSchema.find();
if (employees || accQuali || profQuali) {
return res.status(200).json({ data: { employees, profQuali, accQuali } });
} else {
return res.status(404).json({ message: message });
}
} catch (err) {
return res.status(404).json({ err: err.message });
}
};
This controller is working properly and sends all data in 3 collections with the use of one response. But, I am comfortable if I will be able to Fetch data separately for each employee.
If you want to get the data from the three collections for specific employee or employees, you can use an aggregation pipeline with a $lookup stage, as suggested by #1sina1. For example:
db.employee.aggregate([
{
$match: {"employeeID": "IDA"}
},
{
$lookup: {
from: "academicQualification",
localField: "employeeID",
foreignField: "employeeID",
as: "academicQualification"
}
},
{
$lookup: {
from: "proffesionalQualification",
localField: "employeeID",
foreignField: "employeeID",
as: "proffesionalQualification"
}
}
])
As you can see on the playground

find one with multiple conditions mongoose mongodb

I am trying to obtain data from mongodb. This is the scenario. Each user has a following property(array) that takes an array of users id.
The user model is as below
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
followers: [{ type: mongoose.Types.ObjectId, ref: "user" }],
following: [{ type: mongoose.Types.ObjectId, ref: "user" }],
});
in simple terms. I need to use two conditions where postedBy: { $in: req.user.following }, or postedBy:req.user._id
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
photo: {
type: String,
required: true,
},
likes: [{ type: mongoose.Schema.ObjectId, ref: "user" }],
comments: [
{
text: String,
postedBy: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
},
],
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
});
I have not figured out the second condition to add in the code below.
router.get("/getSubPost", requireLogin, async (req, res) => {
try {
const result = await Post.find({
postedBy: { $in: req.user.following },
})
.populate("postedBy", "-password")
.populate("comments.postedBy", "_id name");
res.json({ result });
} catch (error) {
console.log(error);
}
});

Mongoose - Validate ObjectID related document

I need to validate as required a field "product" in Model Event. Product is ObjectID reference to Product Model.
I tried with this 2 approaches, but it is not validating
product: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Product',
required: true
}]
},
product: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Product',
required: function () {
return this.product.length > 0
},
}]
},
The Event is being created anyway, and when I add no products, field product is an empty array.
Any idea how can I validate it?
Models:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Product = require('../models/Product');
const moment = require('moment');
const EventSchema = new Schema({
client: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Client'
}]
},
product: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Product',
required: true
}]
},
date: {
type: Date,
maxlength: 64,
lowercase: true,
trim: true
},
place: {
type: String,
maxlength: 1200,
minlength: 1,
},
price: {
type: Number
},
comment: {
type: String,
maxlength: 12000,
minlength: 1,
},
status: {
type: Number,
min: 0,
max: 1,
default: 0,
validate: {
validator: Number.isInteger,
message: '{VALUE} is not an integer value'
}
},
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
},
{
timestamps: true
},
);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Provider = require('./Provider')
const ProductSchema = new Schema({
name: {
type: String,
maxlength: 64,
minlength: 1,
required: [true, 'Product name is required'],
},
brand: {
type: String,
maxlength: 64,
minlength: 1,
},
description: {
type: String,
maxlength: 12000,
min: 1,
},
comment: {
type: String,
maxlength: 12000,
minlength: 1
},
state: {
type: String,
maxlength: 64,
minlength: 0
},
disponible: {
type: Boolean,
default: true
},
price: {
type: Number,
default: 0,
min: 0,
max: 999999
},
provider: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Provider'
}]
},
category: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}]
},
event: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Event'
}]
},
image: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Image'
}]
},
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
},
{
timestamps: true
});
You can use custom validators feature of mongoose.
If the validator function returns undefined or a truthy value, validation succeeds. If it returns falsy (except undefined) or throws an error, validation fails.
product: {
type: [
{
type: Schema.Types.ObjectId,
ref: "Product",
required: true
}
],
validate: {
validator: function(v) {
return v !== null && v.length > 0;
},
message: props => "product is null or empty"
}
}
Now when you don't send product field, or send it empty array it will give validation error.
const notEmpty = function(users){
if(users.length === 0){return false}
else { return true }
}
const EventSchema = new Schema({
product: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Product',
required: true,
validate: [notEmpty, 'Please add at least one']
}]
}
})