【问题标题】:Too many parameters error on the following $in query以下 $in 查询中的参数过多错误
【发布时间】:2016-08-23 15:38:38
【问题描述】:

我正在使用 jongo API - org.jongo.MongoCollection 是类。

我有对象 id 列表,并转换为与 ObjectId[] 相同并尝试如下查询

collection.find("{_id:{$in:#}}", ids).as(Employee.class);

The query throws the exception - "java.lang.IllegalArgumentException: Too      
many parameters passed to query: {"_id":{"$in":#}}"

查询无法按照 URL In Jongo, how to find multiple documents from Mongodb by a list of IDs 中指定的方式工作

关于如何解决的任何建议?

谢谢。

【问题讨论】:

    标签: java mongodb mongodb-query jongo


    【解决方案1】:

    List 试试看,如docs 所示:

    List<String> ages = Lists.newArrayList(22, 63);
    friends.find("{age: {$in:#}}", ages); //→ will produce {age: {$in:[22,63]}}
    

    例如,以下我制作的快速而肮脏的 sn-p 现在对我有用(我使用较旧的详细语法,因为我目前在这样的系统上......)

    List<ObjectId> ids = new ArrayList<ObjectId>();
    ids.add(new ObjectId("57bc7ec7b8283b457ae4ef01"));
    ids.add(new ObjectId("57bc7ec7b8283b457ae4ef02"));
    ids.add(new ObjectId("57bc7ec7b8283b457ae4ef03"));
    int count = friends.find("{ _id: { $in: # } }", ids).as(Friend.class).count();
    

    【讨论】:

    • 这不起作用,因为 collection.find 返回 MongoCursor 并抛出强制转换异常。但是以下工作正常。如何将 MongoCursor 转换为用户定义的列表。我正在使用以下 List collectionsAsList = new ArrayList(); MongoCursor cursorCollection = collection.find("{ _id: { $in: # } }", ids).as(Friends.class); while (cursorCollection.hasNext()) { collectionsAsList.add(cursorCollection.next()); }
    • 它确实有效,但你当然必须有一个合适的 Friend 类 - 但这也不是你的问题的主题,但它是如何传递 ID 列表的方式,这就是质疑这是否适合你
    • 效果很好,非常感谢。我有合适的类,但它的类型转换是否正确,因为它当前会引发编译错误。可以在这里放一个代码sn-p吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2022-10-05
    相关资源
    最近更新 更多