【问题标题】:Mongodb Nested document update?Mongodb嵌套文档更新?
【发布时间】:2016-08-04 10:46:03
【问题描述】:
{
    "_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)中的数据。在这里,我想更新 Remainingphones 和 Bookedphones。这里 我正在尝试更新,但它没有更新,因为我创建了嵌套文档帮助我完成它

给定我在 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);

【问题讨论】:

  • 你有什么错误吗?
  • 不,我没有收到错误消息。我收到 200 个响应,但是当我签入数据库时​​它没有更新

标签: javascript arrays angularjs node.js mongodb


【解决方案1】:

从您的代码中,我看到您没有直接嵌入手机,而是制作了一系列嵌入文档,为了让 mongo 了解您要执行的操作,您的更新查询应该是这样的:

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

这个schema设计好像有点奇怪,建议你修改一下,不是很直观。

【讨论】:

    猜你喜欢
    • 2010-11-11
    • 2021-02-13
    • 2013-03-19
    • 2017-03-06
    • 1970-01-01
    • 2021-03-26
    • 2017-04-28
    • 2014-09-23
    • 2013-08-12
    相关资源
    最近更新 更多