good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
upper
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
open
upper
Commits
9e730edb
Commit
9e730edb
authored
10 years ago
by
Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
Adding comments for MongoDB example.
parent
3390592f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mongo/_example/main.go
+20
-13
20 additions, 13 deletions
mongo/_example/main.go
with
20 additions
and
13 deletions
mongo/_example/main.go
+
20
−
13
View file @
9e730edb
...
...
@@ -2,48 +2,53 @@ package main
import
(
"fmt"
"log"
"time"
"upper.io/db"
_
"upper.io/db/mongo"
"upper.io/db"
// Imports the main db package.
_
"upper.io/db/mongo"
// Imports the mongo adapter.
)
var
settings
=
db
.
Settings
{
Database
:
`upperio_tests`
,
Host
:
`127.0.0.1`
,
Database
:
`upperio_tests`
,
// Database name.
Host
:
`127.0.0.1`
,
// Using TCP.
}
type
Birthday
struct
{
Name
string
`bson:"name"`
// Maps the "Name" property to the "name" column of the "birthdays" table.
Name
string
`bson:"name"`
// Maps the "Born" property to the "born" column of the "birthdays" table.
Born
time
.
Time
`bson:"born"`
}
func
main
()
{
// Attemping to establish a connection to the database.
sess
,
err
:=
db
.
Open
(
"mongo"
,
settings
)
if
err
!=
nil
{
fmt
.
Println
(
"Unable to connect:"
,
err
.
Error
())
return
log
.
Fatalf
(
"db.Open(): %q
\n
"
,
err
)
}
// Remember to close the database session.
defer
sess
.
Close
()
// Pointing to the "birthdays" table.
birthdayCollection
,
err
:=
sess
.
Collection
(
"birthdays"
)
if
err
!=
nil
{
if
err
!=
db
.
ErrCollectionDoesNotExists
{
fmt
.
Println
(
"Could not use collection:"
,
err
.
Error
())
return
log
.
Fatalf
(
"Could not use collection: %q
\n
"
,
err
)
}
}
else
{
err
=
birthdayCollection
.
Truncate
()
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
log
.
Fatalf
(
"Truncate(): %q
\n
"
,
err
)
}
}
// Inserting some rows into the "birthdays" table.
birthdayCollection
.
Append
(
Birthday
{
Name
:
"Hayao Miyazaki"
,
Born
:
time
.
Date
(
1941
,
time
.
January
,
5
,
0
,
0
,
0
,
0
,
time
.
UTC
),
...
...
@@ -59,19 +64,21 @@ func main() {
Born
:
time
.
Date
(
1962
,
time
.
November
,
25
,
0
,
0
,
0
,
0
,
time
.
UTC
),
})
// Let's query for the results we've just inserted.
var
res
db
.
Result
res
=
birthdayCollection
.
Find
()
var
birthdays
[]
Birthday
// Query all results and fill the birthdays variable with them.
err
=
res
.
All
(
&
birthdays
)
if
err
!=
nil
{
panic
(
err
.
Error
())
return
log
.
Fatalf
(
"res.All(): %q
\n
"
,
err
)
}
// Printing to stdout.
for
_
,
birthday
:=
range
birthdays
{
fmt
.
Printf
(
"%s was born in %s.
\n
"
,
birthday
.
Name
,
birthday
.
Born
.
Format
(
"January 2, 2006"
))
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment