用可变流星蒙哥更新字段

时间:2018-07-04 08:25:13

标签: mongodb meteor

我有一个名为“镇”的收藏品,其中包含此文档:

{
    "_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变量定义要更新的现货字段。 更新startTime,finishTime,constructioningBuildingName,将构造更改为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

}

1 个答案:

答案 0 :(得分:1)

首先,您应该重新考虑您拥有的数据结构。通常不建议使用编号的键。在您的情况下,最好使用斑点数组从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,
    }}) 


}