mongodb筆記1(基本操作,增刪改)

來源:互聯網
上載者:User

標籤:insert   資料庫   

  1. 查詢現有資料庫

>show dbs


2.建立資料庫,需要建立集合這個庫才建立

>use mydb


3.查看集合

>show collections或者>show tables


4.建立文檔並插入資料

>db.userInfo.insert({_id:1,name:"xiaoming"})


5.批量插入文檔,shell是不支援批量插入的,要想完成批量插入可以用mongodb的應用驅動或者shell的for迴圈

>for(var i =0;i<10;i++){..db.userInfo.insert({name:i})..}


6.查看文檔內容

>db.userInfo.find()


7.save操作,save操作和insert操作區別在於遇到_id相同的情況下

save完成儲存操作

insert則會報錯

> db.userInfo.insert({_id:1,name:"xiaoming"})WriteResult({ "nInserted" : 1 })> db.userInfo.insert({_id:1,name:"xiaocang"})WriteResult({"nInserted" : 0,"writeError" : {"code" : 11000,"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: foobar.userInfo.$_id_  dup key: { : 1.0 }"}})> db.userInfo.save({_id:1,name:"xiaocang"})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


8.刪除列表中所有資料,集合和索引不會被刪除

> db.userInfo.remove({})WriteResult({ "nRemoved" : 1 })> db.userInfo.find()> show collectionssystem.indexesuserInfo


9.根據條件刪除 

db.[documentName].remove({})

刪除集合中的name等於xiaoming的記錄

db.userInfo.remove({name:"xiaoming"})




本文出自 “每天進步一點點” 部落格,請務必保留此出處http://08jw3.blog.51cto.com/998816/1675975

mongodb筆記1(基本操作,增刪改)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.