您好,登錄后才能下訂單哦!
數據庫會用到創建(create)讀取(find)更新(update)刪除(remove),MongoDB也同樣會用到;
一、創建
用insert函數將文檔添加到集合中。例如
創建數據庫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增加一個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集合為空;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。