標籤:文檔 write _id code window -- 使用 weight first
MongoDB資料庫中,建立、查詢、更新、刪除操作的對象是集合。
1.查看某個資料庫中有哪些集合,在此之前需要使用資料庫
C:\Windows\system32>mongoMongoDB shell version: 3.2.10connecting to: test> dbtest> show dbs;chengdu 0.004GBfirst 0.000GBlocal 0.000GB> use first;switched to db first> show collections;blogcolrunoob>
命令解釋:
mongo ---> 串連資料庫, 在執行之前確保MongoDB資料庫服務開啟了
db ---> 查看當前正在使用的資料庫
show dbs --> 查看本地磁碟上那些資料庫
use [DatabaseName] --> 使用某個(DatebaseName)資料庫,若該資料庫不存在,則首先建立資料庫再使用
show collections -->查看當前資料庫中的集合
2. 向集合中插入一條資料
> show collections;blogcolrunoob> dbfirst> user = { ‘name‘:‘chengdu‘,... ‘sex‘ : ‘M‘,... ‘age‘ : 22 }{ "name" : "chengdu", "sex" : "M", "age" : 22 }> db.users.insert(user);WriteResult({ "nInserted" : 1 })> show collections;blogcolrunoobusers>
首先建立了一個文檔對象user,然後再將文檔user插入集合users
命令解釋:
> user = { ‘name‘:‘chengdu‘,
... ‘sex‘ : ‘M‘,
... ‘age‘ : 22 }
{ "name" : "chengdu", "sex" : "M", "age" : 22 } --> 建立一個文檔對象 user
db.users.insert(user) -->向集合users中插入文檔user, 如果集合users不存在,則先建立集合, 然後再插入文檔對象
3.查詢噹噹前集合中的文檔對象,有兩個方法 find() 和 findOne()。find()查看所有的文檔對象,但在shell中最多顯示20個。 findOne()查看第一個文檔對象,只返回一個文檔對象。
> db.users.find();{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "chengdu", "sex" : "M", "age" : 22 }> db.users.findOne();{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "chengdu", "sex" : "M", "age" : 22}>
具體查詢某個文檔對象,帶參數的查詢
> db.users.find({‘name‘:‘chengdu‘});{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "chengdu", "sex" : "M", "age" : 22 }> db.users.findOne({‘name‘:‘chengdu‘});{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "chengdu", "sex" : "M", "age" : 22}>
4.更新集合中的某個文檔對象
> user = { ‘name‘ : ‘cd‘,... ‘sex‘ : ‘M‘,... ‘age‘ : 22 }{ "name" : "cd", "sex" : "M", "age" : 22 }> db.users.update({‘name‘ : ‘chengdu‘}, user);WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.find();{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "cd", "sex" : "M", "age" : 22 }>
使用update語句更新原來集合中name為chengdu的文檔對象,更新之後 name變為cd
在文檔對象上增加一個鍵
> user{ "name" : "cd", "sex" : "M", "age" : 22 }> user.address = ‘Shanghai‘;Shanghai> user{ "name" : "cd", "sex" : "M", "age" : 22, "address" : "Shanghai" }> db.users.update({‘name‘:‘cd‘}, user);WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.find();{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "cd", "sex" : "M", "age" : 22, "address" : "Shanghai" }>
5.刪除某個文檔對象,使用remove()方法
> user1 = { ‘name‘ : ‘xiaohong‘,... ‘sex‘ : ‘F‘,... ‘age‘ : 22,... ‘address‘ : ‘Beijing‘,... ‘qq‘ : ‘12345678‘}{ "name" : "xiaohong", "sex" : "F", "age" : 22, "address" : "Beijing", "qq" : "12345678"}> db.users.insert(user1);WriteResult({ "nInserted" : 1 })> db.users.find();{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "cd", "sex" : "M", "age" : 22, "address" : "Shanghai" }{ "_id" : ObjectId("584eb4bd7629396db95535db"), "name" : "xiaohong", "sex" : "F", "age" : 22, "address" : "Beijing", "qq" : "12345678" }> db.users.remove({‘name‘ : ‘xiaohong‘});WriteResult({ "nRemoved" : 1 })> db.users.find();{ "_id" : ObjectId("584eafa97629396db95535da"), "name" : "cd", "sex" : "M", "age" : 22, "address" : "Shanghai" }>
首先建立了一個文檔對象,該文檔對象的鍵和之前文檔對象的鍵的數目不同,然後再將文檔對象插入到集合中,MongoDB的一個集合中可以儲存鍵數目不同的文檔對象,即可以插入成功,然後使用remove方法將插入的文檔刪除。
MongoDB 的建立、查詢、更新、刪除