1

Hi i am attmepting to remove anything after the first value e.g.

Collection:{

"_id' :.....
"UrlId" : "5dfc1aa2986b7c30f3398be4",
    "coverInput" : 
           [
            "https://test.com/s/files/1/00351576213050",
            "https://test.com.au"
           ],

}

what I want to replace the document with is:

Collection:{

"_id' :.....
"UrlId" : "5dfc1aa2986b7c30f3398be4",
    "coverInput" : 
           [
            "https://test.com/s/files/1/00351576213050"
           ],

}

Im sure this is something simple. I tried this but got an error:

db.test.aggregate([
{
    $addFields: {
        coverInput: { $substr: [ "$coverInput", 0, { $indexOfBytes: [ "$coverInput", "," ] } ] }
    }
}
]).forEach( doc => db.test.updateOne( { _id: doc._id }, { $set: { coverInput: doc.coverInput } } ) )

Thanks in advance!

1
  • $substr operator is used to work with strings. You have to use Aggregation array operators to work with array fields. Commented Jan 8, 2020 at 9:39

1 Answer 1

1

You can use below aggregation

db.collection.aggregate([
  { "$project": {
    "coverInput": {
      "$slice": ["$coverInput", 1]
    }
  }}
])

Or if you want to update the document

db.test.updateOne(
  { _id: doc._id },
  [{
    $set: { coverInput: { "$slice": ["$coverInput", 1] } }
  }]
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.