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
importmongoosefrom'mongoose'import{ratings}from'stars-schema'const{Schema, model}=mongooseconstProductSchema=newSchema({name: String})// creates a ten stars product ratings system. The name of the path is 'stars' rather than the default 'ratings'.ProductSchema.plugin(ratings,{name: 'stars',levels: [1,2,3,4,5,6,7,8,9,10]})// create a modelletproduct=mongoose.model('Product',ProductSchema)letprod=product.create({name: "Product One"})// use the weighted average getter function console.log(prod.stars)// 5.5// get the full ratings without gettersconsole.log(prod.get('stars',null,{getters: false}))// {1: 1, 2:1, 3:1, 4:1, 5:1, 6:1, 7: 1, 8:1, 9:1, 10:1}// give a five star rating. prod.stars=5awaitprod.save()// no need to mark as modified// update the whole ratings objectprod.stars={1: 1,2:1,3:1,4:1,5:1,6:1,7: 1,8:1,9:1,10:1}// update from the modelawaitproduct.findOneAndUpdate({name: "Product One"},{stars: {1: 1,2:1,3:1,4:1,5:1,6:1,7: 1,8:1,9:1,10:1}})// use the increment operatorawaitproduct.findOneAndUpdate({name: "Product One"},{$inc:{['stars.5']: 1}})
Config Options
name: string: A name for the path; defaults to ratings.
levels: Array: An array representing the star system in use; defaults to [1, 2, 3, 4, 5].
getter: Function: A getter function if you don't want to use weighted average.
setter: Function: A setter function if you don't need the default.
validate: boolean: To validate or not; default is true.