Help updating with bracket notation and updating array indexes

I have this set of data:

[
{
_id: new ObjectId(“6249b1abd4b9d2257d72fedf”),
‘223503056178905088’: {
UID: ‘223503056178905088’,
total: 8845455,
name: ‘Cigora /Matze#1368’,
rank: [Array],
joined: 1649019586262
},
‘236840013843791874’: {
UID: ‘236840013843791874’,
total: 6676639,
name: ‘viciousDom#2027’,
rank: [Array],
joined: 1648917920576
},
‘505732249715736576’: {
UID: ‘505732249715736576’,
total: 4541,
name: ‘Mulon#5667’,
rank: [Array],
joined: 1541018286580
}
}
]

In the “rank” the array is made of [“1234”, “”]
So how can I update rank[1]?

Also the main object key (the number) is queried by a variable, which in JS I’d be using bracket notation. But it doesnt seem to work in mongo.
Let userid = ‘505732249715736576’
[userid]rank <— shows error

In the documents it says to use dot notation wrapped in quotes. This still creates a key, doesnt access the properties.
‘[userid]rank’

You need to use positional arguements with
<array_name>.1.<array_field>

From the mongo shell you need to use the “dot” notation, as I show below to get a field inside an imbedded document name “userid” for example

> db.random.insert({"userid": {"name":"test_user", "number": 45}})
WriteResult({ "nInserted" : 1 })
> db.random.find({"userid.name": "test_user"})
{ "_id" : ObjectId("624ca0f94716311f6edceda5"), "userid" : { "name" : "test_user", "number" : 45 } }
1 Like