Currently a document looks like this:
{
"_id": {
"$oid": "630f3c32c1a580642a9ff4a0"
},
"title": "This is a title",
"slug": "this-is-a-title",
"Post": "this is a post",
"iframe": "<iframe width="560" height="315" src="https://www.youtube.com/embed/RzVvThhjAKw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>"
}
But I want it like this:
"_id": {
"$oid": "630f3c32c1a580642a9ff4a0"
},
"title": "This is a title",
"slug": "this-is-a-title",
"Post": "this is a post",
"iframe":"https://www.youtube.com/embed/RzVvThhjAKw"
}
How to remove all other characters other than URL in iframe field?
CodePudding user response:
One option is to use an update pipeline with $split
and $regexMatch
for this:
db.collection.update({},
[
{$set: {
iframe: {
$first: {$filter: {
input: {$split: ["$iframe", " "]},
cond: {$regexMatch: {input: "$$this", regex: "src="}}
}}
}
}
},
{$set: {iframe: {$last: {$split: ["$iframe", "="]}}}}
])
See how it works on the playground example