我是一个新鲜的猫鼬用户,我有一个小练习,我有这个架构
`var BusinessSchema = mongoose.Schema({
personal_email: { type: String, required: true, unique: true },
business_name: { type: String, required: true, unique: true },
business_emails: [{ email: String, Description: String }],
business_logo: { data: Buffer, contentType: String },
//Business Services
services: [{
service_name: { type:String,required:true},
service_price: Number,
promotion_offer : Number,
service_rating : [{Clinet_username:String ,rating : Number}],
service_reviews : [{Clinet_username:String ,review : String}],
type_flag : Boolean,
available_flag : Boolean
}]
});`

我想要做的是使用mongoose更新或添加新服务或删除评级
business.update({// something here to update service_rating },function(err,found_business)
{
}); business.update({// something here to add new service_rating },function(err,found_business)
{
}); business.update({// something here to delete service_rating },function(err,found_business)
{
});

答案 0 :(得分:1)
var where_clause = { /* your where clause */ };
var service_rating = {"username", 5};
添加:
business.update(where_clause, {
'$addToSet' : {
services.service_rating : service_rating
}
}, callback);
删除:
business.update(where_clause, {
'$pull' : {
services.service_rating : service_rating
}
}, callback);
更新:
var other_where = {services.service_rating : {"user", 5}}; // your where clause
business.update(other_where, {
'$set': {
'services.service_rating.Clinet_username' : 'newUser',
'services.service_rating.rating' : 10
}
}, callback);