英文:
Reading documents from MongoDB with GoLang
问题
我能够读写数据库中的文档,但我遇到了一些非常奇怪的行为。
如果搜索名字为"jack",我会得到所有的文档,而如果搜索名字为"Mike",我只会得到一个文档,但实际上有两个。
搜索方法:
func findByFirstName(name string) {
fmt.Println("looking for - ", name)
client, ctx, _ := getConnection()
quickstart := client.Database("ginPlay")
namesCollection := quickstart.Collection("names")
var p []person
nameObject, err := namesCollection.Find(ctx, bson.M{"lastName": bson.D{
{"$gt", name},
}})
if err = nameObject.All(ctx, &p); err != nil {
panic(err)
}
for i := 0; i < len(p); i++ {
fmt.Println(p[i])
}
}
输出结果:
looking for - Jack
Connected to MongoDB!
{ObjectID("613b706bf47bc212c3928870") Mike Smith 25 Netflix}
{ObjectID("613b706cf47bc212c3928872") Jack Jill 36 Surfing}
{ObjectID("613b706ef47bc212c3928874") Jack Jill 25 Hiking}
{ObjectID("613b706ff47bc212c3928876") Jack Jill 30 Snowboarding}
looking for - Mike
Connected to MongoDB!
{ObjectID("613b706bf47bc212c3928870") Mike Smith 25 Netflix}
数据库中的内容如下:
r.GET("/addPerson", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
p1 := person{Name: "Mike", LastName: "Foo", Age: 36, Hobbies: "Development"}
p2 := person{Name: "Mike", LastName: "Smith", Age: 25, Hobbies: "Netflix"}
p3 := person{Name: "Jack", LastName: "Jill", Age: 36, Hobbies: "Surfing"}
p4 := person{Name: "Jack", LastName: "Jill", Age: 25, Hobbies: "Hiking"}
p5 := person{Name: "Jack", LastName: "Jill", Age: 30, Hobbies: "Snowboarding"}
writePerson(p1)
writePerson(p2)
writePerson(p3)
writePerson(p4)
writePerson(p5)
})
数据库连接:
// GetConnection - 获取到 DocumentDB 的客户端
func getConnection() (*mongo.Client, context.Context, context.CancelFunc) {
username := "foo"
password := "pass"
clusterEndpoint := "cluster0.ml6z7m.mongodb.net/db?retryWrites=true&w=majority"
connectionURI := fmt.Sprintf(connectionStringTemplate, username, password, clusterEndpoint)
client, err := mongo.NewClient(options.Client().ApplyURI(connectionURI))
if err != nil {
log.Printf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), connectTimeout*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Printf("Failed to connect to cluster: %v", err)
}
// 强制连接以验证连接字符串
err = client.Ping(ctx, nil)
if err != nil {
log.Printf("Failed to ping cluster: %v", err)
}
fmt.Println("Connected to MongoDB!")
return client, ctx, cancel
}
控制器:
r.GET("/mike", func(c *gin.Context) {
c.String(http.StatusOK, "Mike")
findByFirstName("Mike")
})
r.GET("/jack", func(c *gin.Context) {
c.String(http.StatusOK, "Jack")
findByFirstName("Jack")
})
person 结构体:
type person struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Name string `bson:"name,omitempty"`
LastName string `bson:"lastName,omitempty"`
Age int `bson:"age,omitempty"`
Hobbies string `bson:"hobbies,omitempty"`
}
英文:
I am able to read and write documents to database but I am experiencing some very strange behavior.
If search for the name "jack" I get every document and Mike I get just one document but they're two.
search method
func findByFirstName(name string) {
fmt.Println("looking for - ", name)
client, ctx, _ := getConnection()
quickstart := client.Database("ginPlay")
namesCollection := quickstart.Collection("names")
var p []person
nameObject, err := namesCollection.Find(ctx, bson.M {"lastName": bson.D {
{"$gt", name},
}})
if err = nameObject.All(ctx, &p); err != nil {
panic(err)
}
for i := 0; i < len(p); i++ {
fmt.Println(p[i])
}
}
output
looking for - Jack
Connected to MongoDB!
{ObjectID("613b706bf47bc212c3928870") Mike Smith 25 Netflix}
{ObjectID("613b706cf47bc212c3928872") Jack Jill 36 Surfing}
{ObjectID("613b706ef47bc212c3928874") Jack Jill 25 Hiking}
{ObjectID("613b706ff47bc212c3928876") Jack Jill 30 Snowboarding}
looking for - Mike
Connected to MongoDB!
{ObjectID("613b706bf47bc212c3928870") Mike Smith 25 Netflix}
this is what is in the database
r.GET("/addPerson", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
p1 := person{Name: "Mike", LastName: "Foo", Age: 36, Hobbies: "Development"}
p2 := person{Name: "Mike", LastName: "Smith", Age: 25, Hobbies: "Netflix"}
p3 := person{Name: "Jack", LastName: "Jill", Age: 36, Hobbies: "Surfing"}
p4 := person{Name: "Jack", LastName: "Jill", Age: 25, Hobbies: "Hiking"}
p5 := person{Name: "Jack", LastName: "Jill", Age: 30, Hobbies: "Snowboarding"}
writePerson(p1)
writePerson(p2)
writePerson(p3)
writePerson(p4)
writePerson(p5)
})
database connection
// GetConnection - Retrieves a client to the DocumentDB
func getConnection() (*mongo.Client, context.Context, context.CancelFunc) {
username := "foo"
password := "pass"
clusterEndpoint := "cluster0.ml6z7m.mongodb.net/db?retryWrites=true&w=majority"
connectionURI := fmt.Sprintf(connectionStringTemplate, username, password, clusterEndpoint)
client, err := mongo.NewClient(options.Client().ApplyURI(connectionURI))
if err != nil {
log.Printf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), connectTimeout * time.Second)
err = client.Connect(ctx)
if err != nil {
log.Printf("Failed to connect to cluster: %v", err)
}
// Force a connection to verify our connection string
err = client.Ping(ctx, nil)
if err != nil {
log.Printf("Failed to ping cluster: %v", err)
}
fmt.Println("Connected to MongoDB!")
return client, ctx, cancel
}
controller
r.GET("/mike", func(c *gin.Context) {
c.String(http.StatusOK, "Mike")
findByFirstName("Mike")
})
r.GET("/jack", func(c *gin.Context) {
c.String(http.StatusOK, "Jack")
findByFirstName("Jack")
})
type person struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Name string `bson:"name,omitempty"`
LastName string `bson:"lastName,omitempty"`
Age int `bson:"age,omitempty"`
Hobbies string `bson:"hobbies,omitempty"`
}
答案1
得分: 1
findByFirstName()
函数在给定的代码中通过lastName
进行搜索,并对该字段执行大于($gt
)操作,与给定的name
进行比较:
nameObject, err := namesCollection.Find(ctx, bson.M {"lastName": bson.D {
{"$gt", name},
}})
你可能犯了一个错误,想要将"lastName"
更改为"name"
。
只有Mike Smith
被找到,因为Smith
大于Mike
(Mongo进行二进制字符串比较-参见这里)。同样的原因,即使对于Jack
,输出也不正确;它还包括Mike Smith
,因为Smith
和Jill
都大于Jack
,但不大于Foo
。
英文:
Function findByFirstName()
is searching by lastName
and performs greater-than ($gt
) operation on that field and given name
in posted code:
nameObject, err := namesCollection.Find(ctx, bson.M {"lastName": bson.D {
{"$gt", name},
}})
You probably made a mistake and want to change "lastName"
to "name"
.
Only Mike Smith
is found because Smith
> Mike
(Mongo does binary string comparison - see here). Also, even for Jack
output is not correct for the very same reason; it includes also Mike Smith
as both Smith
and Jill
are > than Jack
but not Foo
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论