mikejs / gomongo

Go driver for MongoDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with FindAll and Query

kamilc opened this issue · comments

I've got a problem querying db
with FindAll and Query.

FindAll and Query ( which is
also fired by FindAll ) returns
cursor which GetNext method
returns always nil BSON ( but
if e.g you have 10 records in db
it will return 10 nil BSONs ).

After checking tests for gomongo
I've noticed that it doesn't test
all values returned by FindAll.

I'm willing to help and contribute here
but I simple don't know where to
start, as the reason for such behaviour
on my machine could be e.g different
version of mongodb or something..

I've installed gomongo with goinstall
today and my mongodb version is 1.6.5

This code:

// database is a wrapper package

db := database.GetDb()
coll := db.GetCollection("Keyword")

q, error := mongo.Marshal(map[string]string{})

if error != nil {
fmt.Println(error.String())
}

ret, error := coll.FindAll(q) //, 0, 10)'

if error != nil {
fmt.Println(error.String())
}

bson, error := ret.GetNext()

if error != nil {
fmt.Println(error.String())
}

fmt.Println("", bson.Get("Name").String())
fmt.Println(Formatter(bson))

Produces this output:
null
null

And I've checked in mongo client console
that db physically has proper values saved...

commented

Hi kamilc, It's almost your code, but it works for me.

show collections
Keyword
keyword
system.indexes
db.Keyword.find()
{ "_id" : ObjectId("4d42667f9bf83aca64d864d9"), "Name" : "Upper case names" }
db.keyword.find()
{ "_id" : ObjectId("4d4266629bf83aca64d864d7"), "name" : "Lower case names" }

package main

import (
"fmt"
"github.com/mikejs/gomongo/mongo"
)

func main() {
dbc, _ := mongo.Connect("127.0.0.1")
db := dbc.GetDB("test")
coll := db.GetCollection("keyword")

q, _:= mongo.Marshal(map[string]string{})
ret, _  := coll.FindAll(q) //, 0, 10)'
bson, _ := ret.GetNext()

fmt.Println(bson.Get("name").String())

coll = db.GetCollection("Keyword")
q, _= mongo.Marshal(map[string]string{})
ret, _  = coll.FindAll(q) //, 0, 10)'
bson, _ = ret.GetNext()

fmt.Println(bson.Get("Name").String())

}

dmitrys@laptop:/tmp$ 8g main.go && 8l main.8 && ./8.out
Lower case names
Upper case names
dmitrys@laptop:
/tmp$

deletus: That was it:)

While in struct we have uppercased attributes
the underlying bson has them in lowercase.

I just forgot about it.