(mongoose-sequnce)(mongoose) + Hapijs Adding auto increment in schema field - auto-increment

I want to add an auto-increment field in the MongoDB schema for
Interview.round.count field i.e. whenever I will Update the interview field it will automatically increase the Count value with +1
//Here is My Schema Field
const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');
const { Schema } = mongoose;
const InterviewSchema = new Schema(
{
name: {
type: String,
required: true,
trim: true,
maxlength: 30,
},
email: {
type: String,
required: true,
trim: true,
},
gender: {
type: String,
required: true,
},
contactNumber: {
type: Number,
required: true,
}
interview: [
{
interviewerName: {
type: String,
trim: true,
},
round: {
count: {
type: Number,
default: 0,
},
type: {
type: String,
default: 'telephonic',
},
},
recommendation: {
type: String,
default: '',
},
},
],
},
},
{
timestamps: true,
},
);
InterviewSchema.plugin(mongoosePaginate);
const InterviewProcess = mongoose.model('interviewprocess', InterviewSchema);
module.exports = InterviewProcess;
Interview schema is defined as an array object.
My goal is to auto-increment the Interview.round.count i.e. for the count field
Can anyone please help me?

Related

How to receive information from a model inside an array in another model? (mongoose)

I have a "cart" model, and within my "order" model, I would like to have an array that receives the information sent by "cart" stored in an array. How can I do this through ref?
const mongoose = require('mongoose');
const CartSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
note: {
type: String,
required: true,
},
price: {
type: Number,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Cart", CartSchema);
order
const mongoose = require('mongoose');
const OrderSchema = new mongoose.Schema(
{
list: {
name: String,
notes: String,
},
totalAmount: {
type: Number,
required: true,
},
payment: {
type: String,
required: true,
},
address: {
type: String,
required: true,
},
addressNote: {
type: String,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Order", OrderSchema);
Here in "list" I would like it to be an array that receives cart model information
Can I do this through ref? What would be the best possible way?

What is use of "ref" ? and how here import of one model in another is done?

I have a category model and product model. i refer category model from product model in category property of schema by using ref:"Category" . when i search in web that show for ref we need to first require the model. but instructor did not require category model in product he just export it . so is ref works here because i cannot understand it works or not ??
category model-->
const mongoose = require("mongoose");
const categorySchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: true,
maxlegth: 32,
},
},
{ timestamps: true }
);
module.exports = mongoose.model("Category", categorySchema);
product model-->
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const productSchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: true,
maxlegth: 32,
},
description: {
type: String,
required: true,
maxlegth: 2000,
},
price: {
type: Number,
trim: true,
required: true,
maxlegth: 32,
},
price: {
type: Number,
trim: true,
required: true,
maxlegth: 32,
},
category: {
type: ObjectId,
ref: "Category", //refer to category models
required: true,
},
quantity: {
type: Number,
},
sold: {
type: Number,
default: 0,
},
photo: {
data: Buffer,
contentType: String,
},
shipping: {
required: false,
type: Boolean,
},
},
{ timestamps: true }
);
module.exports = mongoose.model("Product", productSchema);

Calculating the length of a schema and storing it in the data base

I am using node.js express.js and mongoose to develop my back end, I have this mongoose schema where I want to add a field called NumberRecords for every stored document this field will have the length value of the array csvData, I looked around some similar question here and I found I can use this pre function which I did but I get nothing on my database no field called NumberRecords is being added.
How do I fix this?
const rowSchema = new mongoose.Schema({
SESSION_ID: {
type: String,
required: false,
},
CARD_NUMBER: {
type: String,
required: false,
},
TERMINAL_ID: {
type: String,
required: false,
},
EXTERNAL_STAN: {
type: String,
required: false,
},
})
const csvSchema = new mongoose.Schema(
{
fileName: { type: String, required: true },
csvData: { type: [rowSchema], required: true },
NumberRecords: { type: Number, required: true },
},
{
timestamps: true,
}
);
csvSchema.pre("validate", function (next) {
this.NumberRecords = this.csvData.length;
next();
});
module.exports = mongoose.model("csvRecords", csvSchema);

Populate returns whole parent document

I just started learning express and mongodb. I recently faced the kind of problem, I'm trying to select all the subdocuments which are inside of Room model.
const books = await Room.find().populate('book');
But it returns whole room document when i want to select only bookings field.
Here's the book schema
const bookSchema = new mongoose.Schema({
startDate: {
type: Date,
required: true,
},
endDate: {
type: Date,
required: true,
},
name: {
type: String,
required: true,
},
phone: {
type: String,
required: true,
},
});
module.exports = mongoose.model("book", bookSchema)
And here's the room schema
const roomSchema = new mongoose.Schema({
currentlyReserved: {
type: Boolean,
default: false,
},
people: {
type: Number,
required: true,
},
roomNumber: {
type: Number,
required: true,
},
pricePerPerson: {
type: Number,
required: true,
},
reservedUntil: {
type: Date,
default: null,
},
reservedBy: {
type: String,
default: null,
},
bookings: {
type: [{ type: mongoose.Schema.Types.ObjectId, ref: "book" }],
},
});
module.exports = mongoose.model("room", roomSchema);
You can project with with second arg to find().
https://docs.mongodb.com/manual/reference/method/db.collection.find/#projection
const books = await Room.find({}, {bookings: 1}).populate('bookings');

The opeartor set doesn't work in mongodb. Or any other methods to add a new field into the document?

The operator $set can't add a new field hide. I thought, I was doing everything according to the mongodb official documentation. Can somebody tall me what I'm doing wrong. Thanks
createClass
.updateMany(
{ classname: req.body.className },
{ $set : {"hide":true}},
{ multi: true, upsert: false },
)
Here is the schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Classes = new Schema({
classname: {
type: String,
required: true,
},
Subject: {
type: String,
required: true,
},
Chapter: {
type: String,
required: true,
},
Topic: {
type: String,
required: true,
},
SubjectimgPath: {
type: String,
},
ChapterimgPath: {
type: String,
},
TopicimgPath: {
type: String,
},
content: {
type: Array,
}
});
Classes.index({ "$**": "text" });
const Createclass = mongoose.model("AllClasses", Classes);
module.exports = Createclass;
Try this :
const Classes = new Schema({
classname: {
type: String,
required: true,
},
Subject: {
type: String,
required: true,
},
Chapter: {
type: String,
required: true,
},
Topic: {
type: String,
required: true,
},
SubjectimgPath: {
type: String,
},
ChapterimgPath: {
type: String,
},
TopicimgPath: {
type: String,
},
content: {
type: Array,
}
}, {strict :false} );

Resources