Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
画像ギャラリーを作っています。各画像には _id があり、画像を表示しているときに、次の 3 つの画像と前の 3 つの画像が必要です。これをmongodbクエリで取得するにはどうすればよいですか? これはソートできないので、_id によるソートを使用できると思います。多分mapReduceまたはいくつかの特定のクエリで?
はい、並べ替えることができます_id。2 つのクエリを使用します。1 つは前の 3 つ、もう 1 つは後の 3 つです。
_id
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