如何验证和更新mongoDB Collection文档中的嵌入式对象

How to Validate and Update an embedded object in a document of a mongoDB Collection

本文关键字:文档 Collection 对象 嵌入式 mongoDB 更新 何验证 验证      更新时间:2023-09-26

如何更新mongoDB文档中嵌入对象的值?

{{服务。Id}}和{{服务。在表模板中显示正确的值,但我不确定如何在saveItem()函数中调用它们。当我尝试service.id, service.$.idservice.[0].id时,我得到Error: Unexpected token .。当我尝试"service.id",表单提交,什么都没有发生,当我尝试"service.[0].id",表单卡住编辑,什么都没有发生,当我尝试"service.$.id",我得到一个错误说$字段无法更新。

我应该有别的东西在我的javascript代码?或者我在定义模式时做错了什么(即不需要美元符号)。

谢谢!下面是我的代码:

var Schemas = {};
Items = new Meteor.Collection('items');
Schemas.Items = new SimpleSchema({
  _id: {
    type: String,
  },
  name: {
    type: String,
    label: "Item Name",
    min: 1
  },
  "service.$": {
    type: [Object]
  },
  "service.$.id": {
    type: Number
  },
  "service.$.username": {
    type: String
  }
});
Items.attachSchema(Schemas.Items);
var saveItem = function() {
  var editItem = {
    _id: $('#editId').val(),
    name: $('#editName').val(),
    "service.$.id": $('#editServiceId').val(), //not working
    "service.$.username": $('#editServiceUsername').val() //not working
  }
 
  Items.update(Session.get('editItemId'), {$set: editItem}, {validationContext: 'updateForm'}, function(error, result) {
    if(!error){
      Session.set('editItemId', null);
    }
  });
}
Template._editItemsItem.helpers({
  editing: function() {
  	return Session.equals("editItemId", this._id);
  }
});
Template._editItemsItem.events({
  'click .editItem': function() {
    Items.simpleSchema().namedContext('updateForm').resetValidation();
    Items.simpleSchema().namedContext('insertForm').resetValidation();
    Session.set("editItemId", this._id);
  },
  'click .cancelItemEdit': function() {
    Items.simpleSchema().namedContext('updateForm').resetValidation();
    Items.simpleSchema().namedContext('insertForm').resetValidation();
    Session.set("editItemId", null);
  },
  'click .saveItem': function() {
    saveTeam();
  },
  'keypress input': function(e){
    if(e.keyCode === 13){
      saveItem();
    }
    else if(e.keyCode === 27){
      Items.simpleSchema().namedContext('updateForm').resetValidation();
      Items.simpleSchema().namedContext('insertForm').resetValidation();
      Session.set("editItemId", null);
    }
  }
});
Template._editItems.helpers({
  items: function() {
    return Items.find();
  },
});
<template name="_editItems">
	<table class="ui very compact selectable celled table">
	  <thead>
	    <tr>
	    	<th>_id</th>
	      <th>Name</th>
	      <th>Service Name</th>
	      <th>Service Id</th>
	      <th>Edit</th>
	    </tr>
	  </thead>
	  <tbody>
	    {{#each items}}
				{{> _editItemsItem}}
	    {{/each}}
	  </tbody>
	</table>
</template>
<template name="_editItemsItem">
	{{#if editing}}	
		<tr class="ui form">
			<td><div class="ui small input"><input type="text" id="editId" value="{{_id}}"></div></td>
			<td><div class="ui small input"><input type="text" id="editName" value="{{name}}"></div></td>
			<td><div class="ui small input"><input type="text" id="editServiceUsername" value="{{service.username}}"></div></td>
			<td><div class="ui small input"><input type="text" id="editServiceId" value="{{service.id}}"></div></td>
			<td>
				<button class="saveItem ui small circular primary button "><i class="ui save icon"></i></button>
				<button class="cancelItemEdit ui small circular red button "><i class="ui cancel icon"></i></button>
			</td>
		</tr>
	{{else}}
		<tr>
			<td>{{_id}}</td>
			<td>{{name}}</td>
			<td>{{service.username}}</td>
			<td>{{service.id}}</td>
			<td>
				<button class="editItem ui small circular button"><i class="ui edit icon"></i></button>
			</td>
		</tr>
	{{/if}}
</template>

在@MichelFloyd的帮助下找到了答案。

模式应该如下所示:(无$符号)

 "service.$": {
    type: [Object]
  },
  "service.$.userid": {
    type: Number
  },
  "service.$.username": {
    type: String
  }

然后这个就可以了:

 var editItem = {
   _id: $('#editId').val(),
   name: $('#editName').val(),
  "service.userid": $('#editServiceId').val(),
  "service.username": $('#editServiceUsername').val()
}