Javascript 从multi-object req.body更新数组中的多个对象

Javascript 从multi-object req.body更新数组中的多个对象,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,好的,我有这个收藏: { "_id" : ObjectId("59baa8af86e5df3984674639"), "centralPath" : "C:\\Users\\konrad.sobon\\OneDrive - HOK\\GoogleDrive\\Work\\DynamoWork\\142_SeatManifestGenerator\\RVT2018\\CBUEC_HOK_IN-central.rvt", "totalFamilies" : 31, "

好的,我有这个收藏:

{
    "_id" : ObjectId("59baa8af86e5df3984674639"),
    "centralPath" : "C:\\Users\\konrad.sobon\\OneDrive - HOK\\GoogleDrive\\Work\\DynamoWork\\142_SeatManifestGenerator\\RVT2018\\CBUEC_HOK_IN-central.rvt",
    "totalFamilies" : 31,
    "unusedFamilies" : 18,
    "oversizedFamilies" : 0,
    "inPlaceFamilies" : 0,
    "createdBy" : "konrad.sobon",
    "createdOn" : ISODate("2017-09-14T20:19:09.525Z"),
    "families" : [ 
        {
            "name" : "Spot Elevation - Target Filled_HOK_I",
            "size" : "1.2Mb",
            "sizeValue" : 1200000,
            "instances" : 0,
            "elementId" : 6158,
            "isFailingChecks" : true,
            "isDeleted" : false,
            "_id" : ObjectId("59bae43d2720015998392905"),
            "tasks" : [],
            "parametersCount" : 0,
            "nestedFamilyCount" : 1,
            "voidCount" : 0,
            "refPlaneCount" : 2,
            "arrayCount" : 0
        }, 
        {
            "name" : "Section Head - Filled_HOK_I",
            "size" : "140kb",
            "sizeValue" : 145760,
            "instances" : 0,
            "elementId" : 8762,
            "isFailingChecks" : true,
            "isDeleted" : false,
            "_id" : ObjectId("59bae43d2720015998392904"),
            "tasks" : [],
            "parametersCount" : 0,
            "nestedFamilyCount" : 1,
            "voidCount" : 0,
            "refPlaneCount" : 3,
            "arrayCount" : 0
        }, 
        {
            "name" : "Railing Tag_HOK_I",
            "size" : "244kb",
            "sizeValue" : 249856,
            "instances" : 0,
            "elementId" : 12426,
            "isFailingChecks" : true,
            "isDeleted" : false,
            "_id" : ObjectId("59bae43d2720015998392903"),
            "tasks" : [],
            "parametersCount" : 3,
            "nestedFamilyCount" : 1,
            "voidCount" : 0,
            "refPlaneCount" : 2,
            "arrayCount" : 0
        }, 
        {
            "name" : "Fixed",
            "size" : "316kb",
            "sizeValue" : 323584,
            "instances" : 0,
            "elementId" : 3499132,
            "isFailingChecks" : true,
            "isDeleted" : false,
            "_id" : ObjectId("59bae43d27200159983928e7"),
            "tasks" : [],
            "parametersCount" : 4,
            "nestedFamilyCount" : 2,
            "voidCount" : 0,
            "refPlaneCount" : 18,
            "arrayCount" : 0
        }
    ],
    "__v" : 0
}
我试图做的是发送一个请求来更新该集合中的某些“族”。我有一组这样的族对象:

{"key": [
            {
                "name" : "New Spot Elevation - Target Filled_HOK_I",
                "size" : "1.2Mb",
                "sizeValue" : 1200000,
                "instances" : 0,
                "elementId" : 6158,
                "isFailingChecks" : true,
                "isDeleted" : false,
                "Id" : "59bae43d2720015998392905",
                "tasks" : [],
                "parametersCount" : 0,
                "nestedFamilyCount" : 1,
                "voidCount" : 0,
                "refPlaneCount" : 2,
                "arrayCount" : 0
            }, 
            {
                "name" : "New Section Head - Filled_HOK_I",
                "size" : "140kb",
                "sizeValue" : 145760,
                "instances" : 0,
                "elementId" : 8762,
                "isFailingChecks" : true,
                "isDeleted" : false,
                "Id" : "59bae43d2720015998392904",
                "tasks" : [],
                "parametersCount" : 0,
                "nestedFamilyCount" : 1,
                "voidCount" : 0,
                "refPlaneCount" : 3,
                "arrayCount" : 0
            }
        ]}
现在,我需要能够找到并更新每个指定的族。我认为我可以迭代传入的数组(我在req.body中发送它),然后生成一个需要更新的ID数组,以便在mongo中使用$in操作符。在那之后,我想我可以使用id来检索我感兴趣的属性,然后使用$set来更新它们。我在这里的尝试:

module.exports.updateMultipleFamilies = function (req, res) {
    var id = req.params.id;
    var famIds = []; // [ObjectId]
    var newFamilies = {}; // {"id_string" : family}
    for(var key in req.body) {
        if(req.body.hasOwnProperty(key)){
            for(var i = 0; i < req.body[key].length; i++){
                var family = req.body[key][i];
                newFamilies[family.Id] = family;
                famIds.push(mongoose.Types.ObjectId(family.Id));
            }
        }
    }

    Families
        .updateMany(
            {_id: id, 'families._id': {$in: famIds}},
            {$set: {'name': newFamilies["current_document_id_help"].name}}, function(err, result){
                if(err) {
                    res
                        .status(400)
                        .json(err);
                } else {
                    res
                        .status(202)
                        .json(result);
                }
            }
        )
};

您需要使用families数组,在应用程序中更改代码,然后再次更新families数组。 或者您可以尝试执行{$set:{'families.$.name':newFamilies[“当前文档\u id\u帮助”].name} } 但我不确定这是否会更新你们所有的家庭

第一个解决方案:

var id=req.params.id;
yourCollation.findById(id,函数(err,doc){
if(req.body.key&&req.body.key.constructor===数组){
对于(变量i=0;i})
在此处查看此答案:-恐怕您需要单独更新上层循环中的每个子文档,您当前正在构建
新家庭
数组。这对我来说毫无意义。介意分享一些代码,这样我就能理解你的意思了?我更新了答案,试试这个代码。您没有说您的排序规则名称,所以请更改您的排序规则
module.exports.updateMultipleFamilies1 = function (req, res) {
    var id = req.params.id;
    var bulkOps = [];

    for(var key in req.body) {
        if(req.body.hasOwnProperty(key)){
            bulkOps = req.body[key].map(function(item){
                return {
                    'updateOne': {
                        'filter': {'_id': id, 'families._id': mongoose.Types.ObjectId(item.Id)},
                        'update': {'$set': {'families.$.name': item.name}},
                        'upsert': false
                    }
                }
            })
        }
    }

    Families.collection
        .bulkWrite(
            bulkOps,
            {'ordered': true, w:1}, function(err, result){
            if(err) {
                res
                    .status(400)
                    .json(err);
            } else {
                res
                    .status(202)
                    .json(result);
            }
        })

};