In Jongo, how to find multiple documents from Mongodb by a list of IDs

ⅰ亾dé卋堺 提交于 2019-12-04 13:59:55
yves amsellem

I see two options to achieve a find on multiple ids:

// 1. find with an array of ids
ObjectId[] ids = {id, id, id};
collection.find("{_id:{$in:#}}", ids).as(Friend.class);

// 2.find a list of ids
collection.find("{_id:{$in:[#, #, #]}}", id, id, id).as(Friend.class);

findOne offers a convenience method with an ObjectId and, if you use an annotated String instead of an ObjectId, the Oid.withOid method transforms your String into an ObjectId.

But, in the end, this convenience method input is transformed into a regular stringified query. So if the convenience don't fit your need, try a query instead.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!