javascript - Mongodb嵌套文件更新?

标签 javascript arrays angularjs node.js mongodb

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

最佳答案

从您的代码中我看到您没有直接嵌入电话,而是制作了一系列嵌入式文档,以便 mongo 了解您正在尝试做什么,您的更新查询应该是这样的:

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

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

关于javascript - Mongodb嵌套文件更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36596697/

相关文章:

javascript - ".mouseleave"不遵守 jQuery 中的 ".not"

javascript - 需要javascript检查夏威夷阿拉斯加等的邮政编码

c# - 将 T[,] 转换为 T[][] 的最快方法?

javascript - 如果数组中的项目与另一个数组中的项目一致,则替换它们

http - $http.post 不适用于 JSON 数据

javascript - 从面板中删除选定的项目 -JS

javascript 输出 NaN,我不知道为什么

c - "a[0]"的意思不仅仅是 "a"吗?

javascript - 从 View 中存在的 google api 调用 Angular Controller 中的函数

angularjs - ng-model 未在 Modal 内更新