I have been able to have a correct schema validation with the following rules :
Only the expected field should be saved (hence the strict mode)
The User.Account should be nullable. If null, the required fields are not preventing the schema from being saved.
If User.Account is not null, The validation rules to be respected (.password and .email required).
I do NOT want to create an AccountSchema because it pollutes the account with an _id.
I have been able to do this with 2 schemas (UserSchema and AccountSchema). But as mentioned, the problem is that it creates a _id field in the User.Account field.
const AccountSchema: Mongoose.Schema = mongoose.Schema({
password: { type: String, required: true },
email: { type: String, required: true }
}, {
strict: 'throw',
useNestedStrict: true
});
const UserSchema: Mongoose.Schema = mongoose.Schema({
name: { type: String, required: true },
account: AccountSchema
}, {
strict: 'throw',
useNestedStrict: true
});
Here is a solution with only a UserSchema but the field strict mode and validation are not working as expected.
const UserSchema: Mongoose.Schema = mongoose.Schema({
name: { type: String, required: true },
account: {
type: {
password: { type: String, required: true },
email: { type: String, required: true }
},
required: false
}
}, {
strict: 'throw',
useNestedStrict: true
});
Could anyone let me know what I am missing here?
By the way, I tried custom validation which does work, but it does not guarantee the object schema safety that strict mode should ensure.
Thanks
Related
i want to add batch,department,stream,id fields to the schema depending if usertype is “Student”,i do not understand why,but sometimes it adds the fields sometimes not.forexample if i created a user with usertype “Student” first it does not add the fields,but after that when i created a user with usertype “Teacher” or “Admin” it asks me the fields are required meaning it add the fields and this happens vice versa.why any way to fix this issue?please help guys.
what i have tried well,i have asked chatGpt but no answers i mean just the answers do not solve the issue.
here is the code
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema(
{
fullName: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
userType: {
type: String,
required: true,
},
phoneNumber: { type: String, required: true },
password: { type: String, required: true },
approved: { type: Boolean, default: true },
},
{
timestamps: true,
}
);
userSchema.pre("validate", function (next) {
if (this.userType === "Student") {
this.constructor.schema.add({
batch: {
type: Number,
required: true,
},
department: {
type: String,
required: true,
},
stream: {
type: String,
required: true,
},
id: { type: String, required: true }, //, unique: true
});
}
next();
});
const userModel = mongoose.model("users", userSchema);
module.exports = userModel;
I am trying to post something to mongodb, my models file looks like this and i have input all of the required fields in insomnia but for some reason it's still not working.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const contactSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true },
phoneNumber: { type: Number, required: true },
message: { type: String, required: true },
date: { type: Date, required: false },
});
const Contact = mongoose.model('Contact', contactSchema);
module.exports = Contact;
I am still new to the concept of non-relational databases.I am using MongoDb with atlas in a node application.My collections have a lot of embedded documents, for example this mongoose schema
const feedbackSchema = new Schema({
member: {
type: userSchema,
required: true
},
datePosted: {
type: Date,
default: Date.now()
},
feedbackText: {
type: String,
required: true
}
});
const UserSchema = new Schema(
{
type: {
type: String,
required: true
},
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
userData: {}
},
{ strict: false }
);
member (type userSchema) is another collection.So my question is if a member in the member collection is updated,do I have to manually update this member in the feedback collection?
I had defined mongoose schema and i tried to insert data into mongodb.But it is not inserted as per defined schema
export const EmpSchema: mongoose.Schema = new Schema({
name: {
type: String,
required: true
},
empNo: {
type: String,
required: true
},
skill: {
type: [String],
required: true
},
address: {
type: String,
required: true
}
}, {
_id: false,
versionKey: false,
retainKeyOrder: true
});
It is getting stored like Array elements as last field.like
name
empno
address
skill
export const EmpSchema: mongoose.Schema = new Schema({
name: {
type: String,
required: true
},
empNo: {
type: String,
required: true
},
skill: {
type: [String],
required: true
},
address: {
type: String,
required: true
}
}, {
_id: false,
versionKey: false,
retainKeyOrder: true
});
YOUR SCHEMA DEFINATION IS CORRECT PLEASE CHECK THE CONTROLLER PART WHERE YOU PUT THE INSERT QUERY . THERE MATTERS A INSERTION ORDER
I have the following schema in mongoose that I would like to have as a nested array in another schema:
var NestedSchema = new Schema({
name: { type: String, required: true }
});
Some other schema that needs and array of the nested schema.
var EventSchema = new Schema({
name: { type: String, required: true, unique: true },
fields: [NestedSchema]
});
Which works just fine. However now I want to run some validation against that array.
var validators = // some validators
var EventSchema = new Schema({
name: { type: String, required: true, unique: true },
fields: [{ type: 'NestedSchema', required: true, validate: validators }]
});
Of course type: 'NestedSchema' does not work, it was a shoot in the dark. Does mongoose allow you to have an array of object based on schema
Using:
MongoDB shell version: 3.2.12,
Mongoose : 4.4.7
I managed to have an array based on schema that way:
var NestedSchema = new Schema({
name: { type: String, required: true }
});
var EventSchema = new Schema({
name: { type: String, required: true, unique: true },
fields: [{ type: [NestedSchema], required: true }]
});
Did not try validation, but I believe it will work as required works well. :)