javascript - Meteor.js 和 Collection2 : Update Method that Inserts New Object in subschema

标签 javascript mongodb meteor

上下文

我有一个收藏(“ field ”)。我试图允许用户将新对象(“房间” - 定义为子模式)添加到 field 对象的实例中。每个房间都应该有一个唯一的 ID(在 collection2 架构中使用 autovalue)。

问题/我尝试过的

我尝试了两种方法:

  1. Venues.insert - 尝试创建一个新的整个 Venue 实例,而不是向 roomPricing 子架构添加新行。
  2. Venues.update - 使用 '$set:params"抛出 Exception while invoking method 'addRoom' MongoError: Cannot update 'roomPricing' and 'roomPricing.roomId' at the same time 。如果我从集合中删除 Id,应用程序将更新“现有”房间值,而不是创建房间的"new"实例。

总之,我需要一种方法来更新“父”对象( field ),同时在“房间”子模式中创建新的“子”对象。

收藏

Schema.RoomPricing = new SimpleSchema({
    roomId: {
        type: String,
        autoValue: function(){
             return Random.id();
            },
        optional: true
    },
    roomName: {
        type: String,
        max:50,
        optional: true
      }
    }

// MAIN SCHEMA for the Venues colleciton. 
Schema.Venues = new SimpleSchema({
    venueName: {
        type: String,
        label: "Venue Name",
        max: 200,
        optional: false
},
   roomPricing: {
        type: Schema.RoomPricing,
        optional: true
  }
}

Controller 和方法

  var currentVenueId = this.params._id
  var params = {
      roomPricing: {
          roomName: roomName,
          sitCapacity: sitCapacity,
        }
      }

  Meteor.call('addRoom', currentVenueId, params);

//Method
Meteor.methods({
  'addRoom': function (id, params) {
    Venues.insert({
        _id: id
        },{
        $set:params});
        }
    });

最佳答案

最后这就是我使用的 - 需要 $push 而不是 $set。

'addRoom': function (id, params) {
Venues.update({
     _id: id
   },{
     $push:params});
   }

关于javascript - Meteor.js 和 Collection2 : Update Method that Inserts New Object in subschema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299160/

相关文章:

javascript - jQuery 添加/删除类

sql - 在 MongoDB (NoSQL) 中存储关系数据

javascript - Meteor WhatsApp 克隆教程不会在 chats.controller.js 中构造 ChatsCtrl

javascript - 触发鼠标悬停事件时显示图像

javascript - 带插件的圆 Angular 到 IE 7 中的图像?

java - 在测试环境中禁用 mongoEmbeddedServer

regex - mongoexport 正则表达式未知选项

javascript - meteor 在模板中迭代列表

javascript - 如何从 Meteor.js 中另一个模板的事件触发模板的 EventMap 事件?

javascript - 查询 :invalid regular expression