我正在尝试从使用 $in 运算符在 ArrayList 中传递的 User 集合中按顺序检索对象列表。但我没有按顺序排列对象。
List<String> companyProfiledb = mongotemplate.findDistinct(query, "userID", CompanyProfile.class, String.class);
System.out.println(companyProfiledb);
Query userquery = new Query(Criteria.where("userID").in(companyProfiledb));
System.out.println(userquery);
List<User> userdb = mongotemplate.find(userquery, User.class, "User");
System.out.println(userdb);
当我在控制台中打印时 -
来自 CompanyProfile 的用户 ID 的数组列表:
System.out.println(companyProfiledb);
---> [3, 1, 5]
使用$in运算符查询userID的ArrayList:
System.out.println(userquery);
---> 查询:{ "userID": { "$in": ["3", "1", "5"] } },字段:{ },排序:{ }
从数据库检索文档列表后:
System.out.println(userdb);
---> [用户(userID=1)、用户(userID=3)、用户(userID=5)]
我期望列表的输出为
[用户(userID=3)、用户(userID=1)、用户(userID=5)]
最佳答案
从查询中删除排序
查询:{ "userID": { "$in": ["3", "1", "5"] } }, 字段: { }
关于java - 如何使用 Spring Boot 按顺序检索从 MongoDB 中的 ArrayList 传递的对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642896/