繁体   English   中英

Mongodb嵌套文档更新?

[英]Mongodb Nested document update?

{
    "_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 );

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

从你的代码中我看到你没有直接嵌入手机但是你制作了一系列嵌入式文档,为了让mongo能够理解你想要做什么,你的更新查询应该是这样的:

var credentials = {
    "Categories[0].mainmodels[0].submodels.Bookedphones": '1',
    "Categories[0].mainmodels[0].submodels.Remainingphones":'9'
};

这个架构设计看起来有点奇怪,我建议你修改它,它不是很直观。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM