Example of a method that connects golang to mongoDB

  • 2020-06-19 10:28:25
  • OfStack

Mogondb does not support transactions. All the requirements of transactions should be carefully used, such as the bank transfer operation, transfer 100 million DOLLARS, because the network, power failure caused the transaction was not completed, can not be rolled back, the transaction can not be withdrawn. All use with caution!!

Application scenarios of Mogondb:

For example, an CSDN blog post, blog content, blogger, post time, comments, number of reads and other information can be stored in an ES9en-like data. If you need to store different information in different tables with mysql, you can query multiple tables or use JOIN to query data, resulting in too slow query. With MongoDB, the data is stored at the beginning of 1. When data is needed, the data can be queried once. Updating data or adding data can be directly updated or added to the original data set, which is very convenient.

MongoDB is easy to manage, updates data, and improves performance by not using JOIN for queries like traditional relational data.

Mogodb is easy to scale up and can be deployed on multiple servers. And for big data, csdn blog system, every day, every month, every year there are a large number of blogs written, data volume growth is very big every year, mongoDB can be elastic expansion without downtime,

This means adding several storage servers to form distributed storage.

MongoDB is well suited for this requirement scenario, I wonder how csdn is implemented ?

MongoDB also efficiently stores large binary objects, such as photos, music and videos, and indexes and aggregates large amounts of data in real time

mongodb stores data in a flexible document similar to json, the field structure may vary from document to document, and the data structure may change at any time

The Json model can be mapped to objects in the application code so that it is easy to use and has an orm feel.

It can be directly associated with structures in golang.

A document data structure like json is called BSON, and the most prominent advantage over json is that BSON is traversable.

MongoDB supports 2-d spatial index, using spatial index,mongoDB supports a special query, such as a map website to find the nearest coffee shop, bank and other information. This is easy to implement using mongoDB's spatial index combined with special query methods.

The location index of MongoDB can be used in location-based query scenarios such as Didi, Chahao and OFO.

golang connection mongodb

golang no official mongodb drive, but there is community driven: http: / / labix org/mgo

Step 1: Connect to mongodb


package main

import (
  "gopkg.in/mgo.v2"
  "fmt"
)
func main() {
  session, err := mgo.Dial("mongodb://123.207.215.200:27017")
  defer session.Close()
  if err !=nil {
    fmt.Println(err)
    return
  }
  names,err:=session.DatabaseNames();
  if err !=nil {
    fmt.Println(" Database name not queried :",err)
  }
  fmt.Println(names)
}

Operation results:

[

[local mytest]

]

Other url splicing rules:


mongodb://[username:password@]host1[:port1][,host2[:port2], ... [,hostN[:portN]]][/[database][?options]] 
mongodb://  This is a fixed format and must be specified. 

username: password@optional. If set, after connecting to the database server, the driver will attempt to log in to the database
host1 must specify at least one host. host1 is the only one to be filled in for this URI. It specifies the address to connect to the server. If you want to connect to a replication set, specify multiple host addresses.

portX optional specified port, if not filled, defaults to 27017

If you specify username: ES110en@, connect and verify login to the specified database. If not specified, the test database is opened by default.
The & # 63; options is the connection option. If /database is not used, the prefix/needs to be added. All join options are key-value pairs name=value, passing between the key-value pairs & Or; (semicolon) to separate

Refer to the beginner's tutorial or official documentation:
http://edu.ofstack.com/mongodb/mongodb-connections.html
https://docs.mongodb.com/manual/reference/connection-string/index.html

Additional 1:

The way the windows tool connects mongodb. If the code is connected to mongodb and there is an error, you can use the tool to check whether the service can be connected normally.
(1). Install windows mongodb, reference url: https: / / www ofstack. com article / 85605. htm

(2). Use the command line to enter the mongodb installation directory, bin directory, and execute

[

C:\Program Files\MongoDB\Server\3.6\bin > mongo 123.207.215.200:27017

]

It should be noted that there is no "mongodb://" in url.

linux's approach is similar:


root@VM-192-98-ubuntu:/usr/local/mongodb/bin# ./mongo 127.0.0.1

Related articles: