Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use golang driver in mongodb

Shulou Source: shulou.com Published: 2022-05-31 22:51:59 10月01日 Update

How to use the golang driver in mongodb? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The tutorials are as follows:

Import

Go get github.com/mongodb/mongo-go-driver/mongo

Link mongo service

If client, err = mongo.Connect (getContext (), url); err! = nil {checkErr (err)}

Determine whether the service is available

If err = client.Ping (getContext (), readpref.Primary ()); err! = nil {checkErr (err)}

Select databases and collections

Collection = client.Database ("testing_base") .Collection ("howie")

Delete this collection

Collection.Drop (getContext ())

Insert a piece of data

If insertOneRes, err = collection.InsertOne (getContext (), howieArray [0]); err! = nil {checkErr (err)} fmt.Printf ("InsertOne inserted message ID:%v\ n", insertOneRes.InsertedID)

Insert data in bulk

If insertManyRes, err = collection.InsertMany (getContext (), howieArray); err! = nil {checkErr (err)} fmt.Printf ("InsertMany inserted message ID:%v\ n", insertManyRes.InsertedIDs)

Query single piece of data

If err = collection.FindOne (getContext (), bson.D {{"name", "howie_2"}, {"age", 11}}) .Decode (& howie); err! = nil {checkErr (err)} fmt.Printf ("FindOne queried data:% v\ n", howie)

Delete a single piece of data after querying it

If err = collection.FindOneAndDelete (getContext (), bson.D {{"name", "howie_3"}}) .Decode (& howie); err! = nil {checkErr (err)} fmt.Printf ("FindOneAndDelete queried data:% v\ n", howie)

Modify a single piece of data after asking for it

If err = collection.FindOneAndUpdate (getContext (), bson.D {{"name", "howie_4"}}, bson.M {"$set": bson.M {"name": "I need to modify this data"}) .Decode (& howie); err! = nil {checkErr (err)} fmt.Printf ("data queried by FindOneAndUpdate:% v\ n", howie)

Replace a single piece of data after querying it (all previous data is emptied)

If err = collection.FindOneAndReplace (getContext (), bson.D {{"name", "howie_5"}}, bson.M {"hero": "I replaced this data"}) .Decode (& howie); err! = nil {checkErr (err)} fmt.Printf ("data queried by FindOneAndReplace:% v\ n", howie)

Query more than one piece of data at a time (query createtime > = 3, limit to 2 items, createtime sorted from largest to smallest)

If cursor, err = collection.Find (getContext (), bson.M {"createtime": bson.M {"$gte": 2}}, options.Find (). SetLimit (2), options.Find (). SetSort (bson.M {"createtime":-1})); err! = nil {checkErr (err)} if err = cursor.Err () Err! = nil {checkErr (err)} defer cursor.Close (context.Background ()) for cursor.Next (context.Background ()) {if err = cursor.Decode (& howie); err! = nil {checkErr (err)} howieArrayEmpty = append (howieArrayEmpty, howie)} fmt.Printf ("Find queried data:% v\ n", howieArrayEmpty)

Query how much data is in the collection

If size, err = collection.Count (getContext (), nil); err! = nil {checkErr (err)} fmt.Printf ("how many pieces of data are in Count:% d\ n", size)

Query how much data is in the collection (query createtime > = 3 data)

If size, err = collection.Count (getContext (), bson.M {"createtime": bson.M {"$gte": 3}}); err! = nil {checkErr (err)} fmt.Printf ("how many data are there in Count:% d\ n", size)

Modify a piece of data

If updateRes, err = collection.UpdateOne (getContext (), bson.M {"name": "howie_2"}, bson.M {"$set": bson.M {"name": "I'm going to change his name"}}); err! = nil {checkErr (err)} fmt.Printf ("UpdateOne data:% d\ n", updateRes)

Modify multiple pieces of data

If updateRes, err = collection.UpdateMany (getContext (), bson.M {"createtime": bson.M {"$gte": 3}}, bson.M {"$set": bson.M {"name": "I want to change his name in batches"}}); err! = nil {checkErr (err)} fmt.Printf ("UpdateMany data:% d\ n", updateRes)

Delete a piece of data

If delRes, err = collection.DeleteOne (getContext (), bson.M {"name": "howie_1"}); err! = nil {checkErr (err)} fmt.Printf ("how many pieces of data were deleted by DeleteOne:% d\ n", delRes.DeletedCount)

Delete multiple pieces of data

If delRes, err = collection.DeleteMany (getContext (), bson.M {"createtime": bson.M {"$gte": 7}}); err! = nil {checkErr (err)} fmt.Printf ("how many pieces of data DeleteMany deleted:% d\ n", delRes.DeletedCount) after reading the above, have you mastered how to use golang driver in mongodb? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Data query multiple driver content name I want method more message problem service helpless for this reason skills tutorials databases articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Redmi MariaDB macOS