MongoDB Shell - Insert new Array field

The following code does not seem to update the 2 documents I have in this collection:

db.playerTest.updateMany({},{$set: {"clubs": [[ { "clubId": ObjectId("6076030465508936f00e086c")}, {"name": "Augusta National Golf Club"}, {"nickName": "Augusta"}, {"logoPath": "augusta.png"}]]}})

It needs to be an Array of Objects

It returns the following output, which suggests the syntax is good, but is not being applied:

{ acknowledged: true,
  insertedId: null,
  matchedCount: 0,
  modifiedCount: 0,
  upsertedCount: 0 }

With the same code I get:

> db.playerTest.find()
{ "_id" : 0 }
{ "_id" : 1 }
> db.playerTest.updateMany({},{$set: {"clubs": [[ { "clubId": ObjectId("6076030465508936f00e086c")}, {"name": "Augusta National Golf Club"}, {"nickName": "Augusta"}, {"logoPath": "augusta.png"}]]}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.playerTest.find()
{ "_id" : 0, "clubs" : [ [ { "clubId" : ObjectId("6076030465508936f00e086c") }, { "name" : "Augusta National Golf Club" }, { "nickName" : "Augusta" }, { "logoPath" : "augusta.png" } ] ] }
{ "_id" : 1, "clubs" : [ [ { "clubId" : ObjectId("6076030465508936f00e086c") }, { "name" : "Augusta National Golf Club" }, { "nickName" : "Augusta" }, { "logoPath" : "augusta.png" } ] ] }

I can see one of 2 things

  1. is that your collection that contains the 2 documents is NOT named playerTest
  2. you are not using the database that contains the playerTest collection

Share the output of:

  1. db.playerTest.find()
  2. db
1 Like

I feel such a fool… collection wasn’t camelCase! Doh!

Now the correct data is being applied, I also corrected that the previous syntax was created Array's of Array's. Updated to. by removing a set of square braces:

db.playertest.updateMany({},{$set: {"clubs": [ { "clubId": ObjectId("6076030465508936f00e086c"), "name": "Augusta National Golf Club", "nickName": "Augusta", "logoPath": "augusta.png"}]}})

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.