是否有可能使用 Spring Data MongoDb 來定義自定義標準? (Is possibility using Spring Data MongoDb to define custom Criteria?)


問題描述

是否有可能使用 Spring Data MongoDb 來定義自定義標準? (Is possibility using Spring Data MongoDb to define custom Criteria?)

我有一個方法的簡單接口:

 Criteria toCriteria(String key, String value)

接下來我想要下一個實現

public class EqExpression implements Expression
{

     @Override
     public Criteria toCriteria(String key, String value)
     {
        return Criteria.where(key).eq(Pattern.compile(value));
     }
}

}

但是沒有$eq 運算符。所以我的問題是:

  • 為什麼 org.springframework.data.mongodb.core.query.Criteria 沒有這樣的操作符?
  • 有沒有辦法實現自定義 Criteria 實現或有任何解決方法?

  • 對我來說,最好有類似

     @Override
     public Criteria toCriteria(String key, String value)
     {
        //return new BasicDBObject(key, new BasicDBObject("$eq", value)) converted to Criteria
     }
    
  • 的代碼

總的來說,我的目的是實現rest查詢語言,對於像gtlt這樣的每個操作,我都有Expression<的具體實現


參考解法

方法 1:

using this answer, I was able to create simple $eq Criteria

private Criteria getEqCriteria(String value)
{
    // hack Criteria, because new Criteria().is(value) not working!
    Criteria c = new Criteria();
    try
    {
        Field _criteria = c.getClass().getDeclaredField("criteria");
        _criteria.setAccessible(true);
        @SuppressWarnings("unchecked")
        LinkedHashMap<String, Object> criteria = (LinkedHashMap<String, Object>) _criteria.get(c);
        criteria.put("$eq", value);
        Field _criteriaChain = c.getClass().getDeclaredField("criteriaChain");
        _criteriaChain.setAccessible(true);

        @SuppressWarnings("unchecked")
        List<Criteria> criteriaChain = (List<Criteria>) _criteriaChain.get(c);
        criteriaChain.add(c);
    } catch (Exception e)
    {
        // Ignore
    }
    return c;
}

(by Roman GrytsyshynDaulet)

參考文件

  1. Is possibility using Spring Data MongoDb to define custom Criteria? (CC BY‑SA 2.5/3.0/4.0)

#spring-data-mongodb #java






相關問題

如何在 mongodb 中保存 java.sql.date 對象? (How to save java.sql.date object in mongodb?)

Spring Data MongoDB Core 1.9.1.RELEASE 給出 java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults (Spring Data MongoDB Core 1.9.1.RELEASE gives java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults)

我可以結合 AND 和 OR 條件嗎 (Can I combine AND and OR conditions)

查詢數據時應該使用 MongoTemplate 還是 DBCollection (Should i use MongoTemplate or DBCollection when query data)

是否有可能使用 Spring Data MongoDb 來定義自定義標準? (Is possibility using Spring Data MongoDb to define custom Criteria?)

Spring-data 2.1 使用 kotlin 獲取“UnsupportedOperationException:沒有訪問器設置屬性” (Spring-data 2.1 get "UnsupportedOperationException: No accessor to set property" with kotlin)

調用頁面大小大於 36 的 Spring Data MongoDB 存儲庫方法時出現 StackOverflowError (StackOverflowError when calling Spring Data MongoDB repository method with a page size bigger than 36)

JSON- MongoDB 中帶有 Spring Data 的數組 (JSON- Array in MongoDB with Spring Data)

Spring Data Mongo DB:回复消息長度5502322小於最大消息長度 (Spring Data Mongo DB: The reply message length 5502322 is less than the maximum message length)

Spring Boot 反應式和 mongodb '命令插入需要身份驗證' (Spring boot reactive and mongodb 'command insert requires authentication')

使用 MongoDB Spring Data Aggregation 匹配日期時間字段的問題 (Problem with matching date-time fields using MongoDB Spring Data Aggregation)

在 Spring Boot 應用程序中實現工作進程 (Implementing worker processes in a Spring Boot application)







留言討論