您好,登錄后才能下訂單哦!
數(shù)據(jù)庫(kù)會(huì)用到創(chuàng)建(create)讀取(find)更新(update)刪除(remove),MongoDB也同樣會(huì)用到;
一、創(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集合為空;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。