Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

Mongodb update array of array knowing its one string

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It does seem like you're pretty close here. As you point out, to match that value in the array of arrays, you would run this query:

db.collection.find({
  "vid": {
    $elemMatch: {
      $elemMatch: {
        $in: [
          "adfsdfasfd"
        ]
      }
    }
  }
})

But to update it, you would need:

db.collection.update({
  "vid": {
    $elemMatch: {
      $elemMatch: {
        $in: [
          "adfsdfasfd"
        ]
      }
    }
  }
},
{
  "$push": {
    "vid.$": {
      $each: [
        1000,
        100,
        new Date()
      ]
    }
  }
})

The key here is the positional $ operator.

Playground


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...