当前位置:网站首页>Go operation mongodb
Go operation mongodb
2022-06-24 04:03:00 【Common_ youmen】
1 . brief introduction
mongoDB It is a popular database based on distributed file storage , It is between a relational database and a non relational database (NoSQL) Between the products , Non-relational databases are the most versatile , Most like a relational database .
2 . MongoDB Introduction and deployment
mongoDB It is a popular database based on distributed file storage , It is between a relational database and a non relational database (NoSQL) Between the products , Non-relational databases are the most versatile , Most like a relational database .
mongoDB Store a piece of data as a document (document), Data structures are defined by key values (key-value) The composition of . The documents are similar to those we usually use in programming JSON object . Field values in a document can contain other documents , Arrays and document arrays .
2.1 MongoDB Relevant concepts
mongoDB We are familiar with the concept of SQL The concept comparison is as follows :
MongoDB The term / Concept | explain | contrast SQL The term / Concept |
|---|---|---|
database | database | database |
collection | aggregate | table |
document | file | row |
field | Field | column |
index | index | Indexes |
primary key | Primary key MongoDB Automatically put _id Field sets the primary key | primary key |
2.2 MongoDB install
We download and install the community version here , Official website download address . After opening the above connection , Select the corresponding version 、 Operating system platform ( Common platforms support ) And package type , Click on Download Button to download .
It is added here that ,Windows Platform has ZIP and MSI Two bag types : * ZIP: Compressed file version * MSI: Executable version , Click on ” next step ” Can be installed .
macOS In addition to downloading from this website TGZ The documents , You can also use Homebrew install .
For more installation details, please refer to Official installation tutorial , There are Linux、macOS and Windows Installation tutorial of three major platforms .
install
# establish mongo user
useradd mongod
echo 123 |passwd --stdin mongod
# Create directory structure
mkdir -p /mongodb/{conf,log,data,bin}
# Unzip the software to the specified location
tar xf mongodb-linux-x86_64-rhel70-3.4.24.tgz -C /mongodb/
cp -a /mongodb/mongodb-linux-x86_64-rhel70-3.4.24/bin/* /mongodb/bin/
# Modify directory permissions
chown -R mongod:mongod /mongodb
# Set user environment variables
su - mongod
tail -2 .bash_profile
export PATH
export PATH=/mongodb/bin:$PATH
source .bash_profilestart-up
mongod --dbpath=/opt/data/apps/mongodb/data --logpath=/opt/data/apps/mongodb/log/mongo.log --port=27017 --logappend --fork // If there is a warning, deal with it cat >> /etc/security/limits.conf <<EOF * soft nproc 65530 * hard nproc 65530 * soft nofile 65530 * hard nofile 65530 EOF ulimit -n 65535 ulimit -u 20480 // link mongo mongo // Default connection to native test database
Profile startup
[[email protected] mongodb]$ cat /opt/data/apps/mongodb/conf/mongo.conf dbpath=/opt/data/apps/mongodb/data logpath=/opt/data/apps/mongodb/log/mongo.log logappend = true port=27017 fork=true auth=true #bind_ip = 127.0.0.1 journal=true quiet=true // start-up $ mongod -f /opt/data/apps/mongodb/conf/mongo.conf // close mongod -f /opt/data/apps/mongodb/conf/mongo.conf --shutdown // Join in systemd cat > /etc/systemd/system/mongod.service <<EOF [Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] User=mongod Type=forking ExecStart=/opt/data/apps/mongodb/bin/mongod --config /opt/data/apps/mongodb/conf/mongo.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/opt/data/apps/mongodb/bin/mongod --config /opt/data/apps/mongodb/conf/mongo.conf --shutdown PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl start mongod systemctl stop mongod systemctl restart mongod
3 . MongoDB Basic use
3.1 start-up MongoDB database
3.2 Database common commands
show dbs;: view the database
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
> use youmen # Switch to the specified database , If the database does not exist, create .
switched to db youmen
> db # Display the current database .
youmen
> db.dropDatabase() # Delete the current database
{ "ok" : 1 }3.3 Common data set commands
db.createCollection(name,options): Create a dataset
name: Dataset name
options: Optional parameters , Specify the memory size and index .
> db.createCollection("student");
{ "ok" : 1 }
show collections;: View all collections in the current database .
> show collections;
student
db.student.drop(): Delete the specified dataset
> db.student.drop()
true3.4 Document common commands
Insert a document
{
"acknowledged" : true,
"insertedId" : ObjectId("613ee40f9462ebfb9de4f671")
}Insert multiple documents
> db.student.insertMany([
... {name:" Zhang San ",age:20},
... {name:" Li Si ",age:25}
... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("613ee4349462ebfb9de4f672"),
ObjectId("613ee4349462ebfb9de4f673")
]
}Query all documents
> db.student.find();
{ "_id" : ObjectId("613ee40f9462ebfb9de4f671"), "name" : "youmen", "age" : 18 }
{ "_id" : ObjectId("613ee4349462ebfb9de4f672"), "name" : " Zhang San ", "age" : 20 }
{ "_id" : ObjectId("613ee4349462ebfb9de4f673"), "name" : " Li Si ", "age" : 25 }Inquire about age>20 Year old documents
> db.student.find(
... {age:{$gt:20}}
... )
{ "_id" : ObjectId("613ee4349462ebfb9de4f673"), "name" : " Li Si ", "age" : 25 }Update the document
> db.student.update(
... {name:"youmen"},
... {name:"wunai",age:21}
... );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("613ee40f9462ebfb9de4f671"), "name" : "wunai", "age" : 21 }
{ "_id" : ObjectId("613ee4349462ebfb9de4f672"), "name" : " Zhang San ", "age" : 20 }
{ "_id" : ObjectId("613ee4349462ebfb9de4f673"), "name" : " Li Si ", "age" : 25 }Delete the document
> db.student.deleteOne({name:" Li Si "});
{ "acknowledged" : true, "deletedCount" : 1 }
> db.student.find()
{ "_id" : ObjectId("613ee40f9462ebfb9de4f671"), "name" : "wunai", "age" : 21 }
{ "_id" : ObjectId("613ee4349462ebfb9de4f672"), "name" : " Zhang San ", "age" : 20 }There are too many orders , For more commands, see Official documents :shell command and Official documents :CRUD operation .
4 Go operation MongoDB
4.1 adopt Golang Connect MongoDB
package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Set client connection configuration
clientOptions := options.Client().ApplyURI("mongodb://1.1.1.1:27017")
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Check connections
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
}Connected to the MongoDB after , We can deal with our above youmen In the database student The data set :
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)
func main() {
clientOptions := options.Client().ApplyURI("mongodb://1.1.1.1:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
// Specify to get the data set to operate on a
connection := client.Database("youmen").Collection("student")
fmt.Println(connection)
// disconnect
err = client.Disconnect(context.TODO())
if err != nil {
log.Fatal(err)
}
fmt.Println("Connection to MongoDB closed.")
}4.2 Connection pool mode
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
)
func ConnectToDB(uri string,name string,timeout time.Duration,num uint64) (*mongo.Database,error) {
ctx,cancel := context.WithTimeout(context.Background(),timeout)
defer cancel()
o := options.Client().ApplyURI(uri)
o.SetMaxPoolSize(num)
client,err := mongo.Connect(ctx,o)
if err != nil {
return nil,err
}
return client.Database(name),nil
}
var num uint64
func main() {
//clientOptions := options.Client().ApplyURI()
num=20
clientOptions,err := ConnectToDB("mongodb://1.1.1.1:27017","youmen", time.Duration(num),20)
fmt.Println(clientOptions)
if err != nil {
log.Fatal(err)
}
err = clientOptions.Client().Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
// Specify to get the data set to operate on a
connection := clientOptions.Client().Database("youmen").Collection("student")
fmt.Println(connection)
// disconnect
err = clientOptions.Client().Disconnect(context.TODO())
if err != nil {
log.Fatal(err)
}
fmt.Println("Connection to MongoDB closed.")
}5 BSON
MongoDB Medium JSON The document is stored in a file called BSON( Binary coded JSON) In the binary representation of . And other will JSON The database where data is stored as simple strings and numbers is different ,BSON The code expands JSON Express , Make it contain additional types , Such as int、long、date、 Floating point numbers and decimal128. This makes it easier and more reliable for applications to handle 、 Sort and compare data .
Connect MongoDB Of Go There are two types of representations in the driver BSON data :D and Raw.
type D Families are used to build succinctly using local Go Type of BSON object . This is important for the construction to pass to MongoDB It's a very useful command .D There are four types of families :
D: One BSON file . This type should be used when the order is important , such as MongoDB command .
M: A disordered map. It and D It's the same , It's just that it doesn't keep order .
A: One BSON Array .
E: D One of the elements .
To use BSON, You need to import the bread first :
import "go.mongodb.org/mongo-driver/bson"
Here is a use D Examples of filter documents built by type , It can be used to find name Fields and ’ Zhang San ’ or ’ Li Si ’ Matching documents :
bson.D{{
"name",
bson.D{{
"$in",
bson.A{" Zhang San ", " Li Si "},
}},
}}Raw The type family is used to verify byte slices . You can still use it Lookup() Retrieve a single element from the original type . If you don't want to BSON The cost of deserializing to another type , So it's very useful . In this tutorial we will only use D type .
5.1 CURD
We first in Go Code defines a Student The types are as follows :
type Student struct {
Name string
Age int
}Next , To create some Student Type values , Ready to insert into the database :
s1 := Student{" Xiaohong ", 12}
s2 := Student{" Xiaolan ", 10}
s3 := Student{" Xiao Huang ", 11}Inserted into the document
Use collection.InsertOne() Method to insert a document record :
insertResult, err := collection.InsertOne(context.TODO(), s1)
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted a single document: ", insertResult.InsertedID)Use collection.InsertMany() Method to insert multiple document records :
students := []interface{}{s2, s3}
insertManyResult, err := collection.InsertMany(context.TODO(), students)
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs)Update the document
updateone() Method allows you to update individual documents . It needs a filter document to match the document in the database , And need an update document to describe the update operation . You can use bson.D Type to build filter documents and update documents :
filter := bson.D{{"name", " Xiaolan "}}
update := bson.D{
{"$inc", bson.D{
{"age", 1},
}},
}Next , You can find Xiao Lan through the following statement , Give him another year ;
updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Matched %v documents and updated %v documents.\n", updateResult.MatchedCount, updateResult.ModifiedCount)Search for documents
To find a document , You need one filter file , And a pointer that can decode the result into its value . To find a single document , Use collection.FindOne(). This method returns a result that can be decoded as a value .
We use the one defined above filter To find the name ’ Xiaolan ’ Documents .
// Create a Student Variables are used to receive the results of a query
var result Student
err = collection.FindOne(context.TODO(), filter).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found a single document: %+v\n", result)To find multiple documents , Please use collection.Find(). This method returns a cursor . Cursors provide a stream of documents , You can use it to iterate and decode a document at a time . When the cursor runs out , The cursor should be closed . The following example uses options The package sets a limit to return only two documents .
// Query multiple
// Pass options to Find()
findOptions := options.Find()
findOptions.SetLimit(2)
// Define a slice to store query results
var results []*Student
// hold bson.D{{}} As a filter To match all documents
cur, err := collection.Find(context.TODO(), bson.D{{}}, findOptions)
if err != nil {
log.Fatal(err)
}
// Find multiple documents and return a cursor
// Traversal cursors allow us to decode one document at a time
for cur.Next(context.TODO()) {
// Create a value , Decode a single document to this value
var elem Student
err := cur.Decode(&elem)
if err != nil {
log.Fatal(err)
}
results = append(results, &elem)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
// Close the cursor when finished
cur.Close(context.TODO())
fmt.Printf("Found multiple documents (array of pointers): %#v\n", results)Delete the document
Last , have access to collection.DeleteOne() or collection.DeleteMany() Delete the document . If you deliver bson.D{{}} As filter parameters , It will match all the documents in the dataset . You can also use collection. drop() Delete the entire dataset .
// Delete the one whose name is Xiao Huang
deleteResult1, err := collection.DeleteOne(context.TODO(), bson.D{{"name"," Xiao Huang "}})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult1.DeletedCount)
// Delete all
deleteResult2, err := collection.DeleteMany(context.TODO(), bson.D{{}})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult2.DeletedCount)
package main
import (
"fmt"
"reflect"
)
type person struct {
name string
age int
}
func main() {
v := reflect.ValueOf(person{"steve", 30})
count := v.NumField()
for i := 0; i < count; i++ {
f := v.Field(i)
switch f.Kind() {
case reflect.String:
fmt.Println(f.String())
case reflect.Int:
fmt.Println(f.Int())
}
}
}5.2 obtain Mongo Service status
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
serverStatus, err := client.Database("admin").RunCommand(
ctx,
bsonx.Doc{{"serverStatus", bsonx.Int32(1)}},
).DecodeBytes()
if err != nil {
fmt.Println(err)
}
fmt.Println(serverStatus)
fmt.Println(reflect.TypeOf(serverStatus))
version, err := serverStatus.LookupErr("version")
fmt.Println(version.StringValue())
if err != nil {
fmt.Println(err)
}边栏推荐
- Protect your system with fail2ban and firewalld blacklists
- Why is on-line monitoring of equipment more and more valued by people?
- openEuler社区理事长江大勇:共推欧拉开源新模式 共建开源新体系
- How to bypass CDN to get web pages? How many options are available?
- Tencent cloud console work order submission Guide
- Easyplayer consumes traffic but does not play video and reports an error libdecoder Wasm404 troubleshooting
- Web penetration test - 5. Brute force cracking vulnerability - (4) telnet password cracking
- golang clean a slice
- Pits encountered in refactoring code (1)
- Kubernetes 资源拓扑感知调度优化
猜你喜欢

Installation of pytorch in pycharm

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

SQL注入绕过安全狗思路一

web技术分享| 【地图】实现自定义的轨迹回放

开源之夏2022中选结果公示,449名高校生将投入开源项目贡献

On game safety (I)

老弹出explorer.exe遇到问题已停止工作,怎么办?

祝贺钟君成为 CHAOSS Metric Model 工作组的 Maintainer

Modstartcms enterprise content site building system (supporting laravel9) v4.2.0

Halcon knowledge: contour operator on region (2)
随机推荐
Maintain the visibility of data automation: logging, auditing and error handling of the bridge of knowledge and action
[numpy] numpy's judgment on Nan value
Easyplayer consumes traffic but does not play video and reports an error libdecoder Wasm404 troubleshooting
Configuration process of easygbs access to law enforcement recorder
How to avoid man in the middle attack (mitm)
[new light weight first purchase special] 1-core 2g5m light weight application server costs 50 yuan in the first year. It is cost-effective and helps you get on the cloud easily!
Building RPM packages - spec Basics
Life reopens simulation / synthetic big watermelon / small air conditioner Inventory of 2021 popular open source projects
黑帽实战SEO之永不被发现的劫持
共建欧拉社区 共享欧拉生态|携手麒麟软件 共创数智未来
What is FTP? What is the FTP address of the ECS?
Prometheus pushgateway
High quality travel on national day, visual start of smart Tourism
Can the video streams of devices connected to easygbs from the intranet and the public network go through their respective networks?
golang clean a slice
Several good books for learning data
openGauss 3.0版本源码编译安装指南
How to bypass CDN to get web pages? How many options are available?
Web penetration test - 5. Brute force cracking vulnerability - (1) SSH password cracking
How to do the right thing in digital marketing of consumer goods enterprises?