mgo,mongodb:查找与嵌入结构中的一个字段匹配的文档。

huangapple go评论112阅读模式
英文:

mgo, mongodb: Finding documents that match one field from embedded struct

问题

问题的简化示例

你好,

我正在使用mgo将文档插入到MongoDB中,我尝试在另一个文档中嵌入一个文档。

使用mgo,我使用了两个结构体:

  1. type Test struct {
  2. InTest SubTest `bson:"in_test"`
  3. }
  4. type SubTest struct {
  5. Test1 string `bson:"test1"`
  6. Test2 string `bson:"test2"`
  7. }

然后我插入一个文档:

  1. test := Test{InTest: SubTest{Test1: "test", Test2: "hello"}}
  2. err = col.Insert(test)
  3. if err != nil {
  4. fmt.Printf("无法插入文档:%+v\n", err)
  5. os.Exit(1)
  6. }

现在我想根据嵌入文档中的字段来查找这个文档:

  1. var tests []Test
  2. err = sess.DB("test").C("test").Find(
  3. bson.M{"in_test": bson.M{"test1": "test"}}).All(&tests)
  4. if err != nil {
  5. fmt.Printf("查找文档时出错:%+v\n")
  6. os.Exit(1)
  7. }
  8. fmt.Printf("找到文档:%+v\n", tests)

这将打印:找到文档:[]

而使用两个字段进行搜索将返回文档:

  1. var tests []Test
  2. err = sess.DB("test").C("test").Find(
  3. bson.M{"in_test": bson.M{"test1": "test", "test2": "hello"}}).All(&tests)
  4. if err != nil {
  5. fmt.Printf("查找文档时出错:%+v\n")
  6. os.Exit(1)
  7. }
  8. fmt.Printf("找到文档:%+v\n", tests)

这将打印:找到文档:[{InTest:{Test1:test Test2:hello}}]

我尝试过以bson.M格式插入文档,但结果相同:

  1. type Test struct {
  2. InTest bson.M `bson:"in_test"`
  3. }
  4. test := Test{InTest: bson.M{"test1": "test", "test2": "hello"}}
  5. err = col.Insert(test)
  6. if err != nil {
  7. fmt.Printf("无法插入文档:%+v\n", err)
  8. os.Exit(1)
  9. }
  10. var tests []Test
  11. err = sess.DB("test").C("test").Find(
  12. bson.M{"in_test": bson.M{"test1": "test"}}).All(&tests)
  13. if err != nil {
  14. fmt.Printf("查找文档时出错:%+v\n")
  15. os.Exit(1)
  16. }
  17. fmt.Printf("找到文档:%+v\n", tests)

再次打印:找到文档:[] 或者 找到文档:[{InTest:map[test1:test test2:hello]}](如果搜索两个字段)

如何查找与嵌入结构/文档中的一个字段匹配的文档?

提前感谢!

英文:

SIMPLIFIED EXAMPLE OF ISSUE

Hi,

Using mgo to insert documents into mongodb, I'm trying to embed a document within another.

With mgo I'm using two structs for this like this:

  1. type Test struct {
  2. InTest SubTest `bson:"in_test"`
  3. }
  4. type SubTest struct {
  5. Test1 string `bson:"test1"`
  6. Test2 string `bson:"test2"`
  7. }

I then insert a document:

  1. test := Test{InTest: SubTest{Test1: "test", Test2: "hello"}}
  2. err = col.Insert(test)
  3. if err != nil {
  4. fmt.Printf("Can't insert document: %+v\n", err)
  5. os.Exit(1)
  6. }

Now I'd like to find this document based on a field in the embedded document:

  1. var tests []Test
  2. err = sess.DB("test ").C("test").Find(
  3. bson.M{"in_test": bson.M{"test1": "test"}}).All(&tests)
  4. if err != nil {
  5. fmt.Printf("Got an error finding documents %+v\n")
  6. os.Exit(1)
  7. }
  8. fmt.Printf("Found document: %+v\n", tests)

This prints: Found document: []

Whereas searching using both fields returns the document:

  1. var tests []Test
  2. err = sess.DB("test").C("test").Find(
  3. bson.M{"in_test": bson.M{"test1": "test", "test2": "hello"}}).All(&tests)
  4. if err != nil {
  5. fmt.Printf("Got an error finding documents %+v\n")
  6. os.Exit(1)
  7. }
  8. fmt.Printf("Found document: %+v\n", tests)

This prints: Found document: [{InTest:{Test1:test Test2:hello}}]

I've tried inserting the document in bson.M format as well but with the same results:

  1. type Test struct {
  2. InTest bson.M `bson:"in_test"`
  3. }
  4. test := Test{InTest: bson.M{"test1": "test", "test2": "hello"}}
  5. err = col.Insert(test)
  6. if err != nil {
  7. fmt.Printf("Can't insert document: %+v\n", err)
  8. os.Exit(1)
  9. }
  10. var tests []Test
  11. err = sess.DB("test").C("test").Find(
  12. bson.M{"in_test": bson.M{"test1": "test"}}).All(&tests)
  13. if err != nil {
  14. fmt.Printf("Got an error finding documents %+v\n")
  15. os.Exit(1)
  16. }
  17. fmt.Printf("Found document: %+v\n", tests)

Again printing: Found document: []
or Found document: [{InTest:map[test1:test test2:hello]}] if searching both fields

How do I find a document matching ONE field in an embedded struct/document?

Thanks in advance!

答案1

得分: 2

你的初始问题有误导性,你需要匹配子文档:

  1. func main() {
  2. sess, err := mgo.Dial("localhost")
  3. if err != nil {
  4. fmt.Printf("无法连接到MongoDB,错误:%v\n", err)
  5. os.Exit(1)
  6. }
  7. col := sess.DB("test").C("test")
  8. test := Test{InTest: SubTest{Test1: "test", Test2: "hello"}}
  9. err = col.Insert(test)
  10. if err != nil {
  11. fmt.Printf("无法插入文档:%+v\n", err)
  12. os.Exit(1)
  13. }
  14. var tests []Test
  15. err = col.Find(bson.M{"in_test.test2": "hello"}).All(&tests)
  16. fmt.Println(tests)
  17. }

请注意,这只是代码的翻译部分,不包括其他内容。

英文:

Your initial question was misleading, you need to match the subdocument:

  1. func main() {
  2. sess, err := mgo.Dial("localhost")
  3. if err != nil {
  4. fmt.Printf("Can't connect to mongo, go error %v\n", err)
  5. os.Exit(1)
  6. }
  7. col := sess.DB("test").C("test")
  8. test := Test{InTest: SubTest{Test1: "test", Test2: "hello"}}
  9. err = col.Insert(test)
  10. if err != nil {
  11. fmt.Printf("Can't insert document: %+v\n", err)
  12. os.Exit(1)
  13. }
  14. var tests []Test
  15. err = col.Find(bson.M{"in_test.test2": "hello"}).All(&tests)
  16. fmt.Println(tests)
  17. }

huangapple
  • 本文由 发表于 2014年10月18日 05:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/26433463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定