是否有办法识别MongoDB集合中的倒数第二个对象?
我得到了最后一个:
import pandas as pd
from collections import defaultdict
import random
n_keys = 200
values_per_key = 200
n_unique_values = 200
total_rows = n_keys * values_per_key
keys = [i//values_per_key for i in range(total_rows)]
values = [random.randint(0, n_unique_values-1) for i in range(total_rows)]
data = {'key': keys, 'value': values}
df = pd.DataFrame(data)
#df = pd.DataFrame({'key': [0, 0, 1, 1, 2, 2, 2],
# 'value': ['A', 'A', 'A', 'B', 'C', 'B', 'B']})
counts = defaultdict(list)
values = df['value'].value_counts().index
keys = sorted(df['key'].value_counts().index)
for key in keys:
for value in values:
# the following line makes this super slow
ind = (df['key'] == key) & (df['value'] == value)
counts[value].append(ind.sum())
print(pd.DataFrame(counts, index=keys))
但我怎样才能得到倒数第二个呢?
感谢您的帮助
答案 0 :(得分:0)
您可以在此使用limit
和skip
collection.find({}, {sort: {Date: -1}, limit:1}).fetch();
将为您提供最后一条记录,
collection.find({}, {sort: {Date: -1}, limit:1, skip:1}).fetch();
为您提供倒数第二个记录