Quantcast
Viewing all articles
Browse latest Browse all 128

mongodb search with pattern

Code:

@Override
    public List<Book> getBooksByTerm(String term) {
        Assert.notNull(term);
        BasicQuery query = new BasicQuery("{$regex: '" + term + "', $option:'i'}");
        return mongoTemplate.find(query, Book.class);
 
    }

method threw java.lang.ClassCastException: java.util.regex.Pattern cannot be cast to com.mongodb.DBObject
how to find Book by any field == "*term*", executing this query in console is work
Code:

> db.book.find("{$regex:'нем', $option:'i'}").pretty()
{
        "_id" : ObjectId("51b61eee6b14af5c5e731924"),
        "_class" : "com.library.model.Book",
        "title" : "Немецкий язык: Вводный курс",
        "authors" : "Креветко",
        "publish" : "Москва",
        "category" : "Учеба",
        "volumeOfPages" : 123,
        "dateOfReceipt" : ISODate("2013-06-08T19:19:03.904Z"),
        "countInStore" : 1,
        "countOnHands" : 0
}


Viewing all articles
Browse latest Browse all 128

Trending Articles