Mogodb's Shell Basic Operation Notes (CURD) [3]

MongoDB's Shell Basic Operations

Please reprint from the source: http://eksliang.iteye.com/blog/2174081

1. mongodb client

        When we run mongo 192.168.238.133:27017/test on the client, the shell will connect to the test database of the mongodb server and assign the database connection to the global variable db. This variable is the main entry point for the shell to access mongodb.

 

2. Shell basic operation

1. Connect to mongodb

mongo 127.0.0.1:27017/test

2. Create a user

 use user

3. View all databases

show dbs

4. Add a collection to the specified database

方式一:db.user.insert({"name":"ickes"})

This will add a collection to the database and add a record to the collection

方式二:db.createCollection("scot.user")

5. View the collections in the current database

show collections

6. View the currently used database

db

 

3. CURD of the shell to the database

1. The insert() function can add a document to the collection

Format: db.collectionName.insert({document})

> user={"userName":"ickes","userPwd":"xl123"}
db.user.insert(user);

 2. find() or findOne() to query the documents in the collection (document query is very flexible, which will be discussed later)

Format: db.collectionName.find() or db.collectionName.findOne()

> db.user.find() -- query all documents
{ "_id" : ObjectId("54ae3f26d5ef861093d41356"), "name" : "ickes" }
{ "_id" : ObjectId("54ae4bf10854b94e41b45ba4"), "userName" : "ickes", "userPwd"
: "xl123" }

  

> db.user.findOne()--查询第一条数据
{ "_id" : ObjectId("54ae3f26d5ef861093d41356"), "name" : "ickes" }

3、修改文档(文档修改非常灵活,后面会专门讨论)

 修改使用update()函数

格式:db.collectionName.update({查询条件},{更新条件})

> user=db.user.findOne()   --查询第一个文档
{ "_id" : ObjectId("54ae3f26d5ef861093d41356"), "name" : "ickes" }
> user.name='update.ickes' --将name修改为updae.ickes
update.ickes
> db.user.update({"name":"ickes"},user) --更name=ickes的文档
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.findOne()      --再次查看,果然更新成功
{ "_id" : ObjectId("54ae3f26d5ef861093d41356"), "name" : "update.ickes" }

 实际上上面这种方式是文档的替换,上面执行db.user.update()时,实际上是将新文档替换数据库中匹配的文档。怎么只修改文档的某个属性,后面会单独讲,mongodb的修改器

 

4、删除文档

使用remove()函数,可将文档从数据库中永久删除。

格式db.collectionName.remove({查询条件}),如果查询条件为空会删除索引文档,记住空是这样的空(db.collectionName.remove({}))

> db.user.find()--先查看当前集合中的文档,两个
{ "_id" : ObjectId("54ae3f26d5ef861093d41356"), "name" : "update.ickes" }
{ "_id" : ObjectId("54ae51210854b94e41b45ba5"), "pwd" : "aa" }
> db.user.remove({"pwd":"aa"})  --删除pwd=aa的文档
WriteResult({ "nRemoved" : 1 }) --打印删除成功
> db.user.find()                --再次查看,果然修改成功了
{ "_id" : ObjectId("54ae3f26d5ef861093d41356"), "name" : "update.ickes" }

 

 

四、删除数据中的集合

格式 db.collectionName.drop();

> db.user.drop()
true
> show collections
system.indexes
test

 

五、删除数据库

格式db.dropDatabase()

> show dbs   --查看当前实例拥有的数据库
admin  (empty)
local  0.078GB
test   0.078GB
user   0.078GB
> use user  --切换到user数据库
switched to db user
> db        --查看一下,是否真的切换成功
user
> db.dropDatabase() --删除当前数据库
{ "dropped" : "user", "ok" : 1 }
> show dbs          --再次查看,果然没有了
admin  (empty)
local  0.078GB
test   0.078GB

 

 

 六、Shell帮助查看

       由于MongoDB是一个简化了JavaScript的shell,可以通过查看JavaScript的在线文档得到大量帮助。对于MongoDB特有的功能,shell内置了帮助文档,可以使用help()方法进行查看

      数据库级别的帮助:db.help()

      集合级别的帮助:db.collectionName.help()

      如果想知道一个函数是做什么的,可以直接在shell中输入函数名(函数名不要括号),这样可以直接看到该函数的JavaScript代码,例如

      

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484238&siteId=291194637