Mongodb 从正则表达式选择的数组中删除特定元素
在我的mongo集合中,每个文档都包含一个数组,我想从正则表达式选择的数组中删除一些元素。例如,这里是我的示例文档-Mongodb 从正则表达式选择的数组中删除特定元素,mongodb,Mongodb,在我的mongo集合中,每个文档都包含一个数组,我想从正则表达式选择的数组中删除一些元素。例如,这里是我的示例文档- { "_id" : ObjectId("590af6f3d8bf623afc5d63a6"), "tweet_ids" : [ "795630746077634560", "776444730225729536", "788737667261427713", "788736764592742401",
{
"_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"
]
}
我想删除那些有以下正则表达式的tweet-
[a-z].* hat [a-z].* retweetet
我找到了$regex
,它将选择整个文档,我也看到了答案,但问题是,数组包含json对象,他们正在删除该对象的特定键值。
我该怎么做 您也可以对任何查询条件使用
$pull
,因此在本例中:
db.test.update({}, { $pull: { tweets: /[a-z].* hat [a-z].* retweetet/ }})
db.brexit_nodes_with_tweets.update({},{$pull:{$tweets:{$regex:[a-zA-Z0-9].[a-zA-Z0-9].*retweetet},{multi:true})
在的帮助下,这不是与我之前发布的答案有效地相同吗?不,你的答案不起作用,带“/”的答案不起作用。什么不起作用?我想你在这里用的是JavaScript?当我尝试它时,它工作得很好。没什么大不了的,只是好奇而已。也许你能看到。我正在mongo shell上运行代码