MongoDB資料庫的操作,增刪改查

來源:互聯網
上載者:User

標籤:log   employee   南京   牛津大學   邏輯   運算   turn   lin   pre   

在student集合中插入一些資料

db.student.insert({"學號":10010,"姓名":"德萊文","年齡":26,"地址":"淄博市"});var stuData1={"學號":10020,"姓名":"奧巴馬","年齡":27,"地址":"艾澤拉斯"};db.student.insert(stuData1);var stuData2={"學號":10030,"姓名":"女槍","年齡":27,"地址":"艾澤拉斯"};db.student.insert(stuData2);var stuData3={"學號":10050,"姓名":"瑞雯","年齡":18,"地址":"超神學院"};db.student.insert(stuData3);

  插入數組(資料為json)

var stuData=[{"姓名":"Anner","age":26},{"姓名":"James","age":28},{"姓名":"Allen","age":31},{"姓名":"Smith","age":17}];db.student.insert(stuData);

  插入insert(),用JavaScript操作

//添加1萬條資料for(var i=0;i<10000;i++){db.student.insert({"vid":"www.oracle.com"+i+"line"});};

  查看資料

db.student.find();

  ●資料查詢

   樣本1:查詢學號為10020的記錄

db.student.find({"學號":10020});db.student.find({"學號":10020}).pretty();

  樣本2:不顯示_id的查詢

db.student.find({"學號":10020},{"_id":0});

  ●關係運算

> $gt
< $lt
>= $gte
<= $lte
!= $ne
= key:value $eq

定義一個員工資訊集合

db.employee.drop();db.employee.insert([{"name":"Smith","age":25,"sex":"男","job":"教授","addr":"北京大學","cellphone":"17166660001"},{"name":"Clark","age":29,"sex":"男","job":"講師","addr":"燕京大學","cellphone":"17166660002"},{"name":"Allen","age":28,"sex":"女","job":"主任","addr":"南京大學","cellphone":"17166660003"},{"name":"Anner","age":15,"sex":"女","job":"宣傳","addr":"東京大學","cellphone":"17166660004"},{"name":"James","age":19,"sex":"男","job":"教務","addr":"西京大學","cellphone":"17166660005"}]);db.employee.find();db.employee.find().pretty();

  樣本3:查詢姓名為Anner的記錄

db.employee.find({"name":"Anner"}).pretty();

  樣本4:查詢性別為男的記錄

db.employee.find({"sex":"男"}).pretty();

  樣本5:查詢年齡大於19歲的記錄

db.employee.find({"age":{"$gt":19}}).pretty();

  樣本6:查詢年齡小於等於19歲的記錄

db.employee.find({"age":{"$lte":19}}).pretty();

  樣本7:查詢性別不是女的的記錄

db.employee.find({"sex":{"$ne":"女"}}).pretty();

  ●邏輯運算

與 $and
或 $or
非 $not $nor

  樣本8:查詢年齡在10~20之間的記錄

db.employee.find({"age":{"$gt":10,"$lte":20}});db.employee.find({"age":{"$gt":10,"$lte":20}}).pretty();

  樣本9:查詢年齡不是19歲的記錄

db.employee.find({"age":{"$ne":19}});

  樣本10:查詢年齡大於19歲,或者性別為女的記錄

db.employee.find({"age":{"$gt":19}},);db.employee.find({"$or":[{"age":{"$gt":19}},{"sex":{"$eq":"女"}}]});

  樣本11:對上一題求反

db.employee.find({"$nor":[{"age":{"$gt":19}},{"sex":{"$eq":"女"}}]});

  ●模運算 "$mod"

  樣本12:求年齡mod27餘1的記錄,也就是28

db.employee.find({"age":{"$mod":[27,1]}});

  

  ●範圍查詢
  $in $nin

  樣本13:查詢姓名是Allen,Anner,James的記錄

db.employee.find({"name":{"$in":["Allen","Anner","James"]}});

  樣本14:查詢姓名不是Allen,Anner,James的記錄

db.employee.find({"name":{"$nin":["Allen","Anner","James"]}})

  構建一個集合 employee集合

db.employee.insert([{"name":"Jones","age":31,"sex":"男","job":"技術員","addr":"哈佛大學","cellphone":"15166660001"},{"name":"Blake","age":35,"sex":"男","job":"銷售人員","addr":"牛津大學","cellphone":"15166660002"},{"name":"Martin","age":21,"sex":"男","job":"銷售人員","addr":"劍橋大學","cellphone":"15166660003"},{"name":"Turner","age":25,"sex":"女","job":"後勤","addr":"北京大學","cellphone":"15166660004"},{"name":"Miller","age":23,"sex":"女","job":"行政","addr":"南京大學","cellphone":"15166660005"}]);

  樣本15:查詢job不是"技術員"的記錄

db.emp.find({"jod":{"$nin":["技術員"]}});

  

MongoDB資料庫的操作,增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.