MongDB 鐢ㄦ硶
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongDB 鐢ㄦ硶相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/where' title='where'>where
pom att ssi constant gdb rollback task bool杩戞湡鍦ㄥ仛涓氬姟鐨勬椂鍊欑敤鍒颁簡MongDB闈炲叧绯诲瀷鏁版嵁搴撲竴鑸敤鏉ュ瓨鍌ㄨ亰澶╄褰曟垨鑰呮秷鎭帹閫佹垨鑰呮棩蹇楄褰曠瓑绛夛紝璇濅笉澶氳鐩存帴涓婁唬鐮?/p>
棣栧厛鍏堝鍏?pom 鏂囦欢
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
yml 鏂囦欢鐨勯厤缃甦ata:
mongodb:
uri: mongodb://绔彛/搴?br> username: 123456+
password: 1234567
MongoDB鐨勬煡璇㈡柟寮忓緢澶氱锛屼笅闈㈠彧鍒椾簡涓€浜涘父鐢ㄧ殑锛屾瘮濡傦細
鏌ヨ-----------------------------------------------------------start
娉ㄦ剰锛欰rticle 杩欎釜鏄釜瀹炰綋绫诲璞′篃灏辨槸mongdo鏁版嵁搴撹鏌ヨ鐨勮〃瀵瑰簲鐨勫疄浣撶被瀵瑰簲鐨?br>???? 1銆?鏌ヨ
???? 2銆佹ā绯婃煡璇?br>???? 3銆佸ぇ浜庡皬浜庤寖鍥存煡璇?br>???? 4銆乮n鏌ヨ
???? 5銆乷r鏌ヨ
???? 6銆佹煡璇竴鏉★紝鏌ヨ鍏ㄩ儴
// 鍦ㄥ啓閫昏緫鐨勪笟鍔″眰鐩存帴娉ㄥ叆灏卞彲浠ヤ娇鐢ㄤ簡
@Autowired
private MongoTemplate mongoTemplate;
1.鍙煡璇㈢鍚堟潯浠剁殑绗竴鏉℃暟鎹紝杩斿洖TaskDTO瀵硅薄
Query query = new Query(Criteria.where("userId").is(userId)); // 鏉′欢涓€
query.addCriteria(Criteria.where("taskId").is(toDoDirectProcessingDTO.getTaskId())); // 鏉′欢浜?/p>
query.addCriteria(Criteria.where("sysId").is(toDoDirectProcessingDTO.getSysId())); // 鏉′欢涓?/em>
mongoTemplate.findOne(query, TaskDTO.class); // 鏌ヨ -- findOne(query,鏌ヨ瑕佹槧灏勭殑瀵硅薄绫?;
2.鏌ヨ澶氭潯
Query query = Query.query(Criteria.where("author").is("yinjihuan")); List<Article> articles = mongoTemplate.find(query, Article.class); //find(query, 瀹炰綋绫诲璞?span class="hljs-type">.class);
3. 鏌ヨ闆嗗悎涓墍鏈夋暟鎹紝涓嶅姞鏉′欢
articles = mongoTemplate.findAll(Article.class);
4.鏌ヨ绗﹀悎鏉′欢鐨勬暟閲?/span>
query = Query.query(Criteria.where("author").is("yinjihuan")); long count = mongoTemplate.count(query, Article.class);
5.鏍规嵁涓婚敭ID鏌ヨ
article = mongoTemplate.findById(new ObjectId("57c6e1601e4735b2c306cdb7"), Article.class);
6.in鏌ヨ
List<String> authors = Arrays.asList("yinjihuan", "jason");
query = Query.query(Criteria.where("author").in(authors));
articles = mongoTemplate.find(query, Article.class);
7.ne锛?=锛夋煡璇?/span>
query = Query.query(Criteria.where("author").ne("yinjihuan"));
articles = mongoTemplate.find(query, Article.class);
8. lt(<)鏌ヨ璁块棶閲忓皬浜?0鐨勬枃绔?/span>
query = Query.query(Criteria.where("visitCount").lt(10));
articles = mongoTemplate.find(query, Article.class);
9. 鑼冨洿鏌ヨ锛屽ぇ浜?灏忎簬10
query = Query.query(Criteria.where("visitCount").gt(5).lt(10));
articles = mongoTemplate.find(query, Article.class);
10.妯$硦鏌ヨ锛宎uthor涓寘鍚玜鐨勬暟鎹?/span>
query = Query.query(Criteria.where("author").regex("a"));
articles = mongoTemplate.find(query, Article.class);
11.鏁扮粍鏌ヨ锛屾煡璇ags閲屾暟閲忎负3鐨勬暟鎹?/span>
query = Query.query(Criteria.where("tags").size(3));
articles = mongoTemplate.find(query, Article.class);
12.or鏌ヨ锛屾煡璇uthor=jason鐨勬垨鑰卾isitCount=0鐨勬暟鎹?/span>
query = Query.query(Criteria.where("").orOperator( Criteria.where("author").is("jason"), Criteria.where("visitCount").is(0)));
articles = mongoTemplate.find(query, Article.class);
鏌ヨ-----------------------------------------------------------end
淇敼-----------------------------------------------------------start
鎵归噺淇敼锛?/span>
// List<ToDoDirectProcessingDTO> stringListTaskDTO 鍓嶇浼犺繃鏉ヨ淇敼鐨勬暟鎹?/span>
// msg 淇敼鎴愪粈涔?/span>
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RuntimeException.class,Exception.class })
public boolean batchUpdateMongodb (List<ToDoDirectProcessingDTO> stringListTaskDTO,String msg) {
try {
String userId = SingleSignInUtils.getUserId();
List<BatchUpdateOptionsDTO> list = new ArrayList<>();
for (ToDoDirectProcessingDTO dto : stringListTaskDTO) {
Query query = new Query(Criteria.where("userId").is(userId)); // 鏍规嵁浠€涔堟潯浠朵慨鏀?br> query.addCriteria(Criteria.where("taskId").is(dto.getTaskId())); // 鏍规嵁浠€涔堟潯浠朵慨鏀?br> query.addCriteria(Criteria.where("sysId").is(dto.getSysId())); // 鏍规嵁浠€涔堟潯浠朵慨鏀?br> Update update = new Update();
// 鍒犻櫎 delete
if (msg.equals(URLMethodLConstant.DELETE_FLAG)) {
update.set("status", msg); // update.set("瑕佷慨鏀圭殑瀛楁鍚?,"瑕佷慨鏀圭殑鍊?)
}
// 鏆傜紦 delay
if (msg.equals(URLMethodLConstant.DELAY_FLAG)) {
update.set("status", msg);
}
// 涓嶅悓鎰?0
if (msg.equals(TaskConstant.TASK_SYSTEM_CODE_SUCCESS)) {
update.set("detailStatus", msg);
}
// 鍚屾剰 1
if (msg.equals(TaskConstant.TASK_SYSTEM_CODE_SUCCESS_ONE)) {
update.set("detailStatus", msg);
}
list.add(new BatchUpdateOptionsDTO(query,update,false,true));
}
// int batchUpdateCount = MongoBaseDaoUtils.batchUpdate(mongoTemplate, "瑕佹搷浣滅殑鐨勮〃",鎿嶄綔鐨勯泦鍚? true);
int batchUpdateCount = MongoBaseDaoUtils.batchUpdate(mongoTemplate, URLMethodLConstant.TASK_TABLE, list, true);
logger.info("鍑嗗鏇存柊: {} 鏉℃暟鎹?瀹為檯鏇存柊浜? {}鏉℃暟鎹?鎿嶄綔缁撴灉 {} ",stringListTaskDTO.size(),batchUpdateCount,(batchUpdateCount == stringListTaskDTO.size()));
if (batchUpdateCount == stringListTaskDTO.size()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
return false;
}
宸ュ叿绫伙細 MongoBaseDaoUtils
/**
* 鎵归噺鏇存柊
* @param mongoTemplate
* @param collectionName 瑕佹搷浣滅殑琛ㄥ悕
* @param options false 鏇存柊杩囩▼涓嶅仛鎻掑叆
* @param ordered 濡傛灉涓簍rue,涓€鏉¤鍙ユ洿鏂板け璐ワ紝鍓╀笅鐨勮鍙ュ皢涓嶅啀鎵с€傚鏋滀负false,涓€鏉¤鍙ユ洿鏂板け璐ワ紝鍓╀笅鐨勫皢缁х画鎵ц銆傞粯璁や负true銆?br> * @return
*/
public static int batchUpdate(MongoTemplate mongoTemplate, String collectionName, List<BatchUpdateOptionsDTO> options, boolean ordered) {
return doBatchUpdate(mongoTemplate, collectionName, options, ordered);
}
private static int doBatchUpdate(MongoTemplate mongoTemplate, String collName, List<BatchUpdateOptionsDTO> options, boolean ordered) {
DBObject command = new BasicDBObject();
command.put("update", collName);
List<BasicDBObject> updateList = new ArrayList<>();
for (BatchUpdateOptionsDTO option : options) {
BasicDBObject update = new BasicDBObject();
update.put("q", option.getQuery().getQueryObject());
update.put("u", option.getUpdate().getUpdateObject());
update.put("upsert", option.isUpsert());
update.put("multi", option.isMulti());
updateList.add(update);
}
command.put("updates", updateList);
command.put("ordered", ordered);
Document document = mongoTemplate.getDb().runCommand((Bson) command);
logger.info("doc:" + document);
logger.info("doc--n:" + document.get("n"));
logger.info("doc--nModified:" + document.get("nModified"));
// n涓虹鍚圦uery鏌ヨ鐨勮褰曟€绘暟 鍥犱负鏄牴鎹甶d杩涜鐨勬煡璇? 鍘熷垯涓婂彧瑕佺鍚堟煡璇㈢殑璁板綍鏁扮瓑浜庤鏇存柊鐨勬暟閲忓氨浠h〃鎴愬姛
Object n = document.get("n");
logger.info("doc--n--class:" + n.getClass());
if (n.getClass() == Integer.class) {
return (Integer) n;
}
return Integer.parseInt(String.valueOf(n));
}
// 宸ュ叿绫?/p>
public class BatchUpdateOptionsDTO {
private Query query;
private Update update;
/** false 鏇存柊杩囩▼涓嶅仛鎻掑叆 */
private boolean upsert = false;
/** true,涓€鏉¤鍙ユ洿鏂板け璐ワ紝鍓╀笅鐨勮鍙ュ皢涓嶅啀鎵с€傚鏋滀负false,涓€鏉¤鍙ユ洿鏂板け璐ワ紝鍓╀笅鐨勫皢缁х画鎵ц */
private boolean multi = false;
public BatchUpdateOptionsDTO(Query query, Update updateupdate, boolean b, boolean b1) {
this. query = query;
this. update = updateupdate;
this. upsert = b;
this. multi = b1;
}
public Query getQuery() {
return query;
}
public void setQuery(Query query) {
this.query = query;
}
public Update getUpdate() {
return update;
}
public void setUpdate(Update update) {
this.update = update;
}
public boolean isUpsert() {
return upsert;
}
public void setUpsert(boolean upsert) {
this.upsert = upsert;
}
public boolean isMulti() {
return multi;
}
public void setMulti(boolean multi) {
this.multi = multi;
}
}
以上是关于MongDB 鐢ㄦ硶的主要内容,如果未能解决你的问题,请参考以下文章