1

我有一个看起来像这样的 Mongo 模式:

  var phoneBookSchema = Schema({

      user_id: {
          type: Schema.Types.ObjectId,
          ref: 'User',
          index: {
              unique : true
          },
          required: true
      },

      entries: {
          type: [entry],
          default: []
      },

      matches: {
          type: [],
          default: []
      }

  });

入口文档数组如下所示:

  var entry = Schema({

        _id : false,

        phone: {
              type: String,
              index: true
        },

        name: {
              type: String
        },

        notified: {
              type: Boolean,
              default: false,
              required: true
        }

  });

如何在 Golang 中格式化 PhoneBook 结构,以便我可以运行这样的查询并将结果解组到 PhoneBooks 数组中?

  var results []PhoneBook

  err = pb.Find(bson.M{}).All(&results)
4

1 回答 1

2

我想通了,这是任何可能觉得有用的人的答案。

type PhoneBook struct {
    User_id bson.ObjectId
    Entries []Entry
    Matches []User
}

type Entry struct {
    Phone string
    Name string
    Notified bool
}

type User struct {
    User_id string
    Username string
}
于 2014-04-30T23:51:00.713 回答