question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support $[] operator in updates

See original GitHub issue

This appears to be an issue with simpl-schema and not Collection2, according to https://github.com/Meteor-Community-Packages/meteor-collection2/issues/407.

I’m trying to use the $[] operator to update all elements in an array with 1 update() call.

let test = new Mongo.Collection('test')
test.attachSchema(new SimpleSchema({
  'arrayOfObjects': { type: Array, defaultValue: [] },
  'arrayOfObjects.$': Object,
  'arrayOfObjects.$.name': String,
}))

test.update(
  { _id: 'rickandmorty' },
  { $set: {
    _id: 'rickandmorty',

    arrayOfObjects: [
      {name: 'Rick'},
      {name: 'Morty'},
    ]
  } },
  { upsert: true }
)

// Set all arrayOfObject name's at once:
test.update(
  { _id: 'rickandmorty' },
  { $set: { 'arrayOfObjects.$[].name': 'Marge Simpson' } }
)

This apparently fails due to a missing key in the schema:

Error: arrayOfObjects.$[].name is not allowed by the schema (arrayOfObjects.$[].name) in test update
    at getErrorObject (packages/aldeed:collection2/collection2.js:491:17)
    at doValidate (packages/aldeed:collection2/collection2.js:463:13)
    at Collection.Mongo.Collection.<computed> [as update] (packages/aldeed:collection2/collection2.js:196:14)
    at module (imports/api/catalog/catalog.js:559:6)
    at fileEvaluate (packages/modules-runtime.js:336:7)
    at Module.require (packages/modules-runtime.js:238:14)
    at Module.moduleLink [as link] (/Users/.../.meteor/packages/modules/.0.15.0.itei65.v8gbn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:52:22)
    at module (server/imports/test.js:1:661)
    at fileEvaluate (packages/modules-runtime.js:336:7)
    at Module.require (packages/modules-runtime.js:238:14)
    at Module.moduleLink [as link] (/Users/.../.meteor/packages/modules/.0.15.0.itei65.v8gbn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:52:22)
    at module (server/imports/index.js:1:8)
    at fileEvaluate (packages/modules-runtime.js:336:7)
    at Module.require (packages/modules-runtime.js:238:14)
    at Module.moduleLink [as link] (/Users/.../.meteor/packages/modules/.0.15.0.itei65.v8gbn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:52:22)
    at module (server/main.js:1:8)
    at fileEvaluate (packages/modules-runtime.js:336:7)
    at Module.require (packages/modules-runtime.js:238:14)
    at require (packages/modules-runtime.js:258:21)
    at /Library/WebServer/Documents/meteor/.../.meteor/local/build/programs/server/app/app.js:25654:1
    at /Library/WebServer/Documents/meteor/.../.meteor/local/build/programs/server/boot.js:401:38
    at Array.forEach (<anonymous>) {
  invalidKeys: [
    {
      name: 'arrayOfObjects.$[].name',
      type: 'keyNotInSchema',
      value: 'Marge Simpson'
    }
  ],
  validationContext: ValidationContext {
    _simpleSchema: SimpleSchema {
      pick: [Function: pickOrOmit],
      omit: [Function: pickOrOmit],
      _constructorOptions: [Object],
      _validators: [Array],
      _docValidators: [],
      _validationContexts: [Object],
      _cleanOptions: [Object],
      _schema: [Object],
      _depsLabels: {},
      _schemaKeys: [Array],
      _autoValues: [Array],
      _blackboxKeys: Set {},
      _firstLevelSchemaKeys: [Array],
      _objectKeys: [Object],
      messageBox: [MessageBox],
      version: 2
    },
    _schema: {
      arrayOfObjects: [Object],
      'arrayOfObjects.$': [Object],
      'arrayOfObjects.$.name': [Object]
    },
    _schemaKeys: [ 'arrayOfObjects', 'arrayOfObjects.$', 'arrayOfObjects.$.name' ],
    _validationErrors: [ [Object] ],
    _deps: {}
  },
  sanitizedError: errorClass [Error]: arrayOfObjects.$[].name is not allowed by the schema (arrayOfObjects.$[].name) in test update [400]
      at getErrorObject (packages/aldeed:collection2/collection2.js:497:28)
      at doValidate (packages/aldeed:collection2/collection2.js:463:13)
      at Collection.Mongo.Collection.<computed> [as update] (packages/aldeed:collection2/collection2.js:196:14)
      at module (imports/api/catalog/catalog.js:559:6)
      at fileEvaluate (packages/modules-runtime.js:336:7)
      at Module.require (packages/modules-runtime.js:238:14)
      at Module.moduleLink [as link] (/Users/.../.meteor/packages/modules/.0.15.0.itei65.v8gbn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:52:22)
      at module (server/imports/test.js:1:661)
      at fileEvaluate (packages/modules-runtime.js:336:7)
      at Module.require (packages/modules-runtime.js:238:14)
      at Module.moduleLink [as link] (/Users/.../.meteor/packages/modules/.0.15.0.itei65.v8gbn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:52:22)
      at module (server/imports/index.js:1:8)
      at fileEvaluate (packages/modules-runtime.js:336:7)
      at Module.require (packages/modules-runtime.js:238:14)
      at Module.moduleLink [as link] (/Users/.../.meteor/packages/modules/.0.15.0.itei65.v8gbn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:52:22)
      at module (server/main.js:1:8)
      at fileEvaluate (packages/modules-runtime.js:336:7)
      at Module.require (packages/modules-runtime.js:238:14)
      at require (packages/modules-runtime.js:258:21)
      at /Library/WebServer/Documents/meteor/.../.meteor/local/build/programs/server/app/app.js:25654:1
      at /Library/WebServer/Documents/meteor/.../.meteor/local/build/programs/server/boot.js:401:38
      at Array.forEach (<anonymous>) {
    isClientSafe: true,
    error: 400,
    reason: 'arrayOfObjects.$[].name is not allowed by the schema (arrayOfObjects.$[].name) in test update',
    details: '[{"name":"arrayOfObjects.$[].name","type":"keyNotInSchema","value":"Marge Simpson"}]',
    message: 'arrayOfObjects.$[].name is not allowed by the schema (arrayOfObjects.$[].name) in test update [400]',
    errorType: 'Meteor.Error'
  }
}

