4

我有一个MongoDB 文档(在 MongoLab.com 上)client,它将用于跟踪健身比赛。每周都会有一个“称重”和一些其他细节被跟踪。

我坚持的是如何向文档添加文档以使用AngularJSclient跟踪结果。

我觉得这可能是我遗漏的一些明显和基本的东西,但我还没有弄清楚如何将新字段附加到现有记录、数组或非数组。

以下是我认为的相关细节,但如果合适,请随时询问更多细节。

完整代码粘贴在http://pastebin.com/4tyRiKus

示例客户端(我可以轻松添加客户端)

{
    "_id": {
        "$oid": "50eee69fe4b0e4c1cf20d116"
    },
    "firstName": "Jon",
    "lastName": "Doe"
}

现在从一个不同的模板中通过_id/detail/:cliendId)提取数据,该模板有效,但我希望为称重添加一个数据点。

<form ng-submit="addWeighIn()">
    <input type="date" ng-model="date" required />
    <input type="text" ng-model="weight" placeholder="Weight" smart-float required />
    <input type="submit" class="btn btn-primary" value="Add" />
</form>

单击此处的“添加”按钮继续下一部分。

控制处理的函数(这会触发并在 MongoLab 模块中调用 update())

$scope.addWeighIn = function () {
    console.log("addWeighIn: " + $scope.date + " : " + $scope.weight);
    // the below line is obviously wrong, just out of ideas.
    $scope.client.weights = [ { date: $scope.date, weight: $scope.weight } ];
    $scope.client.update(function () {

    });
};

控制台显示addWeighIn行,网络数据显示它发送请求,但它实际上并没有文档中的权重,与上面相同。

从 $scope.addWeighIn() 调用的MongoLab 资源并触发

Client.prototype.update = function (cb) {
    console.log("mongolab: update: " + cb);
    return Client.update({ id: this._id.$oid },
        angular.extend({}, this, { _id: undefined }), cb);
};

通过 update() 发送的数据(缺少新的称重数据)

{
    "_id": {
        "$oid": "50eee69fe4b0e4c1cf20d116"
    },
    "firstName": "Jon",
    "lastName": "Doe"
}

这就是我在实际数据库中寻找的东西,但我需要添加权重并相信我不理解一些基本的东西。

{
    "_id": {
        "$oid": "50eee69fe4b0e4c1cf20d116"
    },
    "firstName": "Jon",
    "lastName": "Doe"
    "weights": [
        { "date": "1/3/2013",
          "weight": 155 }
        { "date": "1/10/2013",
          "weight": 150 }
    ]
}

我有几个关于该怎么做的问题。

  1. 我可以在HTML代码中以声明方式执行此操作,例如设置ng-modelclient.weights.date吗?例如:(<input type="date" ng-model="client.weights.date" required />不起作用,但我看看是否有类似的方法)
  2. 如果不是声明式的,那么通过 AngularJS 执行此操作的正确方法是什么?
  3. 我应该使用什么样的术语来查找相关内容?这似乎是我的主要挫折,找不到例子。

我在http://docs.mongodb.org/manual/applications/update/找到了$push描述,可能是我需要使用的,但我不确定如何以AngularJS 方式使用它。

4

1 回答 1

3

我最终发现我的问题与MongoLab资源有关。

我用在https://github.com/pkozlowski-opensource/angularjs-mongolab上找到的那个替换了我的悲伤实现,它成功了。

由于我很难找到一个例子,这里是如何添加子文档。

html

<form ng-submit="addWeighIn()">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>Date</th>
            <th>Weight</th>
            <td></td>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="weight in client.weighIn | orderBy:'date'">
            <td>{{weight.date}}</td>
            <td>{{weight.weight}}</td>
            <td></td>
        </tr>
        </tbody>
        <tfoot>
        <tr>
            <td>
                <input type="text" ng-model="date" placeholder="Date of weigh-in" required />
            </td>
            <td class="input-append">
                <input type="text" ng-model="weight" placeholder="Weight" smart-float required />
                <span class="add-on">lbs</span>
            </td>
            <td><input type="submit" class="btn btn-primary" value="Add" /></td>
        </tr>
        </tfoot>
    </table>
</form>

脚本

$scope.addWeighIn = function () {
    console.log("addWeighIn: " + $scope.date + " : " + $scope.weight);
    $weighIn = { date: $scope.date, weight: $scope.weight };
    // push to the existing array if it exists
    if ($scope.client.weighIn)
        $scope.client.weighIn.push($weighIn);
    // else create the field if it doesn't exist
    else
        $scope.client.weighIn = [ $weighIn ];

    $scope.client.update(function () {
        // do whatever after the update
    });
};
于 2013-01-11T17:21:00.937 回答