MongoDB Go Driver tutorial

 3r33535. 3r3-31. 3r33535. Good news! Official go driver for mongoDB released in beta. After some thought, I decided to translate 3r335. Article 3-33547. from the official mongoDB site instead of writing the material yourself. Here is what will happen in this tutorial: 3r3-3550. 3r33548.  3r33535.
Installing mongo-go-driver
Connect to mongoDB using mongo-go-driver
Using BSON facilities
Using CRUD methods
 3r33535. MongoDB Go Driver tutorial Lookup () 3r33547. . 3r33550. 3r33548.  3r33535. 3r33535. This can be useful when you need to get rid of the extra load when converting BSON to another type. In this tutorial only the family of types D will be used. 3r33550. 3r33548.  3r33535.
Using CRUD methods 3r33528. 3r33548.  3r33535. 3r33535. After successfully connecting to the database, we can begin to add and modify data in our collection. Type 3r33512. Collection
contains methods that allow you to send requests to the database. 3r33550. 3r33548.  3r33535. 3r33300. Insert (create) documents 3r3502. 3r33548.  3r33535. 3r33535. First you need to create a few new structures 3r3512. Trainer
for insertion into the database: 3r33550. 3r33548.  3r33535. 3r? 3517. 3r33518. ash: = Trainer {"Ash", 1? "Pallet Town"}
misty: = Trainer {"Misty", 1? "Cerulean City"}
brock: = Trainer {"Brock", 1? "Pewter City"} 3r33524. 3r33548.  3r33535. 3r33535. To add a single document, use the method 3r33512. collection.InsertOne () 3r3353513. : 3r350 3r33548.  3r33535. 3r? 3517. 3r33518. insertResult, err: = collection.InsertOne (context.TODO (), ash)
if err! = nil {3r3353564. log.Fatal (err) 3r3353564.} 3r33535. 3r33535. fmt.Println ("Inserted a single document:", insertResult.InsertedID) 3r33524. 3r33548.  3r33535. 3r33535. To insert multiple documents at the same time there is a method 3r33512. collection.InsertMany () 3r3353513. : 3r350 3r33548.  3r33535. 3r? 3517. 3r33518. trainers: =[]interface {} {misty, brock} 3r3353564. 3r33535. insertManyResult, err: = collection.InsertMany (context.TODO (), trainers)
if err! = nil {3r3353564. log.Fatal (err) 3r3353564.} 3r33535. 3r33535. fmt.Println ("Inserted multiple documents:", insertManyResult.InsertedIDs) 3r3353523. 3r33524. 3r33548.  3r33535. 3r33354. Updating documents 3r3502. 3r33548.  3r33535. 3r33535. Method 3r33512. collection.UpdateOne () 3r3353513. allows you to update a single document. You need to create a filter to search for a document in the database and a document for the update operation. You can create them using types
: 3r350 3r33548.  3r33535. 3r? 3517. 3r33518. filter: = bson.D {{"name", "Ash"}}
3r33535. update: = bson.D {
{"$ inc", bson.D {3r3353564. {"age", 1}, 3r33535.}, 3r36464.} 3r33535. 3r33524. 3r33548.  3r33535. 3r33535. The following code will find the document in which the field is 3r33535. name
matches the value of 3r33512. Ash
and increase the value of 3r?312. age
by 1. 3r???. 3r33548.  3r33535. 3r? 3517. 3r33518. updateResult, err: = collection.UpdateOne (context.TODO (), filter, update)
if err! = nil {3r3353564. log.Fatal (err) 3r3353564.} 3r33535. 3r33535. fmt.Printf ("Matched% v documents and updated% v documents.n", updateResult.MatchedCount, updateResult.ModifiedCount) 3r33524. 3r33548.  3r33535. 3r33399. Search for documents 3r3502. 3r33548.  3r33535. 3r33535. To find a document, you will need a filter, as well as a pointer to a variable into which the result can be decoded. 3r33550. 3r33548.  3r33535. 3r33535. To find a single document, use r3r3512. collection.FindOne () 3r3353513. . This method returns a single value that can be decoded into a variable. 3r33550. 3r33548.  3r33535. 3r33535. We will use the same filter variable that was used in the update request. 3r33550. 3r33548.  3r33535. 3r? 3517. 3r33518. //create a value of
var result Trainer
3r33535. err = collection.FindOne (context.TODO (), filter) .Decode (& result) 3r36464. if err! = nil {3r3353564. log.Fatal (err) 3r3353564.} 3r33535. 3r33535. fmt.Printf ("Found a single document:% + vn", result) 3r3523. 3r33524. 3r33548.  3r33535. 3r33535. To find multiple documents, use r3r3512. collection.Find () 3r33513. . 3r33550. 3r33548.  3r33535. 3r33535. This method returns 3r33512. Cursor
. 3r33512. Cursor
provides a stream of documents with which you can loop through and decode one document at a time. 3r33550. 3r33548.  3r33535. 3r33535. When documents in 3r33512. Cursor
exhausted, should be closed
. Also 3r???. Cursor
You can fine-tune using the package
. 3r33550. 3r33548.  3r33535. 3r33535. In our example, we set a limit on the issuance of two documents. 3r33550. 3r33548.  3r33535. 3r? 3517. 3r33518. //Pass these options to the Find method
options: = options.Find ()
options.SetLimit (2) 3r3353564. 3r33535. //Decorated documents
var results[]* Trainer
3r33535. //Passing the filter
cur, err: = collection.Find (context.TODO (), nil, options)
if err! = nil {3r3353564. log.Fatal (err) 3r3353564.} 3r33535. 3r33535. //Finding multiple documents returns a cursor
//Iterating through the cursor for 3d3r3564. for cur.Next (context.TODO ()) {3r3353564. 3r33535. //create a value into
var elem Trainer
err: = cur.Decode (& elem)
if err! = nil {3r3353564. log.Fatal (err) 3r3353564.} 3r33535. 3r33535. results = append (results, & elem) 3r33535.} 3r33535. 3r33535. if err: = cur.Err (); err! = nil {
log.Fatal (err) 3r3353564.} 3r33535. 3r33535. //Close the cursor once finished
cur.Close (context.TODO ()) 3r3353564. 3r33535. fmt.Printf ("Found multiple documents (array of pointers):% + vn", results) 3r32323. 3r33524. 3r33548.  3r33535. 3r3501. Deleting documents 3r3502. 3r33548.  3r33535. 3r33535. You can delete documents using 3r33535. collection.DeleteOne () 3r3353513. or 3r33512. collection.DeleteMany () 3r3353513. . 3r33548.  3r33535. We pass nil as a filter argument that will match all the documents in the collection. You can also use 3r33512. collection.Drop () 3r31313. to remove the entire collection. 3r33550. 3r33548.  3r33535. 3r? 3517. 3r33518. deleteResult, err: = collection.DeleteMany (context.TODO (), nil)
if err: = cur.Err (); err! = nil {
log.Fatal (err) 3r3353564.} 3r33535. fmt.Printf ("Deleted% v documents in the trainers collectionn", deleteResult.DeletedCount) 3r33523. 3r33524. 3r33548.  3r33535. 3r33535. Further steps
3r33548.  3r33535. 3r33535. → The final code for this tutorial is in 3r33532. 3g3r3547 repositories for GitHub. 3r33548.  3r33535. → Documentation for the driver 3r33536. Available in GoDoc
3r33550. 3r33548.  3r33535. 3r33535. If you have any questions, please contact us in group 3r33542. Google mongo-go-driver
. 3r33548.  3r33535. Please submit bug reports in 3r33546. MongoDB JIRA
. 3r33548.  3r33535. We will be happy to receive your feedback on Go Driver. 3r33550. 3r? 3551. 3r33552. 3r33560. 3r33535. 3r33535. 3r33535. 3r33557. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//"""_mediator") () (); 3r33558. 3r33535. 3r33560. 3r33535. 3r33535. 3r33535. 3r33535.
+ 0 -

Add comment