mongotemplate 動態query拼接

2021-09-24 23:56:19 字數 1463 閱讀 2373

**在用mongodb的時候進行分詞搜尋**
使用ik分詞器對查詢條件進行分詞比如: 男士9分休閒褲。

使用mongotemplate 對分詞結果進行查詢。

query query =

newquery()

;criteria criteria =

newcriteria()

;/** keywords是需要查詢的內容,對keywords進行分詞*/

list

<

string

>

keywordlist =

stringutil

.splitword

(keywords)

;list

<

criteria

>

criterialist =

newarraylist

<

>()

;/** name是查詢的資料庫欄位名*/

criterialist.

add(

criteria

.where

("name").

regex

(keywords));

if(collectionutils

.isnotempty

(keywordlist))}

criteria.

oroperator

(criterialist.

stream()

.toarray

(criteria

::new))

; query.

addcriteria

(criteria)

;

ik分詞器工具類

public

class

stringutil

public

static

boolean

haselement

(string

arr,

string targetvalue)

}return

false;}

public

static

list

<

string

>

splitword

(string word)

throws

ioexception

reader.

close()

;return wordlist;

}public

static

final

string

randomguid

(string shopalias,

string orderalias)

}

mongoTemplate聚合操作

準備資料 customer 集合,資料型別如下 只是部分資料,資料量太多就不一一列出 1 通過mongotemplate.group方法 public groupbyresultsgroup string inputcollectionname,groupby groupby,classentity...

MongoTemplate聚合查詢

表結構 import org.springframework.data.domain.sort import org.springframework.data.domain.sort.direction import org.springframework.data.domain.sort.orde...

MongoTemplate 教程系列(三)

mongodb中聚合 aggregate 主要用於處理資料 諸如統計平均值,求和等 並返回計算後的資料結果。有點類似sql語句中的 count mongodb語法 db.collection name.aggregate aggregate operation 複製 aggregationresul...