在我的mongo集合中,每个文档都包含一个数组,我想从正则表达式选择的数组中删除一些元素。例如,这是我的示例文档 -
{
"_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"
]
}
我想删除那些具有以下正则表单形式的推文 -
[a-z].* hat [a-z].* retweetet
我找到了$regex
,它将选择整个文档,我也看到this回答,但问题在于,数组包含json对象,并且它们正在删除该对象的特定键值。
我怎样才能做到这一点?
答案 0 :(得分:1)
您也可以将$pull
用于任何查询条件,因此在这种情况下:
db.test.update({}, { $pull: { tweets: /[a-z].* hat [a-z].* retweetet/ }})
答案 1 :(得分:1)
db.brexit_nodes_with_tweets.update({},{$ pull:{tweets:{$ regex:" [a-zA-Z0-9]。* hat [a-zA-Z0-9]。* retweetet"}}},{多:真})
在this
的帮助下