下一个和上一个文件 [英] Next and previous documents
本文介绍了下一个和上一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作图片库.每个图像都有一个_id,当我查看图像时,我想要接下来的3张图像和之前的3张图像.如何在mongodb查询中获得此信息?我认为我可以使用_id排序,因为这是无法排序的.也许使用mapReduce或某些特定查询?
I am making an image gallery. Each image has an _id and when I'm viewing an image I want the next 3 images and 3 before. How can I get this in a mongodb query? I think that I can use sort by _id because this is unsortable. Maybe with mapReduce or some specific query?
推荐答案
是的,您可以按_id
进行排序.使用两个查询:一个用于前三个查询,另一个用于后三个查询.
Yes, you can sort by _id
. Use two queries: one for the 3 before and one for the 3 after.
python中的示例:
An example in python:
my_id = coll.find()[20]['_id']
coll.find({ '_id': {'$lt': my_id}}).sort([('_id', -1)]).limit(3) # before
coll.find({ '_id': {'$gt': my_id}}).sort('_id').limit(3) # after
这篇关于下一个和上一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文