How should I store page and subpage in collection with mongoose - node.js

I'm trying to create simple CMS with the ability to add new pages to the site. I have currently created a Collection called Pages:
var PageSchema = new Schema({
name_en: {
type: String,
required: true
},
name_nl: {
type: String,
required: true
},
slug_en:{
type: String,
required: true
},
slug_nl:{
type: String,
required: true
},
content_en: {
type: String,
required: true
},
content_nl: {
type: String,
required: true
},
visible: {
type: Number,
required: true,
default: 1
},
is_headMenu: {
type: Number,
require: true,
default: 0
},
is_footerMenu: {
type: Number,
require: true,
default: 0
},
date: {
type: Date,
default: Date.now
}
});
This collection store all pages, but after a few days I needed to add an option to also store sub-pages to existing pages.
I was thinking of creating an object inside of the Page Schema to hold all the sub-pages.
Like this:
var subPage = new Schema({
name_nl: {
type: String,
required: true
},
name_en: {
type: String,
required: true
},
slug_en:{
type: String,
required: true
},
slug_nl:{
type: String,
required: true
},
content_en: {
type: String,
required: true
},
content_nl: {
type: String,
required: true
},
visible: {
type: Number,
required: true,
default: 1
}
});
var PageSchema = new Schema({
name_en: {
type: String,
required: true
},
name_nl: {
type: String,
required: true
},
slug_en:{
type: String,
required: true
},
slug_nl:{
type: String,
required: true
},
content_en: {
type: String,
required: true
},
content_nl: {
type: String,
required: true
},
visible: {
type: Number,
required: true,
default: 1
},
is_headMenu: {
type: Number,
require: true,
default: 0
},
is_footerMenu: {
type: Number,
require: true,
default: 0
},
subPages: [
subPage
],
date: {
type: Date,
default: Date.now
}
});
But I cant use it because after creating a Page I'm required to fill the object 'subPage' because it's required. I don't want that. I want to add them whenever I want, and this way its not possible. How should I do it properly?

Related

Creating Two Foreign key mongoose inside a controller

Hi I want to implement this snippet of my class diagram using mongoose.
Here is my logic model in detail:
Here is the definition of my Mother model:
const mongoose = require('mongoose')
const motherSchema = mongoose.Schema({
cniMother: { type: Number, required: true },
name: { type: String, required: true },
surname: { type: String, required: true },
birthDay: { type: Date, required: true },
birthPlace: { type: String, required: true },
address: { type: String, required: true },
phone: { type: Number, required: true },
occupation: { type: String, required: true },
});
module.exports = mongoose.model('Mother', motherSchema);
Here is the definition of my Declared model:
const mongoose = require('mongoose')
const declaredSchema = mongoose.Schema({
name: { type: String, required: true },
surname: { type: String, required: true },
father: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Father',
//required: true,
},
mother: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Mother',
//required: true,
},
birthDay: { type: Date, required: true },
birthPlace: { type: String, required: true },
gender: { type: String, required: true },
nationality: { type: String, required: true }
});
module.exports = mongoose.model('Declared', declaredSchema);
I want to retrieve the name of the father and the mother corresponding to the declared.
Can anyone help me please?

How can i calculate daily, weekly or monthly sales from my MongoDB data?

Here is my orders schema with Javascript mongoose;
import mongoose from 'mongoose';
const orderSchema = new mongoose.Schema(
{
orderItems: [
{
name: { type: String, required: true },
qty: { type: Number, required: true },
image: { type: String, required: true },
price: { type: Number, required: true },
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product',
required: true,
},
},
],
shippingAddress: {
fullName: { type: String, required: true },
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: String, required: true },
country: { type: String, required: true },
lat: Number,
lng: Number,
},
paymentMethod: { type: String, required: true },
paymentResult: {
id: String,
status: String,
update_time: String,
email_address: String,
},
itemsPrice: { type: Number, required: true },
shippingPrice: { type: Number, required: true },
taxPrice: { type: Number, required: true },
totalPrice: { type: Number, required: true },
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
seller: { type: mongoose.Schema.Types.ObjectID, ref: 'User' },
isPaid: { type: Boolean, default: false },
paidAt: { type: Date },
isDelivered: { type: Boolean, default: false },
deliveredAt: { type: Date },
},
{
timestamps: true,
}
);
const Order = mongoose.model('Order', orderSchema);
export default Order;
And for the case of sellers how could I do the same for specific sellers.
I would like to create charts and graphs with that data so any help counts. Thank You!

