Javascript Mongodb嵌套文档更新?

Javascript Mongodb嵌套文档更新?,javascript,arrays,angularjs,node.js,mongodb,Javascript,Arrays,Angularjs,Node.js,Mongodb,上面我给出了存储在数据库(mongodb)中的数据。在这里,我想更新剩余的电话和预订的电话。在这里 我正在尝试更新,但它没有更新,因为我创建了嵌套文档,请帮助我完成更新 给定我在Angular前端服务器中编写的代码 { "_id": { "$oid": "5705f793e4b0acd6e2456804a" }, "Categories": [ { "mainmodels": [ {

上面我给出了存储在数据库(mongodb)中的数据。在这里,我想更新剩余的电话和预订的电话。在这里 我正在尝试更新,但它没有更新,因为我创建了嵌套文档,请帮助我完成更新

给定我在Angular前端服务器中编写的代码

{
    "_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"
}
当我运行此命令时,它会点击后端服务器路由

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) {
});
对于查找id,我正在使用此

app.route('/phones/:findId')
                .get(phones.read)
                .put(phones.update)
                .delete(phones.delete);
app.param('findId', phones.phomesByID );
用于更新我使用的

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);
                }
        });
};

从您的代码中,我看到您没有直接嵌入手机,而是创建了一个嵌入文档数组,为了让mongo了解您正在尝试做什么,您的更新查询应该是这样的:

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);

这个模式设计似乎有点奇怪,我建议你修改它,它不是很直观。

你有没有收到任何错误?没有,我没有收到错误。我收到200条回复,但当我签入数据库时,它并没有更新
var credentials = {
    "Categories[0].mainmodels[0].submodels.Bookedphones": '1',
    "Categories[0].mainmodels[0].submodels.Remainingphones":'9'
};