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 鐢ㄦ硶的主要内容,如果未能解决你的问题,请参考以下文章

axios鎷︽埅鍣ㄧ殑鐢ㄦ硶

vi | vim 鐢ㄦ硶

IO鐢ㄦ硶鍑犱緥

鎶撳寘宸ュ叿 tcpdump 鐢ㄦ硶璇存槑

鐩掑瓙妯″瀷 box-sizing 鐢ㄦ硶

杩唬鍣ㄥ拰鐢熸垚鍣ㄧ殑鐢ㄦ硶