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
        }

  });

このようなクエリを実行し、結果を PhoneBook の配列にアンマーシャリングできるように、Golang で PhoneBook 構造体をフォーマットするにはどうすればよいですか?

  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 に答える