Looking for Mongoose Schema suggestions for individual models - node.js

I am in the process of developing a REACT app that would allow a user to find a plant based on their homes environment (long term goal). For my MVP I want to have the user create an account, name their home, add individual rooms with lighting conditions and add plants to each of those rooms. In the event the user does not create any rooms the plants would just sit under their home rather than a specific room by default.
I am struggling on how to design my models and am looking for some suggestions. This will only be my second project on my own so my experience is quite limited.
Currently I have the following models; (attached is a screenshot of each of these models)
plantScheme w/ commonName, scientificName, lightLevel, careLevel, waterReq, etc - This will be seeded with around 100 of the most common household plants to begin with and I intend to use the database to display varying plants on the site whether or not a user is logged in. I also want to link these plants to a specific user when the plant is added to their home.
userSchema w/ username, email, password, home { type: String, required: false, default: 'My Home' } (This begins with where I am open to suggestions. I am not sure if this is the most effective way of establishing the users home), rooms: [ Room.schema ] (pulls from a separate model), plants: [ UserPlant.schema ] (also pulls from a separate model to keep track of plant IDs and room IDs I THINK - haven't tested yet)
userPlantSchema w/ plant_id: { type: Schema.Types.ObjectId, ref: 'Plant', required: true } and room_id: { type: Schema.Types.ObjectId, ref: 'Room', required: false }
and lastly..
roomSchema w/ roomName and lightLevel
If there is a better way of setting up these models and/or keeping track of the users plants I would greatly appreciate the help. Thank you for your time.
screenshot of each model and their established relationships

Related

Notification Schema Structure in MongoDB-NodeJS

I am developing notification process & technology I am using is MongoDB & NodeJS(Express)
Please do let me know how to design schema for this notification process.
What are the collections & documents will be defined.
Thanks
After researching a lot, I wrote my schema in a flowing way. It has helped me to keep scalability in my application and do the best solution for multiple notification features.
NotificationSchema = new Schema({
sender: {type:mongoose.Schema.Types.ObjectId, ref:'User'}, // Notification creator
receiver: [{type:mongoose.Schema.Types.ObjectId, ref:'User'}], // Ids of the receivers of the notification
message: String, // any description of the notification message
read_by:[{
readerId:{type:mongoose.Schema.Types.ObjectId, ref:'User'},
read_at: {type: Date, default: Date.now}
}],
created_at:{type: Date, default: Date.now},
});
Why this looks best notification schema to me:
Let’s explain the schema, I wrote here and what things can be done with this solution.
Sender: I have to keep the sender field as an object, the object is referred to as a user collection for the population. Because the notification happens generally for a single action, I mean it may create from the activity of a single person. So, the sender is who is sending this notification, the source of the notification.
Receiver: I have kept the receiver field as an array of objects and the object is referred to user collection if needed for the population. The receiver can be one person or many persons. So, the receiver field will hold the IDs of the recipients.
Message: message field is used for holding the notification message details.
Read_by: the read_by field is an array of two object properties. this object contain readerId(user-id) and read_at (the seen date-time). The read_by array reduced many works and has given more advanced options to work with. It will be used to track each recipient’s view time to track. So, the notification sender can track which recipient has read the message at which time.
Created_at: this field simply holds the notification creation date and time.
So, this schema is very helpful to use for making notifications from one to one and one to many. That’s why I have considered it as the best notification schema for the MongoDB database.

Node.js Mongodb Two bookings on same time from two different users. only one can book

Currently in my application, users can make a booking for time XYZ, assuming that time XYZ is available. Suppose that another user is also booking for XYZ at the same time as the first user. I want to ensure that if they hit the particular API at the same time only one can have time XYZ and not both. Currently, when a user books a time, I first check my database to see if there is any booking for this particular time, and if there isn't the user gets that time. How can I make sure that if two users book at once, only one gets that time?
Create a unique index, something like this:
db.booking.createIndex( { "time": 1 }, { unique: true, sparse: true } )

design noSQL database mongoose nodejs

I build booking appointments barbershop app in react native with node js mongoose.
in short, each user can make an appointment to barber. each barber has some types of hair cuts types.
user - Appointment (many to many) -> should I need to add appointment array in user?
Appointment - users (many to many) -> ref: user_id.
Barber- Appointment (many to many)-> should I need to add appointment array in user?
Appointment - Barber(many to many) ref: barber_id
Barber- HairCut Types(many to many): ref: types array
that's my demo design
I would like to know if the db design looks right? do I need to save appointments array in user and barber also?

Should I build one or two mongodb models?

I am building a mean stack app allowing people to read and write reviews about products.
Reviews will be classified by products on the website : A user will open the page displaying the list of products reviewed on the website and see the name of each products. When clicking on a product, the user is directed to a new page displaying all the reviews about the product.
I was set on building just one model gathering all reviews :
var ReviewSchema = new mongoose.Schema({
productName: String,
productCreator: String,
review: String,
upvotes: {type: Number, default: 0},
author: String
});
The page listing all products would get all reviews from the data base and then only display the productName and productCreator after eliminating all duplicates.
{{productName}} - {{productCreator}}
But it's probably not efficient at all. Would it be better to build two different models, one for the reviews and one for the products ?
I'm sorry if my question is stupid but I'm still learning about MEAN and don't know much about non-relational db, that's my first project ;)
I have built two models that I have embedded. It takes a little more time when writing all the rest routes, but it will simplify both the back and front in the end.

filter/search function in meteor

I'm looking to implement a search/filter function. It will use a drop down bar as its inputs (so we the user chooses from a set of options).
How my system works now is this: There is a Meteor.users collection, and an Instruments collection. Basically, each user has a list of instruments he plays (for example, Bob plays piano and violin, Alice plays the piano and the flute). In this case, there are 4 objects in the Instruments collection:
1) type: Piano
owner: (bob's ID)
2) type: Violin
owner: (bob's ID)
3) type: Piano
owner: (Alice's ID)
4) type: Flute
owner: (Alice's ID)
So what I'm trying to do is, when I choose "piano" on my drop down list, and then click search, I want it to display Bob and Alice's profiles. I guess this should be done using Meteor's publish feature, but just not sure how to do it (I'm a beginner here).
For example, if how do I tell that specific publication to run when I hit search? I see examples of people setting publish rules to only publish the subset they are searching for, but how do we tell it when to happen? Do we use another template for that so that when we click "search", the URL path changes and thus a different template?
To be clear, my question is just how should I go about doing this. Thanks!
To make it easy, try to bind the instrument ID in Person's Collection as Array,
[{
_id: 'BoBId',
Instrumets: [PianoId, ViolinId],
},{
_id: 'AliceId',
Instrumets: [PianoId, ViolinId],
}];
now you could find any profile, who has the instruments
Person.find({
Instruments:{
$in:[InstrumentsId]
}
});

Resources