MongoDB 的创建、查询、更新、删除

    MongoDB数据库中,创建、查询、更新、删除操作的对象是集合。

1.查看某个数据库中有哪些集合,在此之前需要使用数据库

C:\Windows\system32>mongo
MongoDB shell version: 3.2.10
connecting to: test
> db
test
> show dbs;
chengdu  0.004GB
first    0.000GB
local    0.000GB
> use first;
switched to db first
> show collections;
blog
col
runoob
>

命令解释:    

 mongo ---> 连接数据库, 在执行之前确保MongoDB数据库服务开启了

 db  ---> 查看当前正在使用的数据库

 show dbs   --> 查看本地磁盘上那些数据库

 use  [DatabaseName]  --> 使用某个(DatebaseName)数据库,若该数据库不存在,则首先创建数据库再使用

 show  collections  -->查看当前数据库中的集合

2. 向集合中插入一条数据

> show collections;
blog
col
runoob
> db
first
> user = { 'name':'chengdu',
...        'sex' : 'M',
...        'age' :  22 }
{ "name" : "chengdu", "sex" : "M", "age" : 22 }
> db.users.insert(user);
WriteResult({ "nInserted" : 1 })
> show collections;
blog
col
runoob
users
>

  首先创建了一个文档对象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方法将插入的文档删除。

 

posted @ 2016-12-12 22:37  shootercheng  阅读(1103)  评论(0编辑  收藏  举报