3, mongodb中有三个重要的概念:数据库(dbs), 集合(collections), 文档,其中“集合”就是对应关系数据库中的“表”,“文档”对应“行”。
To display a list of all databases on the server, use the following command: show dbs. show dbs 命令,显示我们服务器上有多少个数据库。 在mongo shell 命令窗口中,输入 show dbs, 如下,我们没有任何数据库。
创建和使用哪一个数据库,用的是use 数据库名 命令。 在mongo shell 命令窗口中输入 use demo , 如果有这个数据库的话,它就会用这个数据库,如果没有这个数据库,它就会新建一个数据库demo. To switch databases to any of those listed in the output of show dbs , use the use demo command。An interesting thing to note is that if you use on a database that doesn't exist, one is created automatically. 输入 use demo, 可以看到我们进入到了 demo 数据库中。db 命令 返回我们现在正在哪个数据库中。命令窗口中再输入db 可以看到 返回demo
如果我们想看我们所在的数据库中有多少个集合collections, 使用show collections 命令, 相当于我们查询数据库中有多少张表。 If you are using an existing database and want to view a list of collections in the database, execute the command: > show collections 命令行中输入 show collections 返回 空,表示我们现在没有任何集合。 删除数据库: db.dropDatabase() 命令,我们在哪个数据库下,它就会删除哪个数据库。 注意:当我们创建demo 数据库以后,show dbs 并没有显示我们新增的数据库,这是因为我们的数据库中没有任何东西,所占空间是0。
先简单写入内容,操作一下上面的命令。 命令行中输入db.newCollection.insert({name:'sam'}) 。