dongtuoleng8624 2014-10-17 21:08
浏览 53
已采纳

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

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:

type Test struct {
    InTest SubTest `bson:"in_test"`
}

type SubTest struct {
    Test1 string `bson:"test1"`
    Test2 string `bson:"test2"`
}

I then insert a document:

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

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

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

This prints: Found document: []

Whereas searching using both fields returns the document:

var tests []Test
err = sess.DB("test").C("test").Find(
    bson.M{"in_test": bson.M{"test1": "test", "test2": "hello"}}).All(&tests)
if err != nil {
    fmt.Printf("Got an error finding documents %+v
")
    os.Exit(1)
}
fmt.Printf("Found document: %+v
", 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:

type Test struct {
    InTest bson.M `bson:"in_test"`
}

test := Test{InTest: bson.M{"test1": "test", "test2": "hello"}}
err = col.Insert(test)
if err != nil {
    fmt.Printf("Can't insert document: %+v
", err)
    os.Exit(1)
}

var tests []Test
err = sess.DB("test").C("test").Find(
    bson.M{"in_test": bson.M{"test1": "test"}}).All(&tests)
if err != nil {
    fmt.Printf("Got an error finding documents %+v
")
    os.Exit(1)
}
fmt.Printf("Found document: %+v
", 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条回答 默认 最新

  • doutan8775 2014-10-17 22:00
    关注

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

    func main() {
        sess, err := mgo.Dial("localhost")
        if err != nil {
            fmt.Printf("Can't connect to mongo, go error %v
    ", err)
            os.Exit(1)
        }
        col := sess.DB("test").C("test")
        test := Test{InTest: SubTest{Test1: "test", Test2: "hello"}}
        err = col.Insert(test)
        if err != nil {
            fmt.Printf("Can't insert document: %+v
    ", err)
            os.Exit(1)
        }
        var tests []Test
        err = col.Find(bson.M{"in_test.test2": "hello"}).All(&tests)
        fmt.Println(tests)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能