2

in my mongo collection, every document contains an array and i want to delete some element from that array selected by regular expression. For example here is my sample document-

{
    "_id" : ObjectId("590af6f3d8bf623afc5d63a6"),
    "tweet_ids" : [
        "795630746077634560",
        "776444730225729536",
        "788737667261427713",
        "788736764592742401",
        "788734782217879552",
        "799242145433415681",
        "824587491902685184",
        "803544932749295616",
        "781746625953890304",
        "826756800980590592",
        "847718025918726145",
        "848884360451424258",
        "809679010556997632"
    ],
    "user_id" : "776432864673923077",
    "color" : "b",
    "tweets" : [
        "NievesPérezSolórzano hat AlbertSánchezGraells retweetet",
        "Our thoughts on #Brexit #EUref @michcini @OUPPolitics The UK’S EU\nReferendum.The background, the vote and the impactpic.twitter.com/cy6h1mXT68",
        "NievesPérezSolórzano hat Raphaël Nowak retweetet",
        "NievesPérezSolórzano hat Esther Dermott retweetet",
        "NievesPérezSolórzano hat AlbertSánchezGraells retweetet",
        "2nd #eureferendum offers choice between #RemainINEU/Hard #Brexit minus https://theconversation.com/a-second-brexit-vote-is-a-real-possibility-now-heres-why-it-should-happen-68862 … @CardiffLaw @ConversationUK @bristolunilaw",
        "5 days to discuss #Article50 vs. 41 days #Maastricht 25 days #Lisbon & 39 original membership process #Brexit #parliamentsovereign #shameful",
        "Stijn Smismans: #Article50 QMV in #EUCouncil & #EP consent could be agreement 4 country 2 remain #Brexit #miller @UKSupremeCourt @ukcla",
        "New event Brexit, the UK & Bristol, 8th November @ 6pm @wshed #Brexit #Bristol @cpe_bristol @aejuncos @michcini @ShelleyNania @SPAISBristol",
        "An excellent account of how negotiations in the EU work by Sir Ivan Rogers @CommonsEU #Brexit",
        "NievesPérezSolórzano hat Rachel Minto retweetet",
        "NievesPérezSolórzano hat Noelle Quenivet retweetet",
        "NievesPérezSolórzano hat LSE Brexit retweetet"
    ]
}

I want to delete those tweets which have the following regex form -

[a-z].* hat [a-z].* retweetet

I found $regex, which will select the whole document and also i saw this answer but the problem is there, the array contains json object and they are deleting a particular key value of that object. How can I do this?

Community
  • 1
  • 1
sovon
  • 877
  • 2
  • 12
  • 28

2 Answers2

1

You can use $pull with any query condition as well, so in this case:

db.test.update({}, { $pull: { tweets: /[a-z].* hat [a-z].* retweetet/ }})
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
1

db.brexit_nodes_with_tweets.update({}, { $pull: { tweets:{$regex:"[a-zA-Z0-9].* hat [a-zA-Z0-9].* retweetet"}}},{multi:true})

with the help of this

sovon
  • 877
  • 2
  • 12
  • 28
  • Isn't this effectively the same as the answer I posted earlier? – JohnnyHK May 22 '17 at 14:06
  • No, your answer did not worked, with '//' did not worked. – sovon May 22 '17 at 20:36
  • What didn't work about it? I assume you're using JavaScript here? It worked fine when I tried it. No biggie, just curious. – JohnnyHK May 22 '17 at 20:38
  • may be you can see [this](https://stackoverflow.com/questions/6297631/mongodb-pull-matching-with-regexp-not-working) . I am running the code on mongo shell – sovon May 22 '17 at 21:17