测试综合技能的期末预测
測試綜合技能
選擇,填空,判斷
簡答
NoSQL數據庫和關系型數據庫的優劣勢?
傳統的關系數據庫:非常完備的關系理論基礎、具有事務性機制的支持、高效的查詢優化機制,有非常嚴格的標準,缺陷:無法滿足海量數據的管理需求,數據模型不夠靈活,無法滿足高并發需求,無法滿足高擴展性和高可用性的需求
NoSQL數據庫的優勢:支持超大規模的數據存儲,數據模型比較靈活,缺點:缺乏底層基礎理論做支撐,很多NoSQL數據庫都不支持事務的強一致性
MongoDB有哪些特點?
1)提供了一個面向文檔存儲,操作起來比較簡單和容易2)可以設置任何屬性的索引來實現更快的排序3)具有較好的水平可擴展性4)支持豐富的查詢表達式,可以輕易查詢文檔中內嵌的對象及數組5)可以實現替換已完成文檔的一些指定的數據字段6)MongoDB中的Map\Reduce主要是用來對數據進行批量處理和聚合操作7)支持各種編程語言8)安裝簡單
內嵌文檔和引用文檔各有什么優勢?
內嵌文檔好處:非扁平化的數據模式,使得信息的表示方式更加自然,還有可以保證對子文檔進行原子性更新,從而實現關系型數據庫中數據的特征,除此之外,嵌套文檔模式,數據讀取速度變快,只需要一次查詢可以獲得相應的數據。缺點:一方面子文檔數據量大,整個文檔加載更新的速度變慢,從而影響系統的效率,,如果這種情況下,子文檔更細很頻繁,會嚴重影響系統的性能,另一方面,子文檔會造成大量的數據重復,如果子文檔比較小,或者更新不是很頻繁,建議選擇內嵌文檔模式。
子文檔比較大,文檔大小經常改變時,應該考慮引用文檔模式
復制集的優勢作用:
復制集提供了冗余,并提高了數據的可用性;復制集增強了服務器的可讀能力;復制集可以實現數據的備份,允許災難恢復;復制集可以監控服務器的狀態。
Redis主從復制的作用,特點?
特點:1)master可以擁有多個slave;2)多個slave可以連接同一個master外,還可以互相連接;3)主從復制不會阻塞master,在同步數據時,master可以繼續處理client請求;4)提高系統的伸縮性
作用:1)故障恢復:當主節點宕機,其他節點依然可以提供服務;2)負載均衡:Master節點提供寫服務,slave節點提供讀服務,分擔壓力;3)高可用基石:是哨兵和cluster實施的基礎,是高可用的基石
Redis是怎么來同步數據的?
第一次從主庫全量復制;主從庫網絡斷開重連同步;主從正常運行期間的同步
哨兵(Sentinel)機制?
哨兵主要是為了解決在主從復制架構中出現宕機的情況,用于管理多個Redis服務器,執行三個任務:監控(不斷的檢查主服務器和從服務器是否正常運行)、提醒(當被監控的某個Redis服務器出現故障的時候,Sentinel可以通過API向管理員或其他應用程序發送通知)、自動故障轉移(當一個主服務器出現故障時,Sentinel會開始一次自動故障遷移操作,它會將失效主服務器的其中一個從服務器升級為新的主服務器,并讓失效主服務器的其他從服務器改為復制新的主服務器;當客戶端試圖連接失效的主服務器時,集群也會向客戶端返回新主服務器的地址,使得集群可以使用新主服務器代替失效服務器)。實現目標:1)實現Redis故障轉移的自動化2)自動發現,自動轉移3)不需要人工參與
大題 shell命令
查詢:
1、查詢comments字段不存在的文檔
db.bai.find({comments:{KaTeX parse error: Expected 'EOF', got '}' at position 13: exists:false}?}); 2、查詢favorit…all:[“頭號玩家”,“肖生克的救贖”]}});
3、查詢name存在”bu”字符,并且age小于20或者大于24(要求使用regex操作符)
db.bai.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: and:[{name:{regex:“bu”}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: or:[{age:{lt:20}},{age:{KaTeX parse error: Expected 'EOF', got '}' at position 6: gt:24}?}]}]}); 4、查詢use…elemMatch:{“userId”:“101”,“content”:“評論1”}}});
5、查詢name=”xiaobu”的文檔,并且只顯示comments前三條數據
db.bai.find({“name”:“xiaobu”},{“comments”:{KaTeX parse error: Expected 'EOF', got '}' at position 12: slice:[0,3]}?}); 6、查詢所有name=…slice":[1,1]},“KaTeX parse error: Expected 'EOF', got '}' at position 6: id":1}?); 7、查詢被userId=…in:[“101”,“001”]}});
或者db.users.find({KaTeX parse error: Expected 'EOF', got '}' at position 57: …userId":"001"}]}?) 更新操作: 1、為name…set:{“country”:“China”}});
2、為所有文檔的favorites字段中 增加一個字段fruit 值為一個數組[apple,banana]
db.bai.update({},{KaTeX parse error: Expected 'EOF', got '}' at position 43: …ple","banana"]}}?,false,true); 3…or:[{age:{KaTeX parse error: Expected 'EOF', got '}' at position 6: lt:18}?},{age:{gt:24}}]},{KaTeX parse error: Expected 'EOF', got '}' at position 27: …ites.movies":1}}?,{multi:true});…push:{“favorites.fruit”:{each:["grapes","watermelon"],each:["grapes","watermelon"],each:["grapes","watermelon"],slice:-3}}});
5、向存在favorites.books數組字段的文檔中,在books數組的第二個元素位置添加元素“MongoDB”,“NoSQL”,并且數組元素按照升序排序
db.bai.update({“favorites.books”:{KaTeX parse error: Expected 'EOF', got '}' at position 12: exists:true}?},{push:{“favorites.books”:{each:["MongoDB","NoSQL"],each:["MongoDB","NoSQL"],each:["MongoDB","NoSQL"],position:1,KaTeX parse error: Expected 'EOF', got '}' at position 7: sort:1}?}}); 聚合操作 1、根據姓…group:{_id:“KaTeX parse error: Expected '}', got 'EOF' at end of input: name",total:{sum:1}}}])
2、根據姓名分組,并統計人數,過濾人數大于1的學生
db.class1.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"name”,num:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}?}},{match:{num:{KaTeX parse error: Expected 'EOF', got '}' at position 5: gt:1}?}}]) 3、統計每名學生在考…group:{_id:“KaTeX parse error: Expected '}', got 'EOF' at end of input: sno",score:{sum:{sum:["sum:["sum:["score.chinese”,“score.math","score.math","score.math","score.english”]}}}}])
或者
db.class1.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …{sno:1,scores:{add:[“score.chinese","score.chinese","score.chinese","score.math”,“KaTeX parse error: Expected 'EOF', got '}' at position 16: score.english"]}?}},{group:{_id:“KaTeX parse error: Expected '}', got 'EOF' at end of input: sno",score:{sum:“KaTeX parse error: Expected 'EOF', got '}' at position 8: scores"}?}}]) 4、統計每名男生在考…match:{sex:0}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roject:{grade:{sum:[“score.chinese","score.chinese","score.chinese","score.math”,“KaTeX parse error: Expected 'EOF', got '}' at position 16: score.english"]}?}}]) 或者 db.clas…match:{sex:0}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: …,sex:1,scores:{sum:[“score.chinese","score.chinese","score.chinese","score.math”,“KaTeX parse error: Expected 'EOF', got '}' at position 16: score.english"]}?}}]) 5、統計每名男生在考…match:{sex:0}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roject:{grade:{sum:[“score.chinese","score.chinese","score.chinese","score.math”,“KaTeX parse error: Expected 'EOF', got '}' at position 16: score.english"]}?}},{sort:{grade:-1}}])
或者
db.class1.aggregate([{KaTeX parse error: Expected 'EOF', got '}' at position 14: match:{sex:0}}?,{project:{_id:0,sex:1,totalnum:{sum:["sum:["sum:["score.chinese”,“score.math","score.math","score.math","score.english”]}}},{KaTeX parse error: Expected 'EOF', got '}' at position 19: …t:{totalnum:-1}}?]) 6、統計總分最高與總分最…group:{_id:“KaTeX parse error: Expected '}', got 'EOF' at end of input: sno",score:{sum:{sum:["sum:["sum:["score.chinese”,“score.math","score.math","score.math","score.english”]}}}},{KaTeX parse error: Expected 'EOF', got '}' at position 15: sort:{score:1}}?,{group:{_id:””,maxSno:{first:"first:"first:"_id”},maxScore:{first:"first:"first:"score”},minSno:{last:"last:"last:"_id”},minScore:{last:"last:"last:"score”}}},{KaTeX parse error: Expected 'EOF', got '}' at position 62: …o:1,minScore:1}}?]) 7、統計每名的學生的平均…project:{_id:0,sno:1,name:1,arr:{avg:["avg:["avg:["score.chinese",“score.math","score.math","score.math","score.english”]}}}])
8、統計所有學生總分的平均分數
db.class1.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …id:null,score:{avg:{KaTeX parse error: Expected '}', got 'EOF' at end of input: sum:{sum:[“score.chinese","score.chinese","score.chinese","score.math”,“KaTeX parse error: Expected 'EOF', got '}' at position 16: score.english"]}?}}}}]) 大題 Java …gte”, 2) .append(“lt",5)).append("categories","Bakery"))3、collection.find(regex("name","tom"));遍歷輸出結果FindIterable<Document>iterable=collection.find()MongoCursor<Document>mongoCursor=iterable.iterator();while(mongoCursor.hasNext())System.out.println(mongoCursor.next());聚合操作collection.aggregate(Arrays.asList(Aggregates.match(eq("categories","Bakery")),Aggregates.group("lt", 5)) .append("categories", "Bakery")) 3、collection.find(regex("name", "^tom")); 遍歷輸出結果 FindIterable<Document> iterable = collection.find() MongoCursor<Document> mongoCursor = iterable.iterator(); while(mongoCursor.hasNext()){ System.out.println(mongoCursor.next()); } 聚合操作 collection.aggregate( Arrays.asList( Aggregates.match(eq("categories", "Bakery")), Aggregates.group("lt",5)).append("categories","Bakery"))3、collection.find(regex("name","tom"));遍歷輸出結果FindIterable<Document>iterable=collection.find()MongoCursor<Document>mongoCursor=iterable.iterator();while(mongoCursor.hasNext())System.out.println(mongoCursor.next());聚合操作collection.aggregate(Arrays.asList(Aggregates.match(eq("categories","Bakery")),Aggregates.group("stars”, Accumulators.sum(“count”, 1))
)
);
增加
List documents = new ArrayList(); documents.add(doc1);
documents.add(doc2);
collection.insertMany(documents);
collection.insertOne(document);
修改
import static com.mongodb.client.model.Updates.*;
collection.updateMany( eq(“stars”, 2),
combine(set(“stars”, 0), currentDate(“lastModified”)));
collection.updateOne( eq("_id", 1), combine(set(“name”, “Fresh Breads and Tulips”), currentDate(“lastModified”)),new UpdateOptions().upsert(true).bypassDocumentValidation(true));
刪除:
collection.deleteMany(eq(“stars”, 4));
collection.deleteOne(eq("_id", 12)));
封裝:
public class MongoUtil {
private MongoClient mongoclient;
private MongoDatabase mongodatabase;
private MongoCollection grade1;
}
public class MongoTest {
public static void main(String[] args) {//連接test數據庫,并使用javatest集合MongoUtil mo = new MongoUtil("test","javatest");//定義插入的文檔listList<Document> lists =new ArrayList<Document>();Document doc = null;//循環添加數據到集合javatestfor(int i=1;i<=10;i++) {doc = new Document();Document doc1= new Document();doc.append("name", "zhangsan"+i);doc.append("sex", Math.round(Math.random() * 10) % 2);doc.append("age", Math.round(Math.random() * 6) + 3);doc.append("hobby", doc1);lists.add(doc);System.out.println(doc);}//插入數據到集合mo.insert(lists);// 2、grade1中,追加zhangsan7`學生興趣愛好為唱歌
Bson filter2 = Filters.eq(“name”, “zhangsan7”);
Document doc2 = new Document("$set",new Document(“hobby”,“唱歌”));
mo.updateOne(filter2,doc2,null);
// 3、 給grade1所有學生的年齡都增加一歲
Bson filter3 = Filters.exists("_id");
Document doc3 = new Document("$inc",new Document(“age”,1));
mo.updateMany(filter3,doc3,null);
// 4、grade1中刪除zhangsan7學生的sex字段
Bson filter4 = Filters.eq(“name”,“zhangsan7”);
Document doc4 = new Document("$unset",new Document(“sex”,""));
mo.updateOne(filter4,doc4,null);
// 5、查看grade1的學生,按年紀升序
Document doc5 = new Document(“age”,1);
mo.findAll(doc5);
// 6、統計sex為0 的學生的個數以及所有學生的平均年齡
List list6 = Arrays.asList(Aggregates.match(Filters.eq(“sex”,0)),Aggregates.group("$sex",Accumulators.avg(“count”,1)));
mo.aggregate(list6);
// 7、刪除sex為0并且age大于5的學生
Bson filter7 = Filters.and(Filters.eq(“sex”,0),Filters.gt(“age”, 5));
mo.deleteMany(filter7);
}
}
總結
以上是生活随笔為你收集整理的测试综合技能的期末预测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日程序C语言49-猴子分桃子问题
- 下一篇: 408考研感受