If I have a structure like this in the document
...
updatedAt: 2021-01-17T16:44:28.824+00:00
vid: (Array)
0: (Array)
0: "adfsdfasfd"
1: "this is some sample text"
2: "https://example.com"
1: (Array)
0: "gfjghjhjgh"
1: "this is another sample text"
2: "https://example2.com"
...
how do I update for instance the array vid[0][0] knowing "adfsdfasfd" ?
in order to get the whole vid[0][0] array I'm using this:
const foundVid = await db.collection('users').find({vid: {$elemMatch: {$elemMatch: {$in: ["adfsdfasfd"]}}}}).project({"vid.$": 1, "_id": 0 }).toArray()
which console.log(foundVid):
{
vid: [
[
"adfsdfasfd"
"this is some sample text"
"https://example.com"
]
]
}
then I tried both $set and $addToSet like:
let update = {
vid: [
[foundVid[0].vid[0][0], foundVid[0].vid[0][1], foundVid[0].vid[0][2], views, likes, date]
]
}
await db.collection('users').updateOne(
{
vid: {
$elemMatch: {
$elemMatch: {
$in: [
"adfsdfasfd"
]
}
}
}
},
{ $addToSet : update }
)
but it doesn't seem to work..
EDIT: what I'm trying to accomplish is to update one array elements by knowing it's first string (which is an id), so after updating my vid[0][0] as described above it should look like:
...
updatedAt: 2021-01-17T16:44:28.824+00:00
vid: (Array)
0: (Array)
0: "adfsdfasfd"
1: "this is some sample text"
2: "https://example.com"
3: 1000
4: 100
5: <a date here>
1: (Array)
0: "gfjghjhjgh"
1: "this is another sample text"
2: "https://example2.com"
...
EDIT 2: I'm sorry I thought was implicit with the term "update" instead of changing the array, I'd need to update it as if it was a $set. This is why I went towards recreating the array (let update = ...
) with the first three current values + updated variables views, likes, date
.
The problem with $set is that it appears to update the whole vid array with only this array in it resulting in:
...
updatedAt: 2021-01-17T16:44:28.824+00:00
vid: [
0: [
0: "adfsdfasfd"
1: "this is some sample text"
2: "https://example.com"
3: 1000
4: 100
5: <a date here>
]
]
...
question from:
https://stackoverflow.com/questions/65910567/mongodb-update-array-of-array-knowing-its-one-string