數(shù)據(jù)庫(kù)會(huì)用到創(chuàng)建(create)讀取(find)更新(update)刪除(remove),MongoDB也同樣會(huì)用到;
在平潭等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供成都網(wǎng)站制作、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作定制設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)整合營(yíng)銷(xiāo)推廣,外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè),平潭網(wǎng)站建設(shè)費(fèi)用合理。
一、創(chuàng)建
用insert函數(shù)將文檔添加到集合中。例如
創(chuàng)建數(shù)據(jù)庫(kù)blog,將文檔增加到集合post中(先將文檔放入post的變量中)
> post={"title":"My blog post","context":"Here's my blog post","date":new Date()} > use blog switched to db blog > db.post.insert(post); WriteResult({ "nInserted" : 1 })
二、讀取
用find方法或者findone方法查看集合中的文檔,例如
> db.post.find() { "_id" : ObjectId("54a50253e287e09898eab58b"), "title" : "My blog post", "context" : "Here's my blog post", "date" : ISODate("2015-01-01T08:15:14.121Z") } > db.post.findOne() { "_id" : ObjectId("54a50253e287e09898eab58b"), "title" : "My blog post", "context" : "Here's my blog post", "date" : ISODate("2015-01-01T08:15:14.121Z") } >
三、更新
重新給變量post賦值
> use blog switched to db blog > post=db.post.findOne() { "_id" : ObjectId("54a50253e287e09898eab58b"), "title" : "My blog post", "context" : "Here's my blog post", "date" : ISODate("2015-01-01T08:15:14.121Z") }
給變量post增加一個(gè)comments文檔
> post.comments = [] [ ]
update方法更新集合
> db.post.update({"title":"My blog post"},post) ;WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.post.findOne() { "_id" : ObjectId("54a50253e287e09898eab58b"), "title" : "My blog post", "context" : "Here's my blog post", "date" : ISODate("2015-01-01T08:15:14.121Z"), "comments" : [ ] }
四、刪除
用removed方法刪除文檔
> db.post.remove({"title":"My blog post"}); WriteResult({ "nRemoved" : 1 }) > db.post.findOne(); null >
刪除后post集合為空;