如何删除 RethinkDB 中基于组的一组数据 with Python
How to delete a set of data based on group in RethinkDB with Python
我有一个 table 的乐器,我想按 category
来拆分它们,计算它们的数量,如果 category
组大于一个数字,则删除其中的所有乐器那个小组。我目前的代码是:
from rethinkdb import RethinkDB
from faker import Faker
from faker_music import MusicProvider
from random import random
from time import sleep
fake = Faker()
fake.add_provider(MusicProvider)
r = RethinkDB()
r.connect( "localhost", 28015).repl()
try:
r.db("test").table_drop("instruments").run()
except:
pass
r.db("test").table_create("instruments").run()
def instrument()->dict:
instrument = {"name":fake.music_instrument(),"category":fake.music_instrument_category()}
return instrument
initial = [instrument() for _ in range(3)]
r.table("instruments").insert(initial).run()
while True:
check = random()
if check < 0.5 and check >0.25:
r.table("instruments").insert(instrument()).run()
if check < 0.25:
cursor = r.table("instruments").group("category").count().gt(3).filter.delete().run()
sleep(1)
其中 r.table("instruments").group("category").count().gt(3).filter.delete().run()
不起作用,但表示我正在努力实现的目标。
好的,这有效:
from rethinkdb import RethinkDB
from faker import Faker
from faker_music import MusicProvider
from random import random
from time import sleep
fake = Faker()
fake.add_provider(MusicProvider)
r = RethinkDB()
conn = r.connect( "localhost", 28015)
try:
r.db("test").table_drop("instruments").run(conn)
except:
pass
r.db("test").table_create("instruments").run(conn)
def instrument()->dict:
instrument = {"name":fake.music_instrument(),"category":fake.music_instrument_category()}
return instrument
initial = [instrument() for _ in range(3)]
r.table("instruments").insert(initial).run(conn)
while True:
check = random()
if check < 0.5 and check >0.25:
r.table("instruments").insert(instrument()).run(conn)
counts ={}
counts = dict(r.table("instruments").group("category").count().run(conn))
to_rm =[]
for category, count in counts.items():
if count >2:
r.table("instruments").filter( {"category":category}).delete().run(conn)
sleep(1)
但我确信有更精简的功能解决方案,如果有人知道请评论。
我有一个 table 的乐器,我想按 category
来拆分它们,计算它们的数量,如果 category
组大于一个数字,则删除其中的所有乐器那个小组。我目前的代码是:
from rethinkdb import RethinkDB
from faker import Faker
from faker_music import MusicProvider
from random import random
from time import sleep
fake = Faker()
fake.add_provider(MusicProvider)
r = RethinkDB()
r.connect( "localhost", 28015).repl()
try:
r.db("test").table_drop("instruments").run()
except:
pass
r.db("test").table_create("instruments").run()
def instrument()->dict:
instrument = {"name":fake.music_instrument(),"category":fake.music_instrument_category()}
return instrument
initial = [instrument() for _ in range(3)]
r.table("instruments").insert(initial).run()
while True:
check = random()
if check < 0.5 and check >0.25:
r.table("instruments").insert(instrument()).run()
if check < 0.25:
cursor = r.table("instruments").group("category").count().gt(3).filter.delete().run()
sleep(1)
其中 r.table("instruments").group("category").count().gt(3).filter.delete().run()
不起作用,但表示我正在努力实现的目标。
好的,这有效:
from rethinkdb import RethinkDB
from faker import Faker
from faker_music import MusicProvider
from random import random
from time import sleep
fake = Faker()
fake.add_provider(MusicProvider)
r = RethinkDB()
conn = r.connect( "localhost", 28015)
try:
r.db("test").table_drop("instruments").run(conn)
except:
pass
r.db("test").table_create("instruments").run(conn)
def instrument()->dict:
instrument = {"name":fake.music_instrument(),"category":fake.music_instrument_category()}
return instrument
initial = [instrument() for _ in range(3)]
r.table("instruments").insert(initial).run(conn)
while True:
check = random()
if check < 0.5 and check >0.25:
r.table("instruments").insert(instrument()).run(conn)
counts ={}
counts = dict(r.table("instruments").group("category").count().run(conn))
to_rm =[]
for category, count in counts.items():
if count >2:
r.table("instruments").filter( {"category":category}).delete().run(conn)
sleep(1)
但我确信有更精简的功能解决方案,如果有人知道请评论。