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

[3.0.3] Upsert fails even if document inserted would match the schema definition #403

Closed SimonSimCity closed 4 years ago

SimonSimCity commented 4 years ago

https://github.com/Meteor-Community-Packages/meteor-collection2/commit/379c810a54d99c729b7a94c1c5228d9b19c83787

https://docs.mongodb.com/manual/reference/method/db.collection.update/#insert-a-new-document-if-no-match-exists-upsert The second tab ("Upsert with Operator Expressions") looks good until you encounter the fact, that you also have operators which will create the value on the document.

Of the operators I've tested its only $in (only having one element in the list) and $eq which should be taken as term. Children of a $and should be parsed as well.

rijk commented 4 years ago

This change breaks an existing upsert, that works in 3.0.4:

Tags.upsert(
  { 
    "color": "orange", 
    "label": "Today", 
    "team": {
      "id": "jPrbLLC8mMQcMe6ZC",
      "slug": "myteam"
    }
  },
  {
    "$set": {
      "lastUsedAt": new Date
    }
  }
)

In 3.0.5 gives the following error: Error: Team is required in tags update

I20200107-20:20:52.143(1)?   invalidKeys: 
I20200107-20:20:52.144(1)?    [ { name: 'team', type: 'required', value: undefined },
I20200107-20:20:52.144(1)?      { name: 'team.id', type: 'required', value: undefined },
I20200107-20:20:52.144(1)?      { name: 'team.slug', type: 'required', value: undefined } ],

This is the schema:

Tags.attachSchema(new SimpleSchema({
  team: {
    type: new SimpleSchema({
      id: {
        type: String
      },
      slug: {
        type: String
      }
    })
  },
  color: {
    type: String
  },
  label: {
    type: String
  },
  lastUsedAt: {
    type: Date
  }
}))
SimonSimCity commented 4 years ago

@Rijk It's best to report this as a new issue ...