You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constAddressSchema=newSchema({location: {type: {type: String,enum: ['Point'],default: 'Point'},coordinates: {type: [Number],index: '2dsphere'},},ratings: {type: mongoose.Mixed,// A mixed type object to handle ratings. Each star level is represented in the ratings object1: Number,// the key is the weight of that star level2: Number,3: Number,4: Number,5: Number,default: {1: 1,2: 1,3: 1,4: 1,5: 1},// TODO:: refre https://plainenglish.io/blog/creating-a-product-rating-system-with-mongodb-and-node-jsget: function(r){// r is the entire ratings objectletitems=Object.entries(r);// get an array of key/value pairs of the object like this [[1:1], [2:1]...]letsum=0;// sum of weighted ratingslettotal=0;// total number of ratingsfor(let[key,value]ofitems){total+=value;sum+=value*parseInt(key);// multiply the total number of ratings by it's weight in this case which is the key}returnMath.round(sum/total);},set: function(r){if(!(thisinstanceofmongoose.Document)){// only call setter when updating the whole path with an objectif(rinstanceofObject)returnr;else{thrownewError('');}}else{// get the actual ratings object without using the getter which returns an integer value// r is the ratings which is an integer value that represent the star level from 1 to 5if(rinstanceofObject){returnr;// handle setting default when creating object}this.get('ratings',null,{getters: false})[r]=1+parseInt(this.get('ratings',null,{getters: false})[r]);returnthis.get('ratings',null,{getters: false});}// return the updated ratings object},validate: {validator: function(i){letb=[1,2,3,4,5];// valid star levelsletv=Object.keys(i).sort();returnb.every((x,j)=>v.length===b.length&&x===parseInt(v[j]));},message: 'Invalid Star Level',},},},{timestamps: true,toObject: {getters: true},toJSON: {getters: true}});module.exports=mongoose.model('Address',AddressSchema);
and I'm trying to get the address with the below aggregation
I have this schema
and I'm trying to get the address with the below aggregation
and I have
then I'm doing
addresses[0].ratings
then gettinginstead of the getter value which is
3
I tried everything but it's not working please help.
The text was updated successfully, but these errors were encountered: