I am trying to reference a schema property named grade in my model to another model's id. But there are some instance where I would like to keep it as null. I tried to keep the default as null and tried but I get the following error:
ValidationError: User validation failed: grade: Cast to Array failed for value "" at path "grade"
The following is my code:
import * as mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
username: String,
email: { type: String, unique: true, lowercase: true, trim: true },
password: String,
school: { type: String, default: null },
mobile: Number,
grade: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Grade', default: null }],
role: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Role' }],
company: { type: String, default: null },
designation: { type: String, default: null },
active: { type: Number, default: 2 },
url: { type: String, default: null },
admin: { type: Number, default: 0},
created: { type: Date, default: Date.now },
last_active: { type: Date, default: Date.now }
});
const User = mongoose.model('User', userSchema);
export default User;
Is there a way I can keep default values as null though referencing it?
In MongoDB or NoSQL databases there is no need to make a default value as NULL as it is based on Schemaless architecture however in relational databases you can do that. as long as you don't have a value for the field this column should not be there at all for this row.
Check below those example for 2 rows of your schema:
{username: "jack", email: "jack#gmail.com", password: "123"}
{username: "msoliman", email: "msoliman#gmail.com", password: "123", school: "AB"}
you notice the first row doesn't have school at all, this is exactly the same as you save in relational database school as NULL.
I hope this helps you understand what I mean, if not please leave a comment. if it helps please don't forget to rate my answer
The NoSQL is defined in such way if the values are available so it will store in schema otherwise it won't consider it. So If you want to define your keys which might have or haven't values take them as you have taken the role in your above schema else required you can take it as
default: '',
school: { type: String, default: '' }
Related
In Node, Mongoose I am trying to achieve something like Laravel's created_by and updated_by columns, these 2 columns are automatically updated by the ORM in every INSERT and UPDATE operation to that particular table.
For example, below is the Material schema, here I want to store the createdBy and updatedBy column with userId whenever any user performs INSERT and UPDATE operations respectively in the materials collection.
I know this can be done while saving the records in particular controller method, but every time in every method I will have to add this and it becomes a pain, so I am looking for a solution where I can define something in the Model itself and it does the job just like the mongoose-delete package.
const mongoose = require('mongoose')
const mongoose_delete = require('mongoose-delete')
const materialSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
type: {
type: String,
required: true,
enum: ['hard', 'soft'],
},
image: {
type: String,
default: null
},
active: {
type: Boolean,
default: true
},
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
default: null
},
updatedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
default: null
}
}, { timestamps: true })
materialSchema.plugin(mongoose_delete, { deletedAt : true, deletedBy : true, deletedByType: String })
const Material = mongoose.model("Material", materialSchema)
module.exports = Material
I need to query documents from two collections together on mongoose.
I am familiar with SQL query and but not familiar with mongoDB.
I have two schema for Users, Messages like following.
Users
const UserSchema = new mongoose.Schema({
name: String,
email: {type: String, unique: true},
password: String,
avatar: {type: String, default: ""},
created_at: { type: Date, default: Date.now() }
});
module.exports = mongoose.model('User', UserSchema);
Messages
const MessageSchema = new mongoose.Schema({
message: { type: String, default: "" },
from: { type: String, default: "" },
to: { type: String: default: "" },
is_read: { type: Boolean, default: false },
channel: { type: String, default: ''},
created_at: { type: Date, required: true, default: Date.now }
});
module.exports = mongoose.model('Message', MessageSchema);
I need to get messages with "is_read" is "false".
I want to get "user name" and "avatar" together.
The "from" value of message should be matched with "_id" of User.
I think this post sums it up well: Mongoose - query to get data from multiple collections
Specifically the second upvoted answer mentions similarities between sql and mongodb, and goes on to explain how to link collections in mongoose queries.
I have a schema "Questions" it has like a dozen of questions in it, I can add and delete those questions, I need this collection reflected in a field of other collection - "User" with one additional field (nested in options).
Question Schema:
var QuestionScema = new mongoose.Schema({
key: { type: String, required: true },
label: { type: String, required: true },
name: { type: String, required: true },
page: { type: String, required: true },
type: { type: String, required: true },
options: [{
key: {type: String, required: true},
value: {type: String, required: true}
}],
});
User Schema:
var UserSchema = new mongoose.Schema({
Name: { type: String, required: true },
Email: { type: String, required: true, unique: true },
Password: { type: String, required: true },
//this is where I need to reflect a Questions collection on each user,
//so that it will look something like this//
Questions: [{
key: {type: String, required: true},
//here can be all other fields from Questions collection, that is not a problem
options: [{
key: {type: String, reuired: true},
value: {type: String, reuired: true},
counter: {type: Number, default: 0} //this is the additional field
}]
}],
//
Notifications: [{
Title: { type: String },
Data: { type: String },
Created: { type: Date, default: Date.now }
}]
});
I can't figure out how to do that.
I have another collection of users, say User2 that will answer those questions from Questions collections and I need to keep track on Users schema (not User2, there I just save questions and answers) of how many times an option for that question is chosen.
A Questiuons entry can look like this:
{
key: Haveyouseenthismovie,
label: Have you seen this movie?,
name: Have you seen this movie?,
page: 1,
type: dropdown,
options: [{
key: yes,
value: yes
}, {
key: no,
value: no
}]
}
I want it to work like that (reflect a collection in field of each User) so I don't have to check if that question is in User collection if not add and if it is, is there an option that I need if it is than increment, if not than add that option (that user selected from options in that question in Questions schema) and increment. That looks like a bummer. So I figured that it will be better if that field will reflect a collection and I will just increment the option that I need on a question that I need.
Please help me figure that out, I don't have enough practise in mongo so I struggle with it sometimes :)
I don't think there is a way to reflect a collection in another document as the way you seem to wish it.
As I understand, the following options are available for you:
Embed the entire question document inside the User documents in User Collection.
Just maintain the '_id' of the question document in the User document in User Collection.
Please read on Data Modelling concepts & maintaining relationship between documents from Mongo DB Page https://docs.mongodb.com/manual/applications/data-models-relationships/
I have this mongoose schema, I added updated_by and created_by, but for some reason when I save models from client to server, those fields aren't visible:
userSchema = mongoose.Schema({
role: {
type: String,
enum: ['Admin', 'Owner', 'User']
},
username: {
type: String,
unique: true,
required: true,
validate: [validation.usernameValidator, 'not a valid username']
},
passwordHash: {
type: String,
required: true,
validate: [validation.passwordValidator, 'not a valid password']
},
email: {
type: String,
unique: true,
required: true,
validate: [validation.emailValidator, 'not a valid email address']
},
firstName: {
type: String,
required: false
},
lastName: {
type: String,
required: false
},
registered_at: {
type: Date,
default: Date.now
},
created_by: {
type: String,
required: false
},
updated_by: {
type: String,
required: false
},
created_at: {
type: Date,
default: Date.now
},
updated_at: {
type: Date,
default: Date.now
}
},
{
autoIndex: false
});
is this normally a problem? Do I have to somehow rebuild something with Mongoose or MongoDB in order for them to pick up the new properties on this model?
Of course, I did restart the mongoDB server, but that didn't do anything.
In any case, if you save your User model, the fields with actual values shown in MongoDB will be the ones you set a value for yourself when saving the model OR the fields with a default value set in your userSchema.
So, just to clarify on this:
address: { type: String, default: ' ' }
will be shown in MongoDB with a value of ' ' unless you set a specific address when saving your User model.
But,
address: String
will NOT be shown in MongoDB unless you set a specific address when saving your User model.
EDIT
Thanks to Matthew for pointing it out, actually upsert behavior is indeed the following:
If upsert is true and no document matches the query criteria, update() inserts a single document.
I have two base schemas User and Course
//User Model
var UserSchema = new Schema({
userId: {
type: String,
default: '',
required: 'Please provide UserId.',
index : true
},
Name :{
type: String,
default: '',
trim : true
}
});
//Course Schema
var CourseSchema = new Schema({
title: {
type: String,
default: '',
required: 'Please fill course title',
trim: true,
index: true
},
description: {
type: String,
default: '',
trim: true
},
category: {
type: String,
ref: 'Category',
index: true
}
});
And a UserCourseProgress schema for storing each user's course progress.
var UserCourseProgress= new Schema({
userId: {
type: String,
ref:'User',
required: 'Please provide the User Id',
index:true
},
courseid: {
type: Schema.Types.ObjectId,
ref: 'Course',
required: 'Please provide the Course Id',
index: true
},
timespent: {
type: Number, //seconds
default: 0
},
score: {
type: Number
}
});
Now I have to aggregate results such over UserCourseProgress schema on Course level such that i show my result like this :
- {Course Name}( {No_Of_Users_taken_Course} ) [e.g. Course A (15) ]
History.aggregate([{
$group: {"_id": {"primary": "$courseid"},
"popularityCount": {$sum: 1}}},
{$sort:{"popularityCount":-1}},
{$project:{"_id":0,
"courseid":"$_id.primary",
"count":1}}
])
Now want to populate Course title from Course Schema through its reference, but after going through the list of aggregation operators(here).I am not able to figure out a way to do so.
Can you suggest any way to populate data from other collections ??