生命不止,继续 go go go !!!
昨天分享了golang如何操作redis数据库,那今天就介绍一下golang中如何使用mongodb数据库。
简介
MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。
在高负载的情况下,添加更多的节点,可以保证服务器性能。
MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象
MongoDB (from humongous) is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas. MongoDB is developed by MongoDB Inc. and is free and open-source, published under a combination of the GNU Affero General Public License and the Apache License.
特点
MongoDB的提供了一个面向文档存储,操作起来比较简单和容易。
你可以在MongoDB记录中设置任何属性的索引 (如:FirstName=”Sameer”,Address=”8 Gandhi Road”)来实现更快的排序。
你可以通过本地或者网络创建数据镜像,这使得MongoDB有更强的扩展性。
如果负载的增加(需要更多的存储空间和更强的处理能力) ,它可以分布在计算机网络中的其他节点上这就是所谓的分片。
Mongo支持丰富的查询表达式。查询指令使用JSON形式的标记,可轻易查询文档中内嵌的对象及数组。
MongoDb 使用update()命令可以实现替换完成的文档(数据)或者一些指定的数据字段 。
Mongodb中的Map/reduce主要是用来对数据进行批量处理和聚合操作。
Map和Reduce。Map函数调用emit(key,value)遍历集合中所有的记录,将key与value传给Reduce函数进行处理。
Map函数和Reduce函数是使用Javascript编写的,并可以通过db.runCommand或mapreduce命令来执行MapReduce操作。
GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件。
MongoDB允许在服务端执行脚本,可以用Javascript编写某个函数,直接在服务端执行,也可以把函数的定义存储在服务端,下次直接调用即可。
MongoDB支持各种编程语言:RUBY,PYTHON,JAVA,C++,PHP,C#等多种语言。
MongoDB安装简单。
MongoDB与redis的对比
MongoDB和Redis都是NoSQL,采用结构型数据存储。二者在使用场景中,存在一定的区别,这也主要由于二者在内存映射的处理过程,持久化的处理方法不同。
MongoDB建议集群部署,更多的考虑到集群方案,Redis更偏重于进程顺序写入,虽然支持集群,也仅限于主-从模式。
When to use Redis?
Caching
Caching using MongoDB simply doesn’t make a lot of sense. It would be too slow.
If you have enough time to think about your DB design.
You can’t simply throw in your documents into Redis. You have to think of the way you in which you want to store and organize your data. One example are hashes in Redis. They are quite different from “traditional”, nested objects, which means you’ll have to rethink the way you store nested documents. One solution would be to store a reference inside the hash to another hash (something like key: [id of second hash]). Another idea would be to store it as JSON, which seems counter-intuitive to most people with a *SQL-background.
If you need really high performance.
Beating the performance Redis provides is nearly impossible. Imagine you database being as fast as your cache. That’s what it feels like using Redis as a real database.
If you don’t care that much about scaling.
Scaling Redis is not as hard as it used to be. For instance, you could use a kind of proxy server in order to distribute the data among multiple Redis instances. Master-slave replication is not that complicated, but distributing you keys among multiple Redis-instances needs to be done on the application site (e.g. using a hash-function, Modulo etc.). Scaling MongoDB by comparison is much simpler.
When to use MongoDB
Prototyping, Startups, Hackathons
MongoDB is perfectly suited for rapid prototyping. Nevertheless, performance isn’t that good. Also keep in mind that you’ll most likely have to define some sort of schema in your application.
When you need to change your schema quickly.
Because there is no schema! Altering tables in traditional, relational DBMS is painfully expensive and slow. MongoDB solves this problem by not making a lot of assumptions on your underlying data. Nevertheless, it tries to optimize as far as possible without requiring you to define a schema.
官网:
https://www.mongodb.com/
下载地址:
https://www.mongodb.com/download-center#community
把安装位置加入到环境变量中,例如:
D:\Program Files\MongoDB\Server\3.4\bin
命令行键入:
C:\Users\wangs>mongo.exe --versionMongoDB shell version v3.4.6git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5OpenSSL version: OpenSSL 1.0.1u-fips 22 Sep 2016allocator: tcmallocmodules: nonebuild environment: distmod: 2008plus-ssl distarch: x86_64 target_arch: x86_64
启动mongodb服务
创建一个目录,然后设置为mongodb的data目录:
例如:D:\mongodb_data\db
命令行键入:
C:\Users\wangs>mongod.exe --dbpath d:\mongodb_data\db2017-07-13T20:10:37.851-0700 I CONTROL [initandlisten] MongoDB starting : pid=5524 port=27017 dbpath=d:\mongodb_data\db 64-bit host=LAPTOP-MNU6522J2017-07-13T20:10:37.851-0700 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R22017-07-13T20:10:37.852-0700 I CONTROL [initandlisten] db version v3.4.62017-07-13T20:10:37.853-0700 I CONTROL [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b52017-07-13T20:10:37.853-0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips 22 Sep 20162017-07-13T20:10:37.853-0700 I CONTROL [initandlisten] allocator: tcmalloc2017-07-13T20