Meteor / MongoDB重复数组对象

时间:2017-01-12 21:33:54

标签: javascript mongodb meteor

我很好奇在meteor中创建按钮以复制数组对象的最佳方法是什么。举个例子,如果我有:



"TasksWithData": [
  {
    "inspectionDetails": [
      {
        "name": "somename",
        "inspector": "Ss6TjGGzqWJRZYStx",
        "inspectorName": "name of inspector",
        "projectType": "inspection",
        "startDate": "2017-01-12T05:00:00.000Z",
        "desc": "a description",
        "activeTask": true
      }
    ],
    "TaskItem": [
      {
        "DataFacilitySection": "dsfgsdfgds",
        "DataItemDescription": "item 2",
        "DataItemSection": "dfgdfgdf",
        "DataItemCode": "dfgdfgdf",
        "DataItemPass": null
      }
    ],
  }
],




如果我想用客户端按钮复制整个TasksWithData数组(这是一个子文档而没有_id)...我该怎么做?

这是我打电话的事件:



  'click .duplicate': function(){
    Meteor.call('duplicateItem', this._id);
  }




这是模板结构:

Inspections / AddInspectionsHome:



<template name="AddInspectionHome">
  <div class="container single-list conatiner-padding">
    <h1>Task Navigator</h1>
    <hr>
      <div class="panel panel-default">
        <div class="panel-heading no-padding">
          <span>Select a Client</span>
        </div>
        {{#each findClients}}
          {{> SingleClientInspection}}
        {{/each}}
      </div>
  </div>
  <!--TODO: Need to add the ability to duplicate these inspections-->
</template>
&#13;
&#13;
&#13;

Inspections / SingleClientAddInspection(ReactiveVar模式,触发用户查看活动检查的能力):

&#13;
&#13;
{{#if activeMode}}
    <div class="panel panel-default col-lg-12">
      <a href="#" class="active btn btn-primary">Close</a>
      <div class="panel panel-default">
        <div class="panel-heading">View Active Tasks</div>
          <table class="table table-striped">
            <thead>
              <tr>
                <th>Facility</th>
                <th>Inspector</th>
                <th>Type</th>
                <th>Due Date</th>
              </tr>
            </thead>
            <tbody>
              {{#each TasksWithData}}
                {{#each inspectionDetails}}
                  {{#if activeTask}}
                    <tr>
                      <td>{{TaskFacility}}</td>
                      <td>{{inspectorName}}</td>
                      <td>{{projectType}}</td>
                      <td>{{startDate}}</td>
                      <td><a href="#" class="duplicate btn btn-success">Copy</a></td>
                    </tr>
                  {{/if}}
                {{/each}}
              {{/each}}
            </tbody>
          </table>
      </div>
    </div>
  {{/if}}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

我建议首先提取原始数组,然后将其连接回自身。

const doc = MyCollection.findOne(_id); // however you get your original doc
let TasksWithData = doc.TasksWithData;
TasksWithData.concat(TasksWithData);
MyCollection.update(_id,{$set: {TasksWithData: TasksWithData}});