I have a MongoDB document (on MongoLab.com) called client , and this will be used to track fitness competitions. Each week it will be "weighed", and some other details are tracked.
I am stuck on how to add a subcategory to a client document to track results using AngularJS.
I feel that this may be something obvious and basic that I am missing, but I did not understand how to add a new field to an existing record, array or non-array.
Below I believe that these are relevant details, but feel free to ask, if necessary, in more detail.
Full code inserted at http://pastebin.com/4tyRiKus
Client example (I can add a client without problems)
{ "_id": { "$oid": "50eee69fe4b0e4c1cf20d116" }, "firstName": "Jon", "lastName": "Doe" }
Now from another template that retrieves data via _id (/ detail /: cliendId), which works, but I want to add a data point for weighting.
<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>
Clicking the Add button here continues through the next part.
Management function to handle (this fires and calls update () in the MongoLab module)
$scope.addWeighIn = function () { console.log("addWeighIn: " + $scope.date + " : " + $scope.weight);
The console shows that the addWeighIn line and the network data indicate that it is sending the request, but in fact it has no weight in the document and is the same as above.
MongoLab resource called from $ scope.addWeighIn () and fires
Client.prototype.update = function (cb) { console.log("mongolab: update: " + cb); return Client.update({ id: this._id.$oid }, angular.extend({}, this, { _id: undefined }), cb); };
Data sent via update () (no new weighing data)
{ "_id": { "$oid": "50eee69fe4b0e4c1cf20d116" }, "firstName": "Jon", "lastName": "Doe" }
This is what I am looking for in a real database, but I need to add weight and believe that I do not understand something fundamental.
{ "_id": { "$oid": "50eee69fe4b0e4c1cf20d116" }, "firstName": "Jon", "lastName": "Doe" "weights": [ { "date": "1/3/2013", "weight": 155 } { "date": "1/10/2013", "weight": 150 } ] }
I have a few questions about what to do.
- Can I do this declaratively in HTML code, for example, set
ng-model to client.weights.date? Example: <input type="date" ng-model="client.weights.date" required /> (Does not work, but I see if there is a similar path) - If not declarative, what is the correct way to do this through AngularJS?
- What terminology should I find on this? This seems to be my main disappointment, cannot find examples.
I found the description of $ push at http://docs.mongodb.org/manual/applications/update/ and may be what I need to use, but I'm not sure how to use this AngularJS method.