morphia操作mongodb
1. 加入依赖
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.9.1</version> </dependency><dependency> <groupId>org.mongodb.morphia</groupId> <artifactId>morphia</artifactId> <version><span style="color: rgba(128, 0, 128, 1)">1.3</span>.<span style="color: rgba(128, 0, 128, 1)">2</span></version> </dependency></pre>
2. 配置 MongoClient
class MongoBase {</span><span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient client; </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">* * MongoClient会自动创建连接池,因此,大部分情况下,整个JVM应用中只需要有一个MongoClient实例就可以。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { MongoClientURI clientURI </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MongoClientURI( </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mongodb://user:password@192.168.1.22:27017/admin</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); client </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient(clientURI); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Datastore getDatastore() { morphia </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Morphia(); Datastore datastore </span>= morphia.createDatastore(client, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">dbName</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> datastore; } </span><span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Morphia morphia;
}
3. 集合 MongDbOperator
import com.google.common.collect.ImmutableMap;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.ppmoney.g2.DateHelper;
import com.ppmoney.g2.common.PagedResult;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.Sort;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/
-
Created by zhangjy on 2018/11/28.
*/
public class MongDbOperator<T> {
private Class<T> tClass;
public MongDbOperator(Class<T> classz) {
this.tClass = classz;
}
public T getOne(ObjectId id) {
return getQueryFilter(id).get();
}
public T getOne(String id) {
return getQueryFilter(new ObjectId(id)).get();
}
public T getOne(Map<String, Object> filter) {
Assert.notNull(filter, "过滤条件不能为空");
Query<T> clientDevicesFilter = getQueryFilter(filter);
return clientDevicesFilter.get();
}
public T getOne(Map<String, Object> filter, Sort sort) {
Assert.notNull(filter, "过滤条件不能为空");
Query<T> queryFilter = getQueryFilter(filter);
queryFilter = queryFilter.order(sort);
return queryFilter.get();
}
/
-
按访问时间倒序排列
-
@param filter 过滤条件
-
@param sort 排序条件
-
@param limit 前几个 (0: 不限制)
*/
public List<T> list(Map<String, Object> filter, Sort sort, int limit) {
Query<T> queryFilter = getQueryFilter(filter);
if (sort != null) {
queryFilter = queryFilter.order(sort);
}
if (limit > 0) {
queryFilter = queryFilter.limit(limit);
}
return queryFilter.<T>asList();
}
/
-
按访问时间倒序排列
-
@param filter 过滤条件
-
@param sort 排序条件
-
@param limit 前几个 (0: 不限制)
*/
public long count(Map<String, Object> filter) {
Query<T> queryFilter = getQueryFilter(filter);
return count(queryFilter);
}
/
-
分页查询
-
@param filter 过滤条件
-
@param sort 排序条件
-
@param limit 前几个 (0: 不限制)
*/
public PagedResult<T> pagedList(Map<String, Object> filter, Sort sort, int pageIndex, int pageSize) {
Query<T> queryFilter = getQueryFilter(filter);
long count = count(queryFilter);
List<T> subItems = queryFilter.offset((pageIndex - 1) * pageSize).limit(pageSize).asList();
PagedResult<T> result = new PagedResult<T>(subItems, pageIndex, pageSize, (int)count);
return result;
}
/
- 按访问时间倒序排列
- @param filter 过滤条件
*/
public List<T> list(Map<String, Object> filter) {
return list(filter, null, 0);
}
public ObjectId insert(T obj) {
Datastore datastore = MongoBase.getDatastore();
Key<T> save = datastore.save(obj);
return (ObjectId) (save.getId());
}
public int updateOne(String id, Map<String, Object> filedValues) {
return updateOne(new ObjectId(id), filedValues);
}
public int updateOne(ObjectId id, Map<String, Object> filedValues) {
Query<T> queryFilter = getQueryFilter(id);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> updateImpl(queryFilter, filedValues);
}
public int update(Map<String, Object> filter, Map<String, Object> filedValues) {
Query<T> queryFilter = getQueryFilter(filter);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> updateImpl(queryFilter, filedValues);
}
public boolean exists(Map<String, Object> filter) {
return getOne(filter) != null;
}
/
-
删除
-
@param filter 用户 Id
*/
public int delete(String id) {
Query<T> queryFilter = getQueryFilter(new ObjectId(id));
return delete(queryFilter);
}
/
-
删除
-
@param filter 用户 Id
*/
public int delete(ObjectId id) {
Query<T> queryFilter = getQueryFilter(id);
return delete(queryFilter);
}
/
-
删除
-
@param filter 用户 Id
*/
public int delete(Map<String, Object> filter) {
Query<T> queryFilter = getQueryFilter(filter);
return delete(queryFilter);
}
/
-
软删除
-
@param filter 用户 Id
*/
public int softDelete(Map<String, Object> filter) {
Query<T> clientDevicesFilter = getQueryFilter(filter);
UpdateOperations<T> updateOperations = getDeleteOperations();
return MongoBase.getDatastore().updateFirst(clientDevicesFilter, updateOperations, false).getUpdatedCount();
}
private long count(Query<T> queryFilter) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> queryFilter.count();
}
/
- 删除
- @param filter 用户 Id
*/
private int delete(Query<T> queryFilter) {
return MongoBase.getDatastore().delete(queryFilter).getN();
}
private int updateImpl(Query<T> queryFilter, Map<String, Object> filedValues) {
Datastore datastore = MongoBase.getDatastore();
UpdateOperations</span><T> updateOperations =<span style="color: rgba(0, 0, 0, 1)"> datastore.createUpdateOperations(tClass);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map.Entry<String, Object><span style="color: rgba(0, 0, 0, 1)"> entry : filedValues.entrySet()) {
updateOperations.</span><span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">(entry.getKey(), entry.getValue());
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> datastore.update(queryFilter, updateOperations, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">).getUpdatedCount();
}
private Query<T> getQueryFilter(ObjectId id) {
return getQueryFilter(ImmutableMap.of("_id", id));
}
private Query<T> getQueryFilter(Map<String, Object> filter) {
Query<T> query = MongoBase.getDatastore().createQuery(this.tClass);
if (CollectionUtils.isEmpty(filter)) {
return query;
}
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map.Entry<String, Object><span style="color: rgba(0, 0, 0, 1)"> entry : filter.entrySet()) {
query </span>=<span style="color: rgba(0, 0, 0, 1)"> query.filter(entry.getKey(), entry.getValue());
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> query;
}
private UpdateOperations<T> getUpdateOperations(Map<String, Object> filedValues) {
UpdateOperations<T> updateOperations = MongoBase.getDatastore().createUpdateOperations(this.tClass);
updateOperations.inc("version");
updateOperations.set("lastModifyTime", System.currentTimeMillis());
for (Map.Entry<String, Object> entry : filedValues.entrySet()) {
updateOperations.set(entry.getKey(), entry.getValue());
}
return updateOperations;
}
private UpdateOperations<T> getDeleteOperations() {
return getUpdateOperations(ImmutableMap.of("deleted", true));
}
}
4. 增加 model
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import lombok.Data;
/
-
Created by zhangjy on 2018/11/30.
/
@Data
@Entity(noClassnameStored = true)
public abstract class BaseMongDbModel {
/*
- mongDb 的 id
*/
@Id
private ObjectId id;
/
- 是否已 (软) 删除
*/
private boolean deleted;
/
- 创建时间
*/
private long createTime;
/
- 最后修改时间
*/
private long lastModifyTime;
/
- 版本号
*/
private int version;
}
@Data @Builder @AllArgsConstructor @NoArgsConstructor public class LoginRecord extends BaseMongDbModel { private Integer userId; private String phone; private String loginIp; private Long loginTime; private String platform; private String deviceId;@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span>.toString()+"LoginRecord{" + "customerId=" + userId + ", phone='" + phone + '\'' + ", loginIp='" + loginIp + '\'' + ", loginTime=" + loginTime + ", platform='" + platform + '\'' + ", deviceId='" + deviceId + '\'' + '}'<span style="color: rgba(0, 0, 0, 1)">; }
}
5. 其他辅助类
public class SimplePagedList { private static final int DEFALUT_PAGE_SIZE = 10; private int pageSize; private int totalCount; private int firstPageIndex;</span><span style="color: rgba(0, 0, 255, 1)">public</span> SimplePagedList(<span style="color: rgba(0, 0, 255, 1)">int</span> totalCount, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> pageSize) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>(totalCount, pageSize, 1<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> SimplePagedList(<span style="color: rgba(0, 0, 255, 1)">int</span> totalCount, <span style="color: rgba(0, 0, 255, 1)">int</span> pageSize, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> firstPageIndex) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.pageSize = pageSize <= 0?10<span style="color: rgba(0, 0, 0, 1)">:pageSize; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.totalCount =<span style="color: rgba(0, 0, 0, 1)"> totalCount; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.firstPageIndex =<span style="color: rgba(0, 0, 0, 1)"> firstPageIndex; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getPageCount() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.totalCount % <span style="color: rgba(0, 0, 255, 1)">this</span>.pageSize == 0?<span style="color: rgba(0, 0, 255, 1)">this</span>.totalCount / <span style="color: rgba(0, 0, 255, 1)">this</span>.pageSize:<span style="color: rgba(0, 0, 255, 1)">this</span>.totalCount / <span style="color: rgba(0, 0, 255, 1)">this</span>.pageSize + 1<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> getStartIndex(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> pageIndex) { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(pageIndex < <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.firstPageIndex) { </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> IllegalArgumentException("pageIndex不合法"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span> (pageIndex - 1) * <span style="color: rgba(0, 0, 255, 1)">this</span>.pageSize + 1<span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> getEndIndex(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> pageIndex) { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(pageIndex < <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.firstPageIndex) { </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> IllegalArgumentException("pageIndex不合法"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span> pageIndex * <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.pageSize; } }
}
import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors;import lombok.Data;
@Data
public class PagedResult<T> {
private static final int DEFALUT_PAGE_SIZE = 10;
private boolean hasNextPage;
private boolean hasPreviousPage;
private List<T> items;
private int pageIndex;
private int pageSize;
private int totalCount;
private int totalPages;</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> PagedResult() { } </span><span style="color: rgba(0, 0, 255, 1)">public</span> PagedResult(List<T> subItems, <span style="color: rgba(0, 0, 255, 1)">int</span> pageIndex, <span style="color: rgba(0, 0, 255, 1)">int</span> pageSize, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> totalCount) { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(pageSize <= 0<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> IllegalArgumentException("pageSize非法"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.init(subItems, pageIndex, pageSize, totalCount); } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> PagedResult(List<T> source, <span style="color: rgba(0, 0, 255, 1)">int</span> pageIndex, <span style="color: rgba(0, 0, 255, 1)">int</span> pageSize, Predicate<T><span style="color: rgba(0, 0, 0, 1)"> function) { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(pageSize <= 0<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> IllegalArgumentException("pageSize非法"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { List</span><T> filtedItems =<span style="color: rgba(0, 0, 0, 1)"> source.stream().filter(function).collect(Collectors.toList()); </span><span style="color: rgba(0, 0, 255, 1)">int</span> count =<span style="color: rgba(0, 0, 0, 1)"> filtedItems.size(); SimplePagedList simplePagedList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SimplePagedList(count, pageSize); List</span><T> subItems = filtedItems.stream().skip((<span style="color: rgba(0, 0, 255, 1)">long</span>)simplePagedList.getStartIndex(pageIndex) - 1L).limit((<span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)">)pageSize).collect(Collectors.toList()); </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.init(subItems, pageIndex, pageSize, count); } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> PagedResult(List<T> source, <span style="color: rgba(0, 0, 255, 1)">int</span> pageIndex, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> pageSize) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>(source, pageIndex, pageSize, (m) -><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; }); } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> init(List<T> subItems, <span style="color: rgba(0, 0, 255, 1)">int</span> pageIndex, <span style="color: rgba(0, 0, 255, 1)">int</span> pageSize, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> totalCount) { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(pageIndex == 0<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.hasNextPage = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.hasNextPage = pageIndex * pageSize <<span style="color: rgba(0, 0, 0, 1)"> totalCount; } </span><span style="color: rgba(0, 0, 255, 1)">this</span>.hasPreviousPage = pageIndex > 1 && totalCount > 0<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.pageIndex =<span style="color: rgba(0, 0, 0, 1)"> pageIndex; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.pageSize =<span style="color: rgba(0, 0, 0, 1)"> pageSize; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.totalCount =<span style="color: rgba(0, 0, 0, 1)"> totalCount; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.totalPages = totalCount % pageSize == 0?totalCount / pageSize:totalCount / pageSize + 1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.items =<span style="color: rgba(0, 0, 0, 1)"> subItems; } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "PagedResult(hasNextPage=" + <span style="color: rgba(0, 0, 255, 1)">this</span>.isHasNextPage() + ", hasPreviousPage=" + <span style="color: rgba(0, 0, 255, 1)">this</span>.isHasPreviousPage() + ", items=" + <span style="color: rgba(0, 0, 255, 1)">this</span>.getItems() + ", pageIndex=" + <span style="color: rgba(0, 0, 255, 1)">this</span>.getPageIndex() + ", pageSize=" + <span style="color: rgba(0, 0, 255, 1)">this</span>.getPageSize() + ", totalCount=" + <span style="color: rgba(0, 0, 255, 1)">this</span>.getTotalCount() + ", totalPages=" + <span style="color: rgba(0, 0, 255, 1)">this</span>.getTotalPages() + ")"<span style="color: rgba(0, 0, 0, 1)">; }
}
7. 测试代码
public static void main(String[] args) { MongDbOperator<LoginRecord> operator = new MongDbOperator<>(LoginRecord.class); LoginRecord model; List<LoginRecord> models; int cutomerId = 50000133; // 1. 普通查询 (根据 id 查询) model = operator.getOne("5e1444b0b58fe60001fcd4eb"); System.out.println(String.format("根据 id 获取 mode, 结果为:%s", model.toString()));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2.普通查询(根据业务主键查询)</span> model = operator.getOne(ImmutableMap.of("userId"<span style="color: rgba(0, 0, 0, 1)">, cutomerId)); System.out.println(String.format(</span>"根据业务主键customerId获取mode,结果为:%s"<span style="color: rgba(0, 0, 0, 1)">, model)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3.普通查询(根据业务查询)</span> models = operator.list(ImmutableMap.of("platform", "app"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(String.format(</span>"根据业务platform获取mode集合,结果为:%s"<span style="color: rgba(0, 0, 0, 1)">, models)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 4.模糊查询(根据正则匹配)</span> models = operator.list(ImmutableMap.of("phone", Pattern.compile("^158*"<span style="color: rgba(0, 0, 0, 1)">))); System.out.println(String.format(</span>"获取186开头的手机号的登陆记录集合,结果为:%s"<span style="color: rgba(0, 0, 0, 1)">, models)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 5.区间查询</span> models = operator.list(ImmutableMap.<String, Object><span style="color: rgba(0, 0, 0, 1)">builder() .put(</span>"loginTime >=", DateHelper.toTimeStamp(LocalDateTime.of(2019, 1, 1, 0, 0, 0<span style="color: rgba(0, 0, 0, 1)">))) .put(</span>"loginTime <", DateHelper.toTimeStamp(LocalDateTime.of(2021, 1, 1, 0, 0, 0<span style="color: rgba(0, 0, 0, 1)">))) .build()); System.out.println(String.format(</span>"获取2019年所有用户登陆记录集合,结果为:%s"<span style="color: rgba(0, 0, 0, 1)">, models)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">6.分页查询</span> PagedResult<LoginRecord> models2 = operator.pagedList(ImmutableMap.of(), Sort.ascending("_id"), 1, 10<span style="color: rgba(0, 0, 0, 1)">); System.out.println(String.format(</span>"获取2019年所有用户登陆记录集合,结果为:%s"<span style="color: rgba(0, 0, 0, 1)">, models2)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">7.新增记录</span> model = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> LoginRecord(); model.setUserId(cutomerId); model.setDeviceId(</span>"Q100000"<span style="color: rgba(0, 0, 0, 1)">); model.setLoginIp(</span>"186.26.56.25"<span style="color: rgba(0, 0, 0, 1)">); model.setPhone(</span>"18626562155"<span style="color: rgba(0, 0, 0, 1)">); model.setPlatform(</span>"app"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">long</span> second = LocalDateTime.now().toInstant(ZoneOffset.of("+8"<span style="color: rgba(0, 0, 0, 1)">)).getEpochSecond(); model.setLoginTime(second); ObjectId id </span>=<span style="color: rgba(0, 0, 0, 1)"> operator.insert(model); System.out.println(String.format(</span>"新增记录,新增id为:%s"<span style="color: rgba(0, 0, 0, 1)">, id)); }</span></pre>