I want to join two schemas into one because they both have almost same attributes and i want to use it for PostSchema that a user and also an owner can create and show their posts but i dont know how to achieve it
for example :
UserSchema
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
gender: String,
role: {
type: String,
default: "User",
},
});
OwnerSchema
const OwnerSchema = new mongoose.Schema({
name: String,
gender: String,
email: String,
password: String,
role: {
type: String,
default: 'Owner'
},
restaurant: RestaurantSchema
})
PostSchema
const PostSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
photo: {
type: String,
required: true,
},
postedBy: {
type: ObjectId,
ref: *Combined user and owner*,
},
comments: [CommentSchema],
}, { timestamps: true });
Get rid of your OwnerSchema - and modify your UserSchema to something like the below:
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
gender: String,
role: {
type: String,
default: "User", //could be Owner when applicable
},
restaurant: RestaurantSchema, //could be null when role=User
});
Related
Here is my very basic product schema:
const productSchema = new Schema({
productName: {
type: String,
required: true,
},
productDescription: {
type: String,
},
productPrice: {
type: Number,
},
});
module.exports = mongoose.model("Product", productSchema);
These products are listed out and a user can add a quantity for each. I am storing in an array of objects as per below. I want to join these two collections together so that I can output the qty of products selected by the user. I believe I need to use populate here but not sure how to set up the Refs and so on.
const PartySchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
catering: [{ id: mongoose.Types.ObjectId, qty: Number }],
created_at: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Party", PartySchema);
I'm sharing this solution with the assumption that the catering field is the sub-document array pointing to the Product Schema:
The Product Schema is fine so it stays the same (although to keep to convention I would advice naming your schema 'Products' instead of 'Product', Mongo Naming Covention):
const productSchema = new Schema({
productName: {
type: String,
required: true,
},
productDescription: {
type: String,
},
productPrice: {
type: Number,
},
});
module.exports = mongoose.model("Products", productSchema);
And next the Party Schema would be:
const PartySchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
catering: [{
id: {
type: mongoose.Types.ObjectId,
ref: 'Products',
},
qty: {
type: Number,
}
}],
created_at: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Parties", PartySchema);
Im new using node js , what im trying to do is creating an attribute that contains a list of projects in the userSchema so i can display the projects when the user log in in my electron app
this is my userschema :
var UserSchema = new Schema(
{
username: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
nom: {
type: String,
required: true,
},
prenom: {
type: String,
required: true,
},
verif_code: {
type: String,
required: false,
},
}, { timestamps: { createdAt: 'created_at' } });
and this is my projectSchema :
var ProjectSchema = new Schema(
{
description: {
type: String,
},
useremail: {
type: String,
},
imageurl: {
type: String,
},
var UserSchema = new Schema(
{
username: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
nom: {
type: String,
required: true,
},
prenom: {
type: String,
required: true,
},
verif_code: {
type: String,
required: false,
},
projects: [{type: Types.ObjectId, ref: 'projects'}],//projects is name of table in database
}, { timestamps: { createdAt: 'created_at' } });
I am new to the backend and trying to learn by building some stuff but unfortunately, I got stuck.
I want to know if I can update a nested array of objects in Users Schema using Mongoose in an efficient and elegant way.
Users Schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
required: true
},
username: {
type: String,
required: true,
unique: true
},
email: {
type: String,
required: true,
unique: true
},
gender: {
type: String,
required: true
},
password: {
type: String,
required: true
},
friends: [{}],
notifications: []
}, {timestamps: true});
module.exports = User = mongoose.model('user', UserSchema);
In the friends' field, I stored friend request with the status of pending
I want if the user whose the request was sent to, hits an endpoint, to accept the request
by changing the status from pending to success.
This is how a friend request was stored:
friendRequest = {
_id: req.user.id,
status: 'pending',
sentByMe: false,
new: true,
inbox: []
}
Thanks as you help me out!!! 🙏🙏🙏
You should first create an additional friendRequest and inbox schemas like this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
const InboxSchema = new Schema({
user_id: {
type: String,
required: true
},
from_id: {
type: String,
required: true
},
message: {
type: String,
required: true
},
the_date_time: {
type: Date,
required: true
}
});
mongoose.model('Inbox', InboxSchema);
const FriendRequestSchema = new Schema({
user_id: {
type: String,
required: true
},
status: {
type: String,
required: true
},
sentByMe: {
type: Boolean,
required: true,
unique: true
},
inbox: [InboxSchema]
})
mongoose.model('FriendRequests', FriendRequestSchema);
and update your Users schema:
const UserSchema = new Schema({
name: {
type: String,
required: true
},
username: {
type: String,
required: true,
unique: true
},
email: {
type: String,
required: true,
unique: true
},
gender: {
type: String,
required: true
},
password: {
type: String,
required: true
},
friends: [FriendSchema],
notifications: [FriendRequestSchema]
}, {timestamps: true});
And then use the friendRequest object
friendRequest = {
_id: req.user.id,
status: 'pending',
sentByMe: false,
new: true,
inbox: []
}
to update the Users collection
Users.update({ _id: user_id }, { $push: { notifications: friendRequest } });
Whenever you have arrays of objects within collections, its best to define additional schemas. You should also consider adding indexes to your collection schemas.
Update:
A FriendSchema would look like this:
const FriendsSchema = new Schema({
friend_id: {
type: String,
required: true
},
friend_name: {
type: String,
required: true
},
friendship_made: {
type: Date,
required: true
}
// you have to define FriendSchema before you define Users since you
// now reference [FriendSchema] in UserSchema
mongoose.model('Friends', FriendSchema);
And so is personA friends with personB?
Users.findOne({ "_id": personA.id, "friends.friend_id": personB.id});
How to have user and profile together?
This is how User schema defined:
const UserSchema = new Schema({
email: { type: String, required: true },
name: { type: String, required: true },
password: { type: String },
picture: { type: String },
});
and Profile schema:
const ProfileSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' },
setting1: { type: String, enum: ['YES', 'NO', 'UNKNOWN'], default: 'UNKNOWN' },
setting2: { type: String, enum: ['YES', 'NO', 'UNKNOWN'], default: 'UNKNOWN' },
setting3: { type: String, enum: ['YES', 'NO', 'UNKNOWN'], default: 'UNKNOWN' },
});
How can I retrieve in one command/line the profile when I ask for user?
User.findOne({ email: 'blabla#gmail.com' })
I have try populate but it is not works:
User.findOne({ email: 'blabla#gmail.com' }).populate('profile')
Please notice: I don't want to have profile in user in the same schema.
You would need to link the Profile inside the UserSchema:
const UserSchema = new Schema({
profile: { type: Schema.Types.ObjectId, ref: 'Profile' },
email: { type: String, required: true },
name: { type: String, required: true },
password: { type: String },
picture: { type: String },
});
I have two models. The first one is UserSchema and the second one is CategorySchema
var UserSchema = Schema({
firstName: {
type: String,
required: true
},
secondName: String,
lastName: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
status: {
type: String,
required: true
},
roles: [{
type: Schema.ObjectId,
ref: 'Role'
}],
publications: [{
title: {
type: String,
},
description: String,
status: {
type: String,
},
createdAt: {
type: Date
},
updatedAt: {
type: Date,
default: Date.now()
},
pictures: [{
name: String
}],
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}],...
the model category is
var CategorySchema = Schema({
name: String,
subcategories: [{
name: String
}]
});
UserSchema has publications. Publications contains an array. Within of publications is categories that contains an array of id of subcategory(subcategory is whithin of CategorySchema)
the problem is when I need to populate categories of UserSchema. Categories of UserSchema have an array of _id of subcategory that belongs to CategorySchema.