Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Pushing to an array on upsert() not supported? #375

Closed derwaldgeist closed 6 years ago

derwaldgeist commented 6 years ago

I have the following schema:

export const checkinSchema = new SimpleSchema({
  // location id
  locationId: {
    type: String,
    index: 1
  },
  // checkins users make at a location
  checkins: {
    type: [Object]
  },
  // user checking in
  'checkins.$.userId': {
    type: String,
    index: 1
  },
  // time-stamp
  'checkins.$.time': {
    type: Date
  }
});

and the following upsert code:

checkinsCollection.upsert(
  { locationId },
  { $push: { checkins: { userId: this.userId, time: new Date() } } }
);

If I run this, Collection2 complains that Checkins is required. Yet, if I add { validate: false }, everything works as expected. MongoDB creates the document and pushes the user / timestamp combination to the array.

Why doesn't Collection2 accept this?

aldeed commented 6 years ago

If you are using the Meteor SimpleSchema package, switch to the NPM package. It should work properly with that one.