如何使用SPRING DATA从MONGODB中按值数组获取文档?
我的文档
{
"id": "5b18fdef89d67e272025f2e3",
"date": "2018-05-10 11:31:37",
"active": true,
"obsolete": false,
"tenant": {
"id": "5ad847e54925fb0fa4424e1a"
},
"plan": {
"id": "5ad7115f7b4152204c86fce6"
},
"log": [
"5b18fdef89d67e272025f2e4"
],
"lineItem": [
"5b18fdef89d67e272025f2e5",
"5b18fdef89d67e2720259899",
"5b18fdef89d67e272025sd5s"
]
}
{
"id": "5b18fdef89d67e272025f232",
"date": "2018-05-12 11:31:37",
"active": true,
"obsolete": false,
"tenant": {
"id": "5ad847e54925fb0fa4424e1a"
},
"plan": {
"id": "5ad7115f7b4152204c86fce6"
},
"log": [
"5b18fdef89d67e272025f23434"
],
"lineItem": [
"5b18fdef89d67e272025f111",
"5b18fdef89d67e2720259222",
"5b18fdef89d67e272025s333"
]
}
我想按lineItem数组过滤文档。如果我将lineItem的值设置为“ 5b18fdef89d67e2720259222”,则它将是包含相同lineitem的返回文档。
模型类
@Document(collection = "trn_inventory")
public class Inventory {
@Id
private String id;
private String date;
private List<String> lineitem;
private String tenant, plan;
private List<String> log;
private boolean active, obsolete;
//getters and setters
}
存储库
@Repository
public interface InventoryRep extends MongoRepository<Inventory, String> {
public List<Inventory> findByLineitemIn(String lineitem);
}
答案 0 :(得分:0)
我在存储库中犯了错误。花了很长时间后,我自己找到了(愚蠢的编码器)。问题在于 findByLineitemIn 方法的参数是应该为列表类型的字符串。
这是已更正的存储库...
@Repository
public interface InventoryRep extends MongoRepository<Inventory, String> {
public List<Inventory> findByLineitemIn(List<String> lineitem);
}
这有多愚蠢。不是吗?我认为这将对像我这样的其他编码人员有所帮助。 ;)