Rachomir
Rachomir

Reputation: 280

MongoDB issue with updating nested few times object

Hey guys I have got a problem with updating value of key in the collection using Meteor / Mongo

data: {
      'id': id,
      'fb_id': fbId,
      'name': fbName,
      'access_token': fbAccessToken,
      'symbol': fbSymbol,
      'config': {
        'get_started': {
          'payload': getStarted
        },
        'persistent_menu': [
          {
            'locale': 'default',
            'call_to_actions': [
              {
                'type': 'postback',
                'title': persistentMenu1,
                'payload': 'menu'
              },
              {
                'type': 'postback',
                'title': persistentMenu2,
                'payload': 'knowledge_base'
              },
              {
                'type': 'nested',
                'title': persistentMenu3,
                'call_to_actions': [
                  {
                    'type': 'postback',
                    'title': nestedPersistentMenu1,
                    'payload': 'subscription'
                  },
                  {
                    'type': 'postback',
                    'title': nestedPersistentMenu2,
                    'payload': 'tth'
                  }
                ]
              }
            ],
            'composer_input_disabled': false
          },
          {
            'locale': 'pl_PL',
            'call_to_actions': [
              {
                'type': 'postback',
                'title': persistentMenu1,
                'payload': 'menu'
              },
              {
                'type': 'postback',
                'title': persistentMenu2,
                'payload': 'knowledge_base'
              },
              {
                'type': 'nested',
                'title': persistentMenu3,
                'call_to_actions': [
                  {
                    'type': 'postback',
                    'title': 'Subskrypcja',
                    'payload': 'subscription'
                  },
                  {
                    'type': 'postback',
                    'title': 'Konsultant',
                    'payload': 'tth'
                  }
                ]
              }
            ],
            'composer_input_disabled': false
          }
        ],
        'greeting': [
          {
            'locale': 'default',
            'text': somethingNew
          },
          {
            'locale': 'pl_PL',
            'text': greetingsText
          }
        ]
      },
      'created_at': '2017-06-05T06:00:37.759455Z',
      'updated_at': updatedAt
    }
  })

I need to get to second element in "greeting" array and change value of 'text' which is "greetingsText" at the moment

I tried to do so using console in the browser by typing this code

Collection.update({_id: "some_id"}, {$set: {fanpageInfo: {config: {"greeting.2": {text: "tata"}}}}}) but it doesnt work unfortunately

there is error saying "update failed:

MongoError: The dotted field 'greeting.2' in 'fanpageInfo.config.greeting.2' is not valid for storage." 

And to be honest I am not sure if I am targeting it properly - I checked docs and asked google but there are just simple tutorials.

Thanks in advance for any help

Upvotes: 1

Views: 111

Answers (2)

Ankur Soni
Ankur Soni

Reputation: 6008

FIND QUERY:

You need to go through these links as below:

Meteor Specific find-nth-element-of-array-in-mongo-collection-meteor

Mongo Specific get-n-th-element-of-an-array-in-mongodb

Simple for finding you need to use slice like ChatRooms.findOne( {}, { chatIds: { $slice: 1 } } );

UPDATE QUERY

You can try

Collection.update({_id: "some_id"}, {$set: {'config.greeting.1.text': 'tata'}});

If there is anything at all like fanpageInfo as parent field of config then update query shall be Collection.update({_id: "some_id"}, {$set: {'fanpageInfo.config.greeting.1.text': 'tata'}});

I ran your above data in mongodb and it works fine with above query. Image is below

enter image description here

For more : Click Here

Upvotes: 0

Styx
Styx

Reputation: 10076

First, your greeting array has only 2 elements, so by using greeting.2 you're attempting to modify third element (zero-based indexes).

Second, you should use dot notation to modify just one particular field:

{ $set: { "fanpageInfo.config.greeting.1.text": "tata" } }

I've changed 2 to 1 in this update. If you will attempt to use 2 instead — it will create another document in array with just text: "tata" inside.

Upvotes: 1

Related Questions