mongodb简介
MongoDB has declared itself the leading NoSQL database based on their statistics and its hard to argue that it isn't popular.
MongoDB基于其统计数据已经宣布自己是领先的NoSQL数据库 ,并且很难说它不受欢迎。
MongoDB has been used in the beloved MEAN stack. The benefits of using a document database like MongoDB means that you can work with JSON-style documents across your entire development stack.
MongoDB已在最受欢迎的MEAN堆栈中使用。 使用像MongoDB这样的文档数据库的好处意味着您可以在整个开发堆栈中使用JSON样式的文档 。
You are able to work with JSON on the frontend (Angular), the backend (Node), and the database (MongoDB).
您可以在前端(Angular),后端(Node)和数据库(MongoDB)上使用JSON。
For more benefits to MongoDB, be sure to check out the official site. Let's get into installing and taking MongoDB for a quick test run.
要获得MongoDB的更多好处,请务必查看官方网站 。 让我们开始安装和使用MongoDB进行快速测试。
安装和运行MongoDB (Installation and Running MongoDB)
Getting MongoDB installed on your computer carries with it some simple steps. They are:
在计算机上安装MongoDB会附带一些简单的步骤。 他们是:
- Run the MongoDB installation 运行MongoDB安装
- Create a physical directory for items to be stored 为要存储的项目创建物理目录
Just like that, we'll be able to use MongoDB locally!
这样,我们将能够在本地使用MongoDB!
The following instructions are for Mac users, MongoDB provides great installation instructions for Windows and Linux as well.
以下说明适用于Mac用户,MongoDB还为Windows和Linux提供了出色的安装说明。
The default folder when installing will be /data/db. This is where everything will be stored and you must make sure that you have permissions to this folder so that MongoDB can write to it.
安装时的默认文件夹为/ data / db 。 这是所有内容的存储位置,您必须确保拥有对该文件夹的权限,以便MongoDB可以对其进行写入。
We can install on Macs using either Homebrew or manually. We'll be focusing on Homebrew for this article.
我们可以使用Homebrew或手动在Mac上安装。 本文将重点介绍Homebrew。
// update your packages
$ brew update
// install mongoDB
$ brew install mongodb
If you would prefer not to use Homebrew, the MongoDB site gives other instructions.
如果您不想使用Homebrew,则MongoDB网站会提供其他说明 。
使用MongoDB入门 (Getting Started Using MongoDB)
There are two steps to get working with MongoDB locally:
要在本地使用MongoDB,有两个步骤:
- Start the process 开始过程
- Connect 连接
Sounds simple enough! Let's get to it!
听起来很简单! 让我们开始吧!
启动MongoDB服务 (Starting MongoDB Service)
Before we can start creating and saving documents in our database, we have to start up MongoDB. There won't be anything to save to if the service isn't started! The command is very simple to get MongoDB up and running:
在开始在数据库中创建和保存文档之前,我们必须启动MongoDB。 如果服务没有启动,将没有任何可保存的东西! 该命令非常简单,可以启动和运行MongoDB:
$ mongod
Once we have issue this command, MongoDB will start up and you should see: waiting for connections on port 27017
发出此命令后,MongoDB将启动,您将看到: waiting for connections on port 27017
![mongo-start](https://i-blog.csdnimg.cn/blog_migrate/0eb2d65b5fd619b977abc476e9249924.png)
连接到MongoDB服务 (Connecting to the MongoDB Service)
With our MongoDB service running, we just have to connect to it. While mongod
starts MongoDB, the command to connect to it is:
运行我们的MongoDB服务后,我们只需要连接到它即可。 当mongod
启动MongoDB时,连接到它的命令是:
$ mongo
We are now connected and are able to issue our MongoDB commands.
现在,我们已连接,并能够发出我们的MongoDB命令。
![mongo-connect](https://cask.scotch.io/2014/11/mongo-connect.jpg)
Notice on the right side, we have connected, and on the left side, our connection has been logged.
请注意,在右侧,我们已连接,在左侧,我们的连接已记录。
常用数据库命令 (Common Database Commands)
列出所有数据库 (List All Databases)
$ show dbs
创建数据库 (Creating a Database)
MongoDB will not create a database unless you insert information into that database.
除非您将信息插入数据库,否则MongoDB不会创建数据库。
The trick is you don't have to worry about explicitly creating a database! You can just use
a database (even if it's not created), create a collection and document, and everything will automatically be made for you!
诀窍是您不必担心显式创建数据库! 您可以use
数据库(即使未创建数据库),创建集合和文档,一切也将自动为您完成!
We'll look at creating collections and documents in the CRUD Commands section.
我们将在“ CRUD命令”部分中介绍如何创建集合和文档。
显示当前数据库 (Show Current Database)
$ db
选择一个数据库 (Select a Database)
$ use db_name
CRUD命令 (CRUD Commands)
创造 (Create)
// save one user
$ db.users.save({ name: 'Chris' });
// save multiple users
$ db.users.save([{ name: 'Chris'}, { name: 'Holly' }]);
By saving a document into the users
collection of the database you are currently in, you have successfully created both the database and collection if they did not already exist.
通过将文档保存到您当前所在的数据库的users
集合中,可以成功创建数据库和集合(如果尚不存在)。
读 (Read)
// show all users
$ db.users.find();
// find a specific user
$ db.users.find({ name: 'Holly' });
更新资料 (Update)
db.users.update({ name: 'Holly' }, { name: 'Holly Lloyd' });
删除 (Delete)
// remove all
db.users.remove({});
// remove one
db.users.remove({ name: 'Holly' });
This is just a quick overview of the types of commands you can run. The MongoDB docs are quite comprehensive and provide a great deal of detail for those that want to dive deeper.
这只是您可以运行的命令类型的快速概述。 MongoDB文档非常全面,为想要深入研究的人员提供了大量详细信息。
MongoDB also provides a great interactive tutorial for you to walk through the above commands.
MongoDB还为您提供了一个出色的交互式教程 ,以指导您完成上述命令。
在Node.js应用程序中使用 (Using In a Node.js Application)
Using a local instance of MongoDB is a very simple process. We will use mongooseJS, the Node package for working with MongoDB.
使用MongoDB的本地实例是一个非常简单的过程。 我们将使用mongooseJS ,即与MongoDB一起使用的Node包。
All you have to do is configure mongoose to connect to a local database. This is a simple process since we don't even need to create the database. We just have to make sure that MongoDB is running by running:
您所要做的就是配置猫鼬以连接到本地数据库。 这是一个简单的过程,因为我们甚至不需要创建数据库。 我们只需要通过运行以下命令来确保MongoDB正在运行:
$ mongod
We will also give mongoose a database name to connect to (it doesn't need to be created since mongoose will create it for us).
我们还将为mongoose提供一个要连接的数据库名称(因为mongoose会为我们创建数据库名称,因此无需创建数据库名称)。
Here's some sample code to connect to a database in Node.
这是一些连接到Node中数据库的示例代码。
// grab the packages we need
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db_name');
That's it! Once we start saving things into our database, that database named db_name
will automatically be created.
而已! 一旦开始将内容保存到数据库中,将自动创建名为db_name
数据库。
For more information on using Mongoose inside of a Node application, read: Using MongooseJS in Node.js and MongoDB Applications.
有关在Node应用程序中使用Mongoose的更多信息,请阅读: 在Node.js和MongoDB Applications中使用MongooseJS 。
GUI工具:Robomongo (GUI Tool: Robomongo)
While it's easy enough to use our command line to get into our MongoDB databases, there's also a GUI for those that are inclined.
使用命令行进入MongoDB数据库很容易,但是对于那些喜欢的人也有一个GUI。
Go ahead and download Robomongo and fire it up.
继续下载Robomongo并启动它。
Creating a connection to our database is very easy. Just set the address to localhost
and the port to 27017
. Name your connection anything you want.
创建到我们数据库的连接非常容易。 只需将地址设置为localhost
并将端口设置为27017
。 根据需要命名您的连接。
![robomongo-connect-local](https://i-blog.csdnimg.cn/blog_migrate/bf6ee7ffa2ee75f7a24c2cc1843347ab.png)
Then once you connect, you have access to all the databases and their respective collections! You now have a GUI to handle database operations and you can even open a shell to get down and dirty with the commands we discussed earlier.
然后,一旦连接,就可以访问所有数据库及其各自的集合! 现在,您已经具有一个GUI来处理数据库操作,甚至可以打开一个外壳来使用我们前面讨论的命令进行操作。
![robomongo-local-db](https://cask.scotch.io/2014/11/robomongo-local-db.jpg)
结论 (Conclusion)
MongoDB is a great NoSQL database that can be configured quickly and used in any of our applications. For Node applications, we can start up MongoDB quickly so that we can get to the fun part, building applications!
MongoDB是一个很棒的NoSQL数据库,可以快速配置并在我们的任何应用程序中使用。 对于Node应用程序,我们可以快速启动MongoDB,以便我们可以开始有趣的部分,构建应用程序!
mongodb简介