如何使用 vertx-mongo 客户端执行 find( id : { $in : [ list ] } ) [英] How do you execute a find( id : { $in : [ list ] } ) using vertx-mongo client
问题描述
Vertx mongo 客户端期望查询参数作为 JSON 对象.到目前为止,我主要做了类似于下面的事情
Vertx mongo client expects query parameter as a JSON object. So far mostly I have done something similar to below
JsonObject queryParam = new JsonObject().put("id", 123);
mongoClient.find("collection", queryParam, asyncResult -> {
if (asyncResult.succeeded()) {
// Do something
promise.complete();
} else {
promise.fail(asyncResult.cause());
}
});
我会根据我的需要建立上面的查询参数.
I would build up the above query param according to my needs.
现在我有一个服务可以返回一个 ID 列表.我必须查询这些匹配 ID 的集合.理想情况下,在 mongo shell 中,我会执行 db.getCollection("collection").find( id: { $in: [1, 2, 3, 4] } )
Now I have a service that returns me a list of IDs. I have to query the collection for those matching IDs. Ideally in a mongo shell I would do db.getCollection("collection").find( id: { $in: [1, 2, 3, 4] } )
我检查了 vertx 文档,甚至在 vertx mongoClient.java 文件中搜索了一点.谷歌搜索没有给我在 vertx 中做到这一点的解决方案.如何在不编写大量代码的情况下实现这一目标?
I checked vertx documents and even searched a bit in vertx mongoClient.java file. Google search didn't yield me solutions to do this in vertx. How can I achieve this without writing a heavy code?
推荐答案
好的,这很简单,我只需要稍微清醒一下就能注意到这一点.复杂的查询可以分解为一个 Json 对象.
Okay, it's pretty straight forward and I just needed to clear my head a bit to notice that. The complex query can be broken down into a Json Object.
JsonObject queryParam = new JsonObject()
.put("id", new JsonObject().put("$in", listOfIds)); // ArrayList<Long>
我可以继续使用带有此 queryParam 的简单查找查询.
I can go ahead and use simple find query with this queryParam.
mongoClient.find("collection", queryParam, asyncResult -> {
// do something
});
这篇关于如何使用 vertx-mongo 客户端执行 find( id : { $in : [ list ] } )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!