{
"_id": {
"$oid": "5705f793e4b0acd6e2456804a"
},
"Categories": [
{
"mainmodels": [
{
"submodels": [
{
"price": "2000",
"submodelname": "lumia021",
"Remainingphones": "0",
"Bookedphones": "0",
"Numofphones": "10"
}
],
"Status": "Active",
"modelname": "lumia",
"fromdate": "2016-04-01T16:39:12.051Z",
"todate": "2016-04-31T19:19:44.051Z"
}
],
"brand": "nokia"
}
],
"rank": "1",
"name": "kalasipalaya"
}
上面我已经提供了存储在数据库中的数据(mongodb)。在这里,我想更新剩余电话和预订电话。这里 我正在尝试更新,但它没有更新,因为我创建了嵌套文档帮助我完成它
给出我在Angular前端服务器中编写的代码
credentials =
{
"Categories.mainmodels.submodels.Bookedphones": '1',
"Categories.mainmodels.submodels.Remainingphones":'9'
}
$http.put('http://localhost:3000/phones/' + '5705f793e4b0acd6e2456804a', credentials).success(function(data, status, headers, config, response) {
});
当我运行它时它会命中后端服务器路由
app.route('/phones/:findId')
.get(phones.read)
.put(phones.update)
.delete(phones.delete);
app.param('findId', phones.phomesByID );
for find id我正在使用此
exports.phomesByID = function(req, res, next, id) {
Phones.findById(id).populate('user', 'displayName').exec(function(err, phones) {
if (err) return next(err);
if (! phones) return next(new Error('Failed to load Phones ' + id));
req.phones = phones ;
next();
});
};
更新我用过
exports.update = function(req, res) {
console.log(req.phones);
var phones = req.phones ;
phones = _.extend(phones , req.body);
console.log(phones);
phones.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(phones);
}
});
};
我已经制作了这样的模型
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var submodelSchema = {
submodelname: {type:String, required: false},
price: {type:String, required: false},
Remainingphones: {type:String, required: false},
Bookedphones: {type:String, required: false},
Numofphones: {type:String, required: false}
};
submodelSchema = 'new Schema('+ submodeleSchema +',{_id:true})';
var typeSchema = {
vtype: {type:String, required: false},
mainservices: {
Status: {type:String, required: false},
modelname : {type:String, required: false},
fromdate: {type:String, required: false},
todate: {type:String, required: false}
},
submodels: [submodelSchema], default:[]
};
typeSchema = 'new Schema('+typeSchema +',{_id:true})';
var PhoneSchema = new Schema({
rank: {
type: String,
default: '',
trim: true
},
name: {
type: String,
default: '',
trim: true
},
Categories: [typeSchema], default:[]
});
mongoose.model('Phone', PhoneSchema);
答案 0 :(得分:0)
从你的代码中我看到你没有直接嵌入手机但你制作了一系列嵌入式文档,为了让mongo能够理解你想要做什么,你的更新查询应该是这样的:
var credentials = {
"Categories[0].mainmodels[0].submodels.Bookedphones": '1',
"Categories[0].mainmodels[0].submodels.Remainingphones":'9'
};
这种架构设计看起来有点奇怪,我建议你修改它,它不是很直观。