1

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

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Rachomir
  • 280
  • 1
  • 5
  • 16

2 Answers2

1

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.

Styx
  • 9,863
  • 8
  • 43
  • 53
0

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

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
  • Thanks a lot for your help! Appreciated – Rachomir Sep 26 '17 at 14:42
  • Yes mate, and I also learned how to do it in the future :) I would love to give you a point next to answer, however my level is to low – Rachomir Sep 26 '17 at 14:47
  • 1
    Also never get demotivated with score, just look ahead to answer and help others, asking fantastic/idiotic questions never really matters. I too learnt something new from your question. So it is win-win situatoin. Make other people's life easy and inspiring. I too started from `ZERO`. So don't bother, be happy and helping here. Stackoverflow have great helping people like @Styx, @jankapankt, @michel floyd, @mikkel whom I know they solve issues immediately and unconditionally, I learnt from them to be unconditional and humble. So fly high :) – Ankur Soni Sep 26 '17 at 14:55