次のフィールドを持つドキュメントを含む MongoDB コレクションがあります。
- 日付 (日付オブジェクト)
- offerType (文字列)
MongoRepository を使用して、日付の範囲内の日付を持ち、 offerType に list で提供される文字列の 1 つが含まれるすべてのドキュメントを検索するメソッドを作成したいと考えています。
例
ドキュメント:
- 日付: 2019 年 10 月 4 日、オファーの種類: オファー 1
- 日付: 2019 年 11 月 4 日、オファーの種類: オファー 3
- 日付: 2019 年 4 月 15 日、オファーの種類: オファー 2
- 日付: 2019 年 4 月 15 日、オファータイプ: オファー 1
私が欲しい:
- 2019 年 9 月 4 日から 2019 年 4 月 12 日までの日付
- 次のオファー: offer1、offer3
前の例では、ドキュメント 1 と 2 を取得します。
私のコード
MongoRepository と、必要なフィールドを含むカスタム オブジェクトを使用します。
import java.util.Date;
import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface ReportVocalOrderRepository extends MongoRepository<ReportVocalOrder, String> {
List<ReportVocalOrder> findByDateBetween(Date startDate, Date endDate, Pageable pageable);
List<ReportVocalOrder> findByDateBetweenAndOfferTypeContaining(Date startDate, Date endDate, List<String> offers, Pageable pageable);
}
ドキュメントクラスは次のとおりです。
@JsonInclude(Include.NON_NULL)
@Document(collection = Constants.Mongo.Collections.VOCAL_ORDER_REPORT)
@ApiModel
public class ReportVocalOrder {
@Id
private String id;
private Date date;
private String offerType;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getOfferType() {
return offerType;
}
public void setOfferType(String offerType) {
this.offerType = offerType;
}
}
MongoRepository の最初のメソッドは正常に機能します。2 番目のものは代わりに空のリストを返します。
問題は、mongoRepository にクエリを実行して、引数として渡されたリストの値の 1 つを含むことができるフィールドを検索することです。
この実装の何が問題になっていますか? このクエリを実装するより良い方法はありますか?