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
3b492959
Commit
3b492959
authored
10 years ago
by
José Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
MongoDB: Adding placeholders for group by support. Currently not implemented.
parent
34385a3f
No related branches found
No related tags found
Loading
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
mongo/collection.go
+1
-0
1 addition, 0 deletions
mongo/collection.go
mongo/database_test.go
+63
-0
63 additions, 0 deletions
mongo/database_test.go
mongo/result.go
+11
-0
11 additions, 0 deletions
mongo/result.go
with
75 additions
and
0 deletions
mongo/collection.go
+
1
−
0
View file @
3b492959
...
...
@@ -45,6 +45,7 @@ type chunks struct {
Offset
int
Sort
[]
string
Conditions
interface
{}
GroupBy
[]
interface
{}
}
func
(
self
*
Collection
)
Find
(
terms
...
interface
{})
db
.
Result
{
...
...
This diff is collapsed.
Click to expand it.
mongo/database_test.go
+
63
−
0
View file @
3b492959
...
...
@@ -24,6 +24,7 @@
package
mongo
import
(
"math/rand"
"os"
"reflect"
"strings"
...
...
@@ -267,6 +268,68 @@ func TestResultCount(t *testing.T) {
}
func
TestGroup
(
t
*
testing
.
T
)
{
var
err
error
var
sess
db
.
Database
var
stats
db
.
Collection
if
sess
,
err
=
db
.
Open
(
Adapter
,
settings
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
type
stats_t
struct
{
Numeric
int
`db:"numeric" bson:"numeric"`
Value
int
`db:"value" bson:"value"`
}
defer
sess
.
Close
()
if
stats
,
err
=
sess
.
Collection
(
"stats_test"
);
err
!=
nil
{
if
err
!=
db
.
ErrCollectionDoesNotExist
{
t
.
Fatal
(
err
)
}
}
// Truncating table.
if
err
==
nil
{
if
err
=
stats
.
Truncate
();
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
// Adding row append.
for
i
:=
0
;
i
<
1000
;
i
++
{
numeric
,
value
:=
rand
.
Intn
(
10
),
rand
.
Intn
(
100
)
if
_
,
err
=
stats
.
Append
(
stats_t
{
numeric
,
value
});
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
// db.stats_test.group({key: {numeric: true}, initial: {sum: 0}, reduce: function(doc, prev) { prev.sum += 1}});
// Testing GROUP BY
res
:=
stats
.
Find
()
.
Group
(
bson
.
M
{
"key"
:
bson
.
M
{
"numeric"
:
true
},
"initial"
:
bson
.
M
{
"sum"
:
0
},
"reduce"
:
`function(doc, prev) { prev.sum += 1}`
,
})
var
results
[]
map
[
string
]
interface
{}
err
=
res
.
All
(
&
results
)
// Currently not supported.
if
err
!=
db
.
ErrUnsupported
{
t
.
Fatal
(
err
)
}
//if len(results) != 10 {
// t.Fatalf(`Expecting exactly 10 results, this could fail, but it's very unlikely to happen.`)
//}
}
// This test uses and result and tries to fetch items one by one.
func
TestResultFetch
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
mongo/result.go
+
11
−
0
View file @
3b492959
...
...
@@ -116,6 +116,13 @@ func (self *Result) All(dst interface{}) error {
return
nil
}
// Used to group results that have the same value in the same column or
// columns.
func
(
self
*
Result
)
Group
(
fields
...
interface
{})
db
.
Result
{
self
.
queryChunks
.
GroupBy
=
fields
return
self
}
// Fetches only one result from the resultset.
func
(
self
*
Result
)
One
(
dst
interface
{})
error
{
var
err
error
...
...
@@ -187,6 +194,10 @@ func (self *Result) query() (*mgo.Query, error) {
q
:=
self
.
c
.
collection
.
Find
(
self
.
queryChunks
.
Conditions
)
if
self
.
queryChunks
.
GroupBy
!=
nil
{
return
nil
,
db
.
ErrUnsupported
}
if
self
.
queryChunks
.
Offset
>
0
{
q
=
q
.
Skip
(
self
.
queryChunks
.
Offset
)
}
...
...
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