Mongodb 基础 之 初识

数据库操作

查看:show dbs;

创建:use dbname; // db.createCollection('collection_name');    隐式创建,需要创建的数据库中有表才表示创建成功

删除:use dbname; db.dropDatabase();   // 选择好对应的数据库,然后删除

如下实例:

查看数据库:


> show dbs; admin 0.000GB local 0.000GB test_1 0.000GB >

 创建数据库:隐式创建,直接use不存在的数据库

> use student;
switched to db student

 查看没有:因为没有在这个数据库中创建表,只有创建表后才创建成功

> use student;
switched to db student
> show dbs;
admin   0.000GB
local   0.000GB
test_1  0.000GB

 下面表示创建成功:

> use student;
switched to db student
> show dbs;
admin   0.000GB
local   0.000GB
test_1  0.000GB
> 
> show collections;
> 
> show tables;
> db.createCollection('minstudent');
{ "ok" : 1 }
> show tables;
minstudent
> show dbs;
admin    0.000GB
local    0.000GB
student  0.000GB
test_1   0.000GB

 删除数据库:

> db.dropDatabase();
{ "dropped" : "student", "ok" : 1 }
> 

数据文档操作

这里的文档类似与MySQL中的表,因为Mongodb是文档数据库,所以可以说是对文档的操作;

创建:db.createCollection('collection_name');

删除:db.collection_name.drop();

查看:show collections;

如下实例:

创建

> use school;
switched to db school
> 
> db.createCollection('student');
{ "ok" : 1 }

 删除

 
> db.student.drop();
true

 查看

> db.teacher.insert({'name':'teacher1'});
WriteResult({ "nInserted" : 1 })
> show collections;
teacher

文档内容操作

插入数据:db.collection_name.insert({key:value})  单个插入

                  db.collection_name.insert({_id:value, key:value}) 有_id的插入

                  db.collection_name.insert([{key:value},{key:value}])   多个插入

查看:db.collection_name.find();    // 这里只例举简单的查询方法,会单独写一篇文档详细说明查询的方法

删除:db.collection_name.remove(查询表达式,选项)

修改:db.collection_name.update(查询表达式,新值,选项)

如下实例:

插入

> db.student.insert({'name': 'wallace'});
WriteResult({ "nInserted" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }

 修改_id

> db.student.insert({'name': 'bbbb', '_id':111});
WriteResult({ "nInserted" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "bbbb" }
{ "_id" : 111, "name" : "bbbb" }

 插入多条

> db.student.insert([{'age': 'bbbb', 'age':111, 'home': 'beijin'},{'age':'laowang', 'age': 1, 'home':'江西'}]);
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 2,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "bbbb" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810567cdad789abb91899c"), "age" : 111, "home" : "beijin" }
{ "_id" : ObjectId("5c810567cdad789abb91899d"), "age" : 1, "home" : "江西"}

删除数据

> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "bbbb" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810567cdad789abb91899c"), "age" : 111, "home" : "beijin" }
{ "_id" : ObjectId("5c810567cdad789abb91899d"), "age" : 1, "home" : "江西" }
{ "_id" : ObjectId("5c810575cdad789abb91899e"), "age" : 111, "home" : "beijin" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "age" : 1, "home" : "江西" }
> db.student.remove({home:'beijin'});
WriteResult({ "nRemoved" : 2 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "bbbb" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810567cdad789abb91899d"), "age" : 1, "home" : "江西" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "age" : 1, "home" : "江西" }
> db.student.remove({home:'江西'}, true);
WriteResult({ "nRemoved" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "bbbb" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "age" : 1, "home" : "江西" }
> 

修改

> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "bbbb" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "age" : 1, "home" : "江西" }
> db.student.update({home:'江西'},{home:北京});
2019-03-08T09:00:31.981-0500 E QUERY    [thread1] ReferenceError: \u5317\u4EAC is not defined :
@(shell):1:32
> db.student.update({home:'江西'},{home:'北京'});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "bbbb" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "home" : "北京" }

修改二:$set 具体到某一个键值对的值

> db.student.insert({'name':'bbb',age:18, classmate:4});
WriteResult({ "nInserted" : 1 })
> db.student.update({name:'bbb'}, {$set:{'age':19}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "1111" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "home" : "北京" }
{ "_id" : ObjectId("5c835969043334857a7dbe85"), "name" : "aaa", "age" : 18, "classmate" : 3 }
{ "_id" : ObjectId("5c835973043334857a7dbe86"), "name" : "bbb", "age" : 19, "classmate" : 4 }

修改三:$unset 删除某一对象的键值对

> db.student.update({name:'bbb'}, {$unset:{'age':19}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "1111" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "home" : "北京" }
{ "_id" : ObjectId("5c835969043334857a7dbe85"), "name" : "aaa", "age" : 18, "classmate" : 3 }
{ "_id" : ObjectId("5c835973043334857a7dbe86"), "name" : "bbb", "classmate" : 4 }
> 

修改列名;

> db.student.update({name:'bbb'}, {$rename:{'name':'sc'}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "1111" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "home" : "北京" }
{ "_id" : ObjectId("5c835969043334857a7dbe85"), "name" : "aaa", "age" : 18, "classmate" : 3 }
{ "_id" : ObjectId("5c835973043334857a7dbe86"), "classmate" : 4, "sc" : "bbb" }
> 

增加值:

> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "1111" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "home" : "北京" }
{ "_id" : ObjectId("5c835969043334857a7dbe85"), "name" : "aaa", "age" : 37, "classmate" : 3 }
{ "_id" : ObjectId("5c835973043334857a7dbe86"), "classmate" : 4, "sc" : "bbb" }
> db.student.update({name:'aaa'},{$inc:{age:10}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find();
{ "_id" : ObjectId("5c810428cdad789abb91899a"), "name" : "wallace" }
{ "_id" : ObjectId("5c810441cdad789abb91899b"), "name" : "1111" }
{ "_id" : 111, "name" : "bbbb" }
{ "_id" : ObjectId("5c810575cdad789abb91899f"), "home" : "北京" }
{ "_id" : ObjectId("5c835969043334857a7dbe85"), "name" : "aaa", "age" : 47, "classmate" : 3 }
{ "_id" : ObjectId("5c835973043334857a7dbe86"), "classmate" : 4, "sc" : "bbb" }
> 

 以上的再属于Mongodb初识的内容,接下来会对再文档内容超做过程中的查询表达式进行详细的讲解。

上一篇:2020MongoDB基本命令-R操作(查)


下一篇:【MongoDB详细使用教程】三、高级查询