Model.find is not a function mongoose

I have a mini casino game and have a mongoose model defined as following:
const jackpotSchema = mongoose.Schema({
round_id: {
type: String,
default: uniqid(),
required: true,
},
createdAt: {
type: Date,
required: true,
default: Date.now
},
closedAt: {
type: Date,
},
startedAt: {
type: Number,
},
roundSecret: {
type: String,
required: true,
},
random_org_hash: {
type: String,
},
random_org_obj: {
type: String
},
roundSecretHash: {
type: String,
},
winningPercentage: {
type: Number,
// required: true,
// first 5 characters of the secret hash, converted into decimal then divided by 10K
},
publicHash: {
type: String,
required: true,
// SHA-256 roundSecret + winningPercentage
},
finalHash: {
type: String,
},
winningTicket: {
type: Number,
},
winningDepositIndex: {
type: Number,
},
deposits: [{
uid: {
type: String,
required: true,
},
user: {
type: String,
required: true,
},
nonce: {
type: Number,
},
amount: {
type: Number,
required: true,
},
user_avatar: {
type: String,
},
ticketRangeMin: {
type: Number,
required: true,
},
ticketRangeMax: {
type: Number,
required: true,
},
referral: {type: String},
timestamp: {type: Date},
timestamp_readable: {type: String}
}],
total: {
type: Number,
required: true,
default: 0,
},
totalTickets: {
type: Number,
default: 0,
},
winner: {
type: String,
default: "",
},
winner_avatar: {
type: String,
},
active: {
type: Boolean,
default: true,
required: true,
},
open: {
type: Boolean,
required: true,
default: true,
},
roundTime: {
type: Number,
// default: potConfig.potLength,
default: 20000,
},
success: {
type: Boolean,
},
fee: {
type: Number,
default: potConfig.potFee,
}
})
module.exports = mongoose.model("Jackpot", jackpotSchema)
When running my app locally, I experience no errors.
However when running on a ubuntu production environment, I get the following error:
Jackpot.find is not a function
The stack trace says that the error is coming from db_utils.js
which looks like the following:
const Jackpot = require("./models/Jackpot");
...
async retrieveStats () {
var jackpots = await Jackpot.find().lean();
}
I have checked to see whether my module.exports was defined correctly, and it is. Not sure why this error is happening.
My local and production node versions match.
12.18.4
Don't you need to do
const jackpotSchema = new mongoose.Schema({...})
instead of
const jackpotSchema = mongoose.Schema({...})

Mongoose/MongoDB Empty SubSchema Array

I have a model in Mongoose that looks like this:
const orderLogSchema = mongoose.Schema(
{
removed: { type: String, required: true },
},
{
timestamps: true,
}
)
const orderSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
orderItems: [
{
name: { type: String, required: true},
qty: { type: Number, required: true},
image: { type: String, required: true},
price: { type: Number, required: true},
product: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Product'},
}
],
shippingAddress: {
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: String, required: true },
country: { type: String, required: true },
},
paymentMethod: {
type: String,
required: true,
},
paymentResult: {
id: { type: String },
status: { type: String },
update_time: { type: String },
email_address: { type: String },
},
taxPrice: {
type: Number,
required: true,
default: 0.0
},
shippingPrice: {
type: Number,
required: true,
default: 0.0
},
totalPrice: {
type: Number,
required: true,
default: 0.0
},
isPaid: {
type: Boolean,
required: true,
default: false
},
paidAt: {
type: Date,
},
isDelivered: {
type: Boolean,
required: true,
default: false
},
deliveredAt: {
type: Date,
},
couponCode: {
type: Object,
required: false
},
orderVerifyLog: [orderLogSchema],
}, {
timestamps: true
})
I need to be able to empty out the orderVerifyLog which is populated with the orderLogSchema. I have tried a bunch of things most recently I pulled the order into a variable, and I am able to access orderVerifyLog by using order.orderVerifyLog but I cant figure out how to empty out that orderLogSchema. I tried:
order.orderVerifyLog = []
order.save()
Is there an easy way to work with that "sub" schema and zero it out?
Basically I am pulling the log items from that schema for an order and displaying them on my front-end. Then I was to fire off an action to clears it out so they don't display every time the order is loaded (Only want them to show once). I also tired to loop over each item in the orderVerifyLog and use pull to remove them, but for some reason it always leaves one in there.

find according to referenced objectid

i have this schema:
var scheme = schema({
name: { type: String, required: true },
deleted: { type: Boolean, default: false },
user: {
type: ObjectId,
ref: 'User'
},
created: {type: Date, default: Date.now},
modified: {type: Date, default: Date.now},
eventinfo: { type: String, required: false },
street: { type: String, required: false },
city: { type: String, required: false },
country: { type: String, required: false },
postalcode: { type: String, required: false },
usecurrentlocation: { type: Boolean, required: false },
schedule_agenda_html: { type: String, required: false },
gspsetup_html: { type: String, required: false },
evdropboxstat: { type: Boolean, required: false },
evsponsors_html: { type: String, required: false },
evexhibitors_html: { type: String, required: false },
floorplan_html: { type: String, required: false },
social_media_fb: { type: String, required: false },
social_media_tw: { type: String, required: false },
social_media_li: { type: String, required: false },
evorganizers_html: { type: String, required: false },
coverscreen_img: { type: String, required: false },
logo_img: { type: String, required: false },
background_img: { type: String, required: false },
background_color: { type: String, required: false },
event_fromdate: {type: Date, default: Date.now},
event_todate: {type: Date, default: Date.now},
attendees_feedback: { type: Boolean, required: false },
social_network: { type: Boolean, required: false },
feature_icon_color: { type: String, required: false }
});
exports.EacEvent = mongoose.model("EacEvent", scheme);
i need to find all the EacEvents where user _id is equal to some thing like this (56a8a1eaf7a3289c10c9ed30) i have tried many things but either i am getting all the EacEvents or no EacEvent. thanks
Route File
here is my route file code which i am using to get the required results..
var models = require('../models')
,mongoose = require('mongoose')
var schema = mongoose.Schema;
var ObjectId = schema.ObjectId;
// var ObjectID = schema.ObjectId;
exports.myeventapps = function (req, res) {
var eac_userid;
if (req.user){
eac_userid = JSON.stringify(req.user._id);
console.log('================================' + eac_userid);
}
//models.EacEvent.find().exec(function(err, myevents) {
models.EacEvent.find({ deleted: false,'user._id':ObjectId(eac_userid)}).sort({ modified: -1 }).exec(function(err, myevents) {
res.locals.myevents = myevents;
console.log('================================' + myevents);
res.render('eac_myevent_apps', { title: "My Event Apps", myeventapps: myevents });
});
}
i have also tried comparing user._id in .populate() function but did not get the required results
Please try it, match user field directly, rather than user._id.
Refer to Mongoose Populate.
models.EacEvent.find({ deleted: false, 'user': ObjectId(eac_userid)}).sort(...
Maybe we can use the req.user._id directly
models.EacEvent.find({ deleted: false, 'user': req.user._id}).sort(...

Resources