如何在Mongodb,Go中为数组添加更多字段?

时间:2016-02-25 09:00:08

标签: mongodb go mgo

这些是我的Mongodb文档结构。

    $scope.myZipcode = function myZipcode(){
       $scope,zip = document.getElementById("zip").value;
       $scop.pat1 = 'http://ziptasticapi.com/'+zip; 
    $http.get(pat1).success(function (response) {
    $scope.city = response.city;
    $scope.state = response.state;}

  }

这是我的mongodb文件。

<body ng-app="myApp">
   <div ng-controller="myCtr">
       <form  name="myForm">
           <input type="text" ng-model='pinCode'  id="zip" ng-blur="myZipcode">
           {{city}}
           {{state}}
       </form>
  </div>
  <script>
    myApp.controller('myCtr', function($scope, $http){
         $scope.zip = document.getElementById("zip").value || 0;
         $scope. pat1 = 'http://ziptasticapi.com/'+ $scope.zip || ''; 
         $scope.myZipcode();  

  $scope.myZipcode = function myZipcode(){
         $scope,zip = document.getElementById("zip").value;
         $scop.pat1 = 'http://ziptasticapi.com/'+zip; 
        $http.get(pat1).success(function (response) {
            $scope.city = response.city;
            $scope.state = response.state;}

           }
    });
  </script> 
</body>

我需要再添加一个&#34;流程&#34;处理数组。可能吗?如果是,我如何在mgo中查询?

1 个答案:

答案 0 :(得分:1)

要将另一个文档插入数组,请使用$push

在mgo,

// Create the new 'ProcessItem' document you want to insert.
newProcess := ProcessItem {
    ProcessItemMinimal : processItem,
    SortOrder          : sortOrder
}

change := bson.M {
    "$push": bson.M {
        "process": newProcess,
    },
}

// Update the necessary 'Company' document
companyCollection.UpdateId(company.ID, change)