将数据正确保存到mongoDB

时间:2015-09-27 08:17:05

标签: javascript arrays angularjs mongodb

在Angular中有一个应用程序。不要理解如何正确保存数据。

这是我api的一部分:

.post('/type/add', function(req, res){
   var type = new NewType({
      type: req.body.type,
      subtype: req.body.subtype,
   });
   type.save(function(err, newTypeOne){
      if(err){
         res.send(err);
         return;
      }
      res.json({message: "New type Created!"});
   });
})

这是我的mongodb架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Type = new Schema({
    type: String,
    subtype: String
});
module.exports = mongoose.model('NewType', Type);

这是我的控制者:

.controller('AddTypeController', ['CreateType', 'AllType', '$location', function(CreateType, AllType, $location){
   vm = this;
   vm.createType = function(){
     vm.message = '';
     CreateType.create(vm.typeData)
       .success(function(data){
       vm.typeData = '';
       vm.message = data.message;
       $location.path('/type')
       });
   };
   AllType.getAll().success(function(data){
      vm.types = data;
   });
}])

这是我的服务:

angular.module('typeService', [])
.factory('AllType', function($http){
   var typeFactory = {};
   typeFactory.getAll = function(){
      return $http.get('/api/type');
   };
   return typeFactory;

})
.factory('CreateType', function($http){
   var typeFactory = {};
   typeFactory.create = function(typeData){
      return $http.post('/api/type/add', typeData);
   };
   return typeFactory;
})

我的HTML:

<div ng-controller="AddTypeController as type">
<form>
   <md-select ng-model="type.typeData.type" placeholder="Type">
      <md-option ng-value="typeone.type" ng-repeat="typeone in type.types">{{typeone.type}}</md-option>
   </md-select>
   <md-input-container flex>
      <label>SubTyp</label>
      <input ng-model="type.typeData.subtype">
   </md-input-container>
   <button ng-click="type.createType()">Send</button>
</form>
</div>

现在我有一个问题: 例如,我还没有数据。我添加了第一个选择 - TypeOne,并在第二个选择 - SubTypeOne。在数据库中我现在有{type:TypeOne,subtype:SubTypeOne}。当我向TypeOne添加第二个subType时,我首先选择TypeOne并在输入中添加子类型 - SubTypeTwo。现在我在数据库中有第二条记录{type:TypeOne,subtype:SubTypeTwo},然后在第一次选择i中有两次TypeOne。

但是我想做这样的事情:如果我已经有了需要的类型,并且想要只添加新的子类型。在这种情况下,我想在数据库中像这样:{type:TypeOne,subtype:[subtype:SubTypeOne,subtype:SubTypeTwo]}。为此,我应该像数组一样保存子类型,但不要理解如何。

1 个答案:

答案 0 :(得分:0)

In your model you should have the subtypes as an array of strings. Then you can push them onto that array. An example is shown below: var Type = new Schema({ type: String, subtype: [String] }); Then you can have an update method (mongoose: findOneAndUpdate) instead of just creating a new Type. Or you can have it check for if it can find one, and update that, otherwise create a new one. An example is shown below: CreateType.findOneAndUpdate({type: 'type1'}, {$push: {subtype: 'type2'} }, function(err, data) { if (err) // Do the create for a new one here else // Send back a 200 OK or whatever else }); This will allow you to update the array as they are. I encourage you to look into the findOneAndUpdate documentation on mongoose, as well as the $push mongo documentation. Hope this helps! I've been working on an app lately that is basically doing what I've described here. So I've actually been using this to attest that it works.