Updating/upsert/insert to a nested collection in Meteor &

Updating/upsert/insert to a nested collection in Meteor & MongoDB

本文关键字:in Meteor amp collection upsert insert to Updating nested      更新时间:2023-10-07

我有这个集合和架构:

Words = new Mongo.Collection("words");
WordsSchema = new SimpleSchema({
  date: {
    type: String
  },
  owner: {
    type: String
  },
  entries: {
    type: [Entry]
  }
});
Entry = new SimpleSchema({
  post: {
    type: String
  },
  elapsed_time: {
    type: Number
  },
  createdAt: {
    type: Date
  }
});
Words.attachSchema( WordsSchema );

我只是想通过UPSERT(或用UPSERT=true更新)将条目添加到这个集合中,如下所示:

Words.update({
      date: today,
      owner: Meteor.userId()
    }, {
      $push: {
        entries: { post: post, elapsed_time: elapsed_time, createdAt: new Date() }
      }
    }, {
      upsert: true
    });

即,如果存在日期=今天(存储为"YYYY-MM-DD")且所有者=用户ID的对象,则它只添加特定条目。

但我做错了什么,因为我得到了错误:"条目是必需的"。

非常感谢任何帮助或想法!谢谢

我不确定这是怎么回事。但我猜简单模式不支持类型:[Entry]。因此,在这里您可以尝试这两种方法来实现相同的功能。1.对两种模式都进行Marge。

WordsSchema = new SimpleSchema({
  date: {
    type: String
  },
  owner: {
    type: String
  },
  entries: {
    type: Array
  }
  "entries.$": {
    type: Object
  },
  "entries.$.post: {
    type: String
  },
  "entries.$.verified": {
    type: Boolean
  },
"entries.$.verified": {
    type: Boolean
  }
});

2.通过使用$,您可以分离两种模式

WordsSchema = new SimpleSchema({
  date: {
    type: String
  },
  owner: {
    type: String
  },
  entries: {
    type: Array
  }
  "entries.$": {
    type: Entry
  }
});
Entry = new SimpleSchema({
  post: {
    type: String
  },
  elapsed_time: {
    type: Number
  },
  createdAt: {
    type: Date
  }
});