spring boot mongodb templete 兩個字段比較查詢

使用MongoTemplete比較兩個字段比較麻煩(類似SQL select * from db where quantity>finish)
1,Spring data MongoDB 目前不支持 $expr
2,可行的方式之一是利用project 和 $cond(MongoTemplet也不直接支持 $cond,要用到AggregationSpELExpression)
參考:
上代碼:

Aggregation aggregation = newAggregation(project("quantity", "finish")
.and(AggregationSpELExpression.expressionOf("cond(quantity>finish, 1, 0)")).as("flag1")
,match(Criteria.where("flag1").is(1)));
list = MongoTemplate.aggregate(aggregation, "xxxx", xxx.class).getMappedResults();		

先添加一個字段(flag1)存比較結果,再按比較結果過濾。

ps:話說mongodb資料好少啊,這麼小問題整了一個下午

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章