Mongodb Schema using mongoose - mongodb

This is my user schema :
const userSchema = new Schema({
email: String,
username: String,
password: String,
secretToken: String,
active: Boolean,
type: String
}, {
timestamps: { // this will give us the detail when the account is created
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
this is my requirement Schema:
const requirementSchema = new Schema({
name: String,
age: String,
class: String,
subject: String,
email: String
}, {
timestamps: { // this will give us the detail when the requiremnt is created
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
how can I bring id from user schema to the requirement schema as secondary key?

I think mongodb is not meant for relationship. I could led to very bad practice if your database grow. But you can try https://docs.mongodb.com/manual/reference/database-references/#DatabaseReferences-DBRef for Database References.
There is no primary key foreign key concept in Mongodb. and normalization is also discouraged in mongodb.

Related

MongoDB Reference ObjectID from the same table

I want to do a category table that have a parent category by referencing the ObjectId from the same table.
Is it a normal practice or bad practice to refer ObjectId from the same table? If this is wrong, how do I go about it?
const CategorySchema = new Schema({
title: String,
description: String,
thumbnail: String,
image: String,
parentCategory: {
type: Schema.Types.ObjectId,
ref: 'category',
},
})
module.exports = mongoose.model('category', CategorySchema)

MongoDB - will the collection referencing another collection be also updated when referenced collection updates and vice versa?

If I update a field in collection B that is referenced by collection A, will collection A update too? Also vice versa?
collection A:
const userSchema = new mongoose.Schema({
email: String,
userName: String, //lets say I update username
password: String,
favoriteBooks: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Book'
}
})
collection B:
const bookSchema = new mongoose.Schema({
name: String,
reviews: [String] //lets say I update or add review
})
If I update a subdocument:
collection A:
const userSchema = new mongoose.Schema({
email: String,
userName: String, //lets say I update username
password: String,
favoriteBooks: [BookSchema]
})
collection B:
const bookSchema = new mongoose.Schema({
name: String,
likesCount: Number
})
Thanks

how to database design for user authentication like student, teacher , super admin

I am trying to create different types of registration for user . I have got three collection for users . I have been references user collection in both of teacher and student because I need to get email and password.
If a teacher register including email, password, firstname , lastname etc , there is a collection .
if a student register including email, password, firstname , lastname etc , there is another collection .
our all of email and password will be one collections
user - table/collection
- email : test#gmail.com
- password: asdfasdf
student - table /collection
- firstname: 'sujon"
teacher - table/collection
- firstname: "sujon"
const UserSchema = mongoose.Schema({
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
})
const StudentSchema = mongoose.Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user',
},
firstname: {
type: String,
},
lastname: {
type: String,
},
photo: {
type: String,
},
education: {
type: String,
},
birth: {
type: Date,
},
sex: {
type: Boolean,
},
})
const TeacherSchema = mongoose.Schema({
user: {
type: mongoose.Schema.ObjectId,
ref: "user"
},
firstname: {
type: String
},
lastname: {
type: String
},
photo: {
type: String
},
designation: {
type: String
},
birth: {
type: Date
},
sex: {
type: Boolean
}
});
how can implement database design
Creating a single User schema would be fine. You can have a single schema with all properties (since all three types of user have almost same properties) and then add a 'roles' field like this:
roles: [{ type: String }]
The roles field can have multiple roles [ 'Teacher', 'Student' ... ]. In this way a single user can have multiple roles for example a Teacher can also be an admin etc.
Also you won't have to create a new model whenever a new role is introduced.
Users can be queried based on their roles. The roles can then be used for authentication as well for example a user with role 'Teacher' can create assignment or a user with role 'Student' can submit assignments. When registering a user you can set some sort of model validation on the api or client side to accept a certain model.

How create nested mongoDb schema in mongoose?

i'm trying to design mongoose shema for users cleaner and customer, they have some common fields e.g. name, but also have extra (different fields) client(rating) and customer number. I'm not sure that my design is good.
I've created separate userSchema for customer and cleaner, and created separate address schema.
// User Schema
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
}
});
// AddressSchema
const AddressSchema = new mongoose.Schema({
city: {
type: String,
required: true
},
street: {
type: String,
required: true
}
});
// CustomerSchema
const CustomerSchema= new mongoose.Schema({
name: UserSchema,
address: AddressSchema
});
// CleanerSchema
const CleanerSchema= new mongoose.Schema({
name: UserSchema,
rating: {
type: Number,
required: true
}
});
My schema doesn't work. Could you give best practice example for my schema?
This is how I would define your Customer and Cleaner schemas. I don't think it's necessary to create the separate name and address schemas.
const CustomerSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
address: {
city: {
type: String,
required: true
},
street: {
type: String,
required: true
}
}
});
const CleanerSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
rating: {
type: Number,
required: true
}
});

How to set a expires for a subdocument in mongoose

This is my schemes:
var authUserScheme = mongoose.Schema({
token: String,
ip: String,
valid: {type: Date, default: Date.now(), expires: '1m' },
}, {_id: false});
var usersSchema = mongoose.Schema({
// OTHER THINGS
auth : [ authUserScheme ],
// other things
});
When i set an 'auth' path, mongodb deletes the entire document, but i want to delete only the auth row when expire date... It is possible?
Sorry for my english, i speak spanish.
You can't use a TTL index to delete a portion of a document on expiry.
However, it looks like your authUserScheme is really more of a session concept than an embedded document.
A better approach would be to use a reference from the authUserScheme to the related user, eg:
var authUserSchema = mongoose.Schema({
token: String,
ip: String,
valid: {type: Date, default: Date.now(), expires: '1m' },
user: { type: Number, ref: 'User' }
});
var userSchema = mongoose.Schema({
name: String,
// Other fields
})
var AuthUser = mongoose.model('AuthUser', authUserSchema);
var User = mongoose.model('User', userSchema);