Issue with update in minimongo: "Key $ must not start with '$'"

Just updated a project to meteor 1.5.2_1

And I was able to do an $addToSet operation like this:

return Calls.update({ _id: callId }, {
                $addToSet: {
                    units: {
                        unit: callSign,
                        currentStatus: "AS",
                        statusColor: "yellow",
                        textColor: "black",
                        assignable: false,
                        assignedBy: Meteor.users.findOne(this.userId).username,
                        assignedOn: new Date(),
                    },
                }
            });

but now, when I try to update this collection with this:

return Calls.update({ _id: callId, "units.unit": callSign }, {
            $set: {
                "units.$.statusColor": "#74eeea",
                "units.$.textColor": "black",
                "units.$.currentStatus": "ER",
                "units.$.enrouteBy": Meteor.users.findOne(this.userId).username,
                "units.$.enrouteOn": new Date(),
                }
        }, {
            upsert: true
        });

I get the error

Key $ must not start with ‘$’

I’m not sure if this is referring to $set or to the ‘$’ in the middle of each key I’m updating. I’m guessing the latter.

I haven’t changed anything about this, and it was running fine on 1.5.0…so what am I missing?

1 Like

Just to be certain I went back and made sure all my dependencies were working in my project on 1.5, and as far as I can tell non issues. Then I ran the upgrade again, and still get the same error in 1.5.2_1. Has something changed in the way you should handle updating using $set and nested documents? I use the

<key>.$.<sub-key> : value

form to do this because I’m updating a specific subdocument and it’s values in the collection. It seems this is where the error is coming from.

[EDIT] - I meant to put the log from the console here…sorry, got ahead of myself.

I20170929-05:56:56.876(-5)? Exception while invoking method 'call.enrouteUnit' MinimongoError: Key $ must not start with '$'
I20170929-05:56:56.955(-5)?     at Object.Future.wait (/Users/brianmcgonagill/.meteor/packages/meteor-tool/.1.5.2_1.sp8lzj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:446:16)
I20170929-05:56:56.956(-5)?     at [object Object].<anonymous> (packages/meteor.js:209:22)
I20170929-05:56:56.958(-5)?     at [object Object].MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:742:49)
I20170929-05:56:56.959(-5)?     at [object Object].update (packages/mongo/collection.js:550:29)
I20170929-05:56:56.960(-5)?     at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/aldeed_collection2-core.js:232:19)
I20170929-05:56:56.961(-5)?     at [object Object].Meteor.methods.call.enrouteUnit (imports/api/calls.js:249:22)
I20170929-05:56:56.962(-5)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1768:12)
I20170929-05:56:56.964(-5)?     at packages/ddp-server/livedata_server.js:719:19
I20170929-05:56:56.965(-5)?     at [object Object].EVp.withValue (packages/meteor.js:1134:15)
I20170929-05:56:56.965(-5)?     at packages/ddp-server/livedata_server.js:717:46
I20170929-05:56:56.967(-5)?     - - - - -
I20170929-05:56:56.969(-5)?     at MinimongoError (packages/minimongo/common.js:1054:17)
I20170929-05:56:56.970(-5)?     at assertIsValidFieldName (packages/minimongo/local_collection.js:1883:11)
I20170929-05:56:56.971(-5)?     at findModTarget (packages/minimongo/local_collection.js:1977:7)
I20170929-05:56:56.973(-5)?     at packages/minimongo/local_collection.js:1158:24
I20170929-05:56:56.974(-5)?     at Array.forEach (native)
I20170929-05:56:56.975(-5)?     at packages/minimongo/local_collection.js:1142:28
I20170929-05:56:56.976(-5)?     at Array.forEach (native)
I20170929-05:56:56.977(-5)?     at Function.LocalCollection._modify (packages/minimongo/local_collection.js:1132:27)
I20170929-05:56:56.982(-5)?     at Function.LocalCollection._createUpsertDocument (packages/minimongo/local_collection.js:935:19)
I20170929-05:56:56.983(-5)?     at [object Object].MongoConnection._update (packages/mongo/mongo_driver.js:534:38)
I20170929-05:57:07.375(-5)? Exception while invoking method 'call.enrouteUnit' MinimongoError: Key $ must not start with '$'
I20170929-05:57:07.376(-5)?     at Object.Future.wait (/Users/brianmcgonagill/.meteor/packages/meteor-tool/.1.5.2_1.sp8lzj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:446:16)
I20170929-05:57:07.377(-5)?     at [object Object].<anonymous> (packages/meteor.js:209:22)
I20170929-05:57:07.378(-5)?     at [object Object].MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:742:49)
I20170929-05:57:07.378(-5)?     at [object Object].update (packages/mongo/collection.js:550:29)
I20170929-05:57:07.379(-5)?     at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/aldeed_collection2-core.js:232:19)
I20170929-05:57:07.380(-5)?     at [object Object].Meteor.methods.call.enrouteUnit (imports/api/calls.js:249:22)
I20170929-05:57:07.380(-5)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1768:12)
I20170929-05:57:07.381(-5)?     at packages/ddp-server/livedata_server.js:719:19
I20170929-05:57:07.381(-5)?     at [object Object].EVp.withValue (packages/meteor.js:1134:15)
I20170929-05:57:07.382(-5)?     at packages/ddp-server/livedata_server.js:717:46
I20170929-05:57:07.382(-5)?     - - - - -
I20170929-05:57:07.383(-5)?     at MinimongoError (packages/minimongo/common.js:1054:17)
I20170929-05:57:07.383(-5)?     at assertIsValidFieldName (packages/minimongo/local_collection.js:1883:11)
I20170929-05:57:07.384(-5)?     at findModTarget (packages/minimongo/local_collection.js:1977:7)
I20170929-05:57:07.385(-5)?     at packages/minimongo/local_collection.js:1158:24
I20170929-05:57:07.386(-5)?     at Array.forEach (native)
I20170929-05:57:07.386(-5)?     at packages/minimongo/local_collection.js:1142:28
I20170929-05:57:07.387(-5)?     at Array.forEach (native)
I20170929-05:57:07.387(-5)?     at Function.LocalCollection._modify (packages/minimongo/local_collection.js:1132:27)
I20170929-05:57:07.388(-5)?     at Function.LocalCollection._createUpsertDocument (packages/minimongo/local_collection.js:935:19)
I20170929-05:57:07.389(-5)?     at [object Object].MongoConnection._update (packages/mongo/mongo_driver.js:534:38)

Thanks for any help or suggestions to try.

1 Like

I have found the issue in my code.

Simply put, MongoDB doesn’t allow the use of upsert for positional sub-document updating / inserting.

Basically, you need to run a query and see if your sub-document exists, and if not, inseert it (AddToSet or Push), and if it does, then update it normally.

3 Likes