使用可变流星 mongo 更新字段

Update Field with variable meteor mongo

我有一个名为 "Towns" 的 collection,里面有这份文件:

{
    "_id" : "vK4PvdNBfBbdv92PH",
    "ownerId" : "J8MpsWChPQdET6jwQ",
    "createdAt" : ISODate("2018-07-04T07:25:28.406Z"),
    "spot1" : {
        "construction" : false,
        "constructingBuildingName" : "",
        "buildingName" : "factory",
        "level" : 1,
        "startTime" : 0,
        "finishTime" : 0
    },
    "spot2" : {
        "construction" : false,
        "constructingBuildingName" : "",
        "buildingName" : "",
        "level" : 0,
        "startTime" : 0,
        "finishTime" : 0
    },
    "spot3" : {
        "construction" : false,
        "constructingBuildingName" : "",
        "buildingName" : "",
        "level" : 0,
        "startTime" : 0,
        "finishTime" : 0
    }
}

我想做的是在这种情况下用变量更新 spot2s 字段。

spotName 变量定义要更新的 spot 字段。 更新 startTime、finishTime、constructingBuildingName,将 construction 更改为 true

'buildNewBuilding': function (userid, buildingid, spotnumber) {
    var spotName = "spot" + spotnumber.toString();
    var data = Buildings.find({_id: buildingid}).fetch();
    var constructingBuildingName = data[0].name;
    var startTime = Math.floor(Date.now() / 1000);
    var finishTime = startTime + data[0].time;


    Towns.update({ownerId: userid}, {}) //??
    //update startTime,finishTime,constructingBuildingName,change construction to true

}

首先,您应该重新考虑您拥有的数据结构。通常不建议使用带编号的键。在您的情况下,最好使用点数组从 mongo 请求引擎中获得更好的收益。

唯一应该保留结构的情况是您 100% 确定每个城镇不需要 4 个点。

{
    "_id" : "vK4PvdNBfBbdv92PH",
    "ownerId" : "J8MpsWChPQdET6jwQ",
    "createdAt" : ISODate("2018-07-04T07:25:28.406Z"),
    "spots" : [
        {
            "name": "spot1",
            "construction" : false,
            "constructingBuildingName" : "",
            "buildingName" : "factory",
            "level" : 1,
            "startTime" : 0,
            "finishTime" : 0
        },
        {
            "name": "spot2",
            "construction" : false,
            "constructingBuildingName" : "",
            "buildingName" : "",
            "level" : 0,
            "startTime" : 0,
            "finishTime" : 0
        },
        {
            "name": "spot3",
            "construction" : false,
            "constructingBuildingName" : "",
            "buildingName" : "",
            "level" : 0,
            "startTime" : 0,
            "finishTime" : 0
        }
    ]
}

那么,我建议你去mongoDB documentation here里面看看。它解释了如何使用 $elemMatch

进行此类更新
'buildNewBuilding': function (userid, buildingid, spotnumber) {
    var spotName = "spot" + spotnumber.toString();
    var data = Buildings.find({_id: buildingid}).fetch();
    var constructingBuildingName = data[0].name;
    var startTime = Math.floor(Date.now() / 1000);
    var finishTime = startTime + data[0].time;


    Towns.update({ownerId: userid, spots: {$elemMatch: {name: spotName}}}, {$set: {
        "spots.$.constructingBuildingName": constructingBuildingName,
        "spots.$.startTime": startTime,
        "spots.$.finishTime": finishTime,
        "spots.$.construction": true,
    }}) 


}