#向集合中插入文檔有兩種方式(insert、save) db.col.insert({title: 'MongoDB 教程', description: 'MongoDB 是一個 Nosql 數據庫', by: '菜鳥教程', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }); # 也能夠把文檔賦值給變量 document=({title: 'MongoDB 教程', description: 'MongoDB 是一個 Nosql 數據庫', by: '菜鳥教程', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }); db.col.insert(document)
#查詢集合中的文檔,經過find db.col.find(); { "_id" : ObjectId("5d020b62f99940c1effaa307"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5d020b80f99940c1effaa308"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5d020b81f99940c1effaa309"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5d020b82f99940c1effaa30a"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5d020b83f99940c1effaa30b"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5d020b84f99940c1effaa30c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5d020b84f99940c1effaa30d"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5d020b85f99940c1effaa30e"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
比較insert和save的區別:java
其實insert和save在不指定_id的時候效果是同樣的,若是指定了_id字段,那麼save就是更新當前文檔信息sql
{ "_id" : ObjectId("5d020beff99940c1effaa316"), "title" : "MongoDB 教程", "description" : "MongoDB 是一個 Nosql 數據庫", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } #利用save方法指定_id db.col.save({ "_id":"5d020beff99940c1effaa316", "title" : "java 教程", "description" : "java是一個跨平臺的編程語言", "by" : "菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "java8", "註解", "反射", "網絡編程", "epoll 事件驅動", "多路複用", "聲明式API" ], "likes" : 100 }); #結果以下 { "_id" : "5d020beff99940c1effaa316", "title" : "java 教程", "description" : "java是一個跨平臺的編程語言", "by" : " 菜鳥教程", "url" : "http://www.runoob.com", "tags" : [ "java8", "註解", "反射", "網絡編程", "epoll 事件驅動", "多路 複用", "聲明式API" ], "likes" : 100 }
#save和update的區別,save通常是經過_id字段匹配原先的文檔後用新的文檔替換掉原先的文檔,而upate是根據條件對局部進行更新mongodb
#update的語法以下: db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } ) query : update的查詢條件,相似sql update查詢內where後面的。 update : update的對象和一些更新的操做符(如$,$inc...)等,也能夠理解爲sql update查詢內set後面的 upsert : 可選,這個參數的意思是,若是不存在update的記錄,是否插入objNew,true爲插入,默認是false,不插入。 multi : 可選,mongodb 默認是false,只更新找到的第一條記錄,若是這個參數爲true,就把按條件查出來多條記錄所有更新。 writeConcern :可選,拋出異常的級別