How to use nested data in model class using NodeJS - node.js

I have data like ug.tutionfees andpg.tutionfees in MongoDB. How to implement this data in NodeJS model class and angular plz tell me.
My model class:
const mongoose = require('mongoose');
const UniversitySchema = mongoose.Schema({
worldranking:String,
countryranking:String,
universityname:String,
bachelorprogram:String,
masterprogram:String,
phdprogram:String,
country:String
},{collection:'us'});
const University =module.exports = mongoose.model('University',UniversitySchema);
I want to implement ug.tutionfees and pg.tutionfees in this model class.

If you have only these ug.tutionfees and pg.tutionfees are two different objects you can add into schema as below:
ug :{
tutionfees :{
type: String
}
},
pg :{
tutionfees :{
type: String
}
}
Check one of my schema structure:
'use strict'
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var rbpiSchema = new Schema({
rbpiId: {
type: String,
required: true,
unique: true
},
name: {
type: String,
required: true
},
description: {
type: String
},
last_action: {
activity: {
type: String
},
status: {
type: String
},
ut: {
type: Number
},
description: {
type: String
}
},
ct: {
type: Number,
require: true,
default: new Date().getTime()
},
ut: {
type: Number,
require: true,
default: new Date().getTime()
}
});
module.exports = mongoose.model('rbpi', rbpiSchema);

Related

Mongoose Virtual Referencing Other Model/Collection

Trying to reference one collection in mongo via another model's virtual. Currently the only output I'm getting is 'undefined'. Was able to execute this in my main js file as a standalone query but can't figure out how to get this to function as a virtual. Help appreciated.
const mongoose = require('mongoose');
const { stylesList } = require('../styles.js')
const Schema = mongoose.Schema;
const Post = require('./post')
const options = { toJSON: { virtuals: true } };
// options object for the schema, passed in after the actual schema.
const BrandSchema = new Schema({
name: {
type: String,
unique: true,
required: true
},
linkedBrands: [String],
styles: [{ type: String, enum: stylesList }],
links: {
homepage: {
type: String,
required: true
},
grailed: {
type: String,
required: true
},
wikipedia: String
},
bannerImage: String,
likes: {
type: Number,
default: 0
}
}, options);
BrandSchema.set('toObject', { getters: true });
BrandSchema.virtual('amountWorn').get(async function() {
const wornPosts = await Post.find({includedBrands: this._id});
return wornPosts.length
});
module.exports = mongoose.model('Brand', BrandSchema)

mongoose population (refs) is not working

I have created two models where I am trying to ref one model to another but it's not working and I don't have any idea about it I used it exactly the same way as in the mongoose example but it is not helping, the only difference between my working and their example is I have two different files for both models and I am exporting them to use in another file
The example I took reference from Mongoose
First Model
const userModel = require('./userModel')
const {Schema} = mongoose
const tweet = new Schema({
date: {
type: Date,
required: true,
},
msg:{
type: String,
maxlength:140,
required:true
},
tweetid:{
type: mongoose.Types.ObjectId
}
})
const tweetSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'userModel'
},
tweets: {
type: [{tweet}],
default: []
}
})
const tweetModel = mongoose.model('tweet',tweetSchema)
module.exports = tweetModel
Second Model
const {Schema} = mongoose
const userSchema = new Schema({
email: {
type:String,
required: true,
unique: true,
trim:true
},
password: {
type:String,
required: true,
trim:true
},
follows: {
type: [String],
default: []
}
})
const userModel = mongoose.model("User",userSchema)
module.exports = userModel
Your reference string should be equal to the model name that you have passed to mongoose.model:
const tweetSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
tweets: {
type: [{tweet}],
default: []
}
})

Mongoose model how to have an array as property which contains same model as parent

So what I'm trying to do is I want to create a Note object inside note I have a subnote property, I want to have the subnote to be able to be as the same type as the parent model, Right now I'm achieving this by creating two same models with different names and both of them referencing each other for e.x this is the way I'm doing it:
Note schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const noteSchema = new Schema({
icon: {
type: String,
},
banner: {
type: String,
},
name: {
type: String,
required: true,
},
user_id: {
type: String,
required: true,
},
date: {
type: Date,
required: true,
default: Date.now,
},
sub_note: {
type: mongoose.Schema.Types.ObjectId,
ref: "sub_notes",
},
content: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "blocks",
},
],
});
module.exports = Note = mongoose.model("notes", noteSchema);
SubNote schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const subNoteSchema = new Schema({
icon: {
type: String,
},
banner: {
type: String,
},
name: {
type: String,
required: true,
},
user_id: {
type: String,
required: true,
},
date: {
type: Date,
required: true,
default: Date.now,
},
sub_note: {
type: mongoose.Schema.Types.ObjectId,
ref: "notes",
},
content: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "blocks",
},
],
});
module.exports = SubNote = mongoose.model("sub_notes", subNoteSchema);
But I'm not sure if the above method is the proper way to do it..., Please guide me. Thanks.
u can achieve this by simply doing this
const noteSchema = new Schema({
sub_note: {
type: mongoose.Schema.Types.ObjectId,
ref: "notes",
},
});
module.exports = Note = mongoose.model("notes", noteSchema);
or simply using "this" keyword
const noteSchema = new Schema({
sub_note: this
});
module.exports = Note = mongoose.model("notes", noteSchema);

How do I index a field on a mongoose Schema that uses a discriminator?

The error started after I started using the discriminator.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Base = require("../config/Base");
const Refill = Base.discriminator(
"Refill",
new Schema({
cylinderSize: { type: Number, required: true },
cylinderSwap: { type: Boolean, required: true },
quantity: { type: Number, required: true },
location: {
type: { type: String },
coordinates: [Number]
}
})
);
Refill.index({ location: "2dsphere" });
module.exports = mongoose.model("Refill");
This returns the error Refill.index is not a function
In Mongoose, indexes must be created on schemas, not models. In your case, the Refill object is a model. One approach is to implement this in three steps:
Create the schema
Add the index to the schema
Create the model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Base = require("../config/Base");
const refillSchema =
new Schema({
cylinderSize: { type: Number, required: true },
cylinderSwap: { type: Boolean, required: true },
quantity: { type: Number, required: true },
location: {
type: { type: String },
coordinates: [Number]
}
});
refillSchema.index({ location: "2dsphere" });
const Refill = Base.discriminator("Refill", refillSchema);
module.exports = mongoose.model("Refill");
I just took out the Refill.index({ location: "2dsphere" }); and the rest of my code is working fine apparently indexing that field wasn't necessary.

How to give Char datatype in nodejs

I want to set a char datatype in nodejs model if it is possible. I try to my best for find that solution.
If not possible than please give me another way for doing it.
Thank you
show error
char is not defined
model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const AdminSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: Char,
required: true
},
skype_id: {
type: String
},
password: {
type: String,
required: true
}
});
Use a String type with a maxlength of 1:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const AdminSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
maxlength: 1
},
skype_id: {
type: String
},
password: {
type: String,
required: true
}
});
More info on this can be found in the docs.
Here, some example datatype for you.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const AdminSchema = new Schema({
name: {
type: String, // String Format
required: true // If required
},
first_name: {
type: String // String format, but not required
},
favorite_number: {
type: Number, // Number format
get: v => Math.round( v ),
set: v => Math.round( v ),
alias: 'i'
},
... // etc
});

Resources