If you go down the path of error’s, after 3 or so you might end up with a few more lines in your schema, which are clearly wrong, but “fix” the issue:

  ...
  'arrayOfObjects.$[]': Array,
  'arrayOfObjects.$[].$': Object, 
  'arrayOfObjects.$[].name': String,

Versions of stuff: Meteor: 1.10.2 aldeed:collection2: 3.0.6 simpl-schema: 1.7.3

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:11

github_iconTop GitHub Comments

2reactions
kfritschcommented, Mar 30, 2021

Made a PR https://github.com/longshotlabs/node-mongo-object/pull/12 to add support for any positional operators as described in my earlier comment

2reactions
aldeedcommented, May 20, 2020

Yeah basically all the operators that existed when the package was written are supported but there’s never been an effort to add support for newer operators like $[]. $[<id>] is not supported either. It would probably be good to list out all that are supported in the readme.

I’ll keep this open as an enhancement request if someone wants to make it work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Update Operators — MongoDB Manual
In MongoDB 4.4 and earlier, update operators process all document fields in lexicographic order. Consider this example $set command: { $set: { "a....
Read more >
Updating installed Operators | OpenShift Container Platform 4.9
The subscription of an installed Operator specifies an update channel that tracks and receives updates for the Operator. You can change the update...
Read more >
Chapter 13. Upgrading the Quay Operator Overview
Upgrades are supported from previous versions of the Operator which used the QuayEcosystem API for a limited set of configurations. To ensure that...
Read more >
System Update - SupportPal Documentation
System update enables you to safely and easily update your help desk via the operator panel, without needing to manually download the update...
Read more >
Upgrading foundational services from an operator release - IBM
You can upgrade foundational services that were installed in a prior release. Supported upgrade paths. You can upgrade only the following supported paths:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found