func GetRandomKeywords(howMany int) ([]Keyword, error) { sess, d, err := db.GetFactory().GetSession() if err != nil { return nil, fmt.Errorf("couldn't get session: %v", err) } defer sess.Close() total, err := d.C(KEYWORD_COLLECTION).Count() if err != nil { return nil, fmt.Errorf("error counting keywords: %v", err) } toRet := []Keyword{} for i := 0; i < howMany; i++ { toSkip := rand.Int() % total out := &Keyword{} err = d.C(KEYWORD_COLLECTION).Find(bson.M{}).Select(bson.M{"kw": 1}). Skip(toSkip).One(out) if err != nil { return nil, fmt.Errorf("error finding keyword: %v", err) } toRet = append(toRet, *out) } return toRet, nil }
func (self *Keyword) Insert() error { sess, d, err := db.GetFactory().GetSession() if err != nil { return fmt.Errorf("couldn't get session: %v", err) } defer sess.Close() return d.C(KEYWORD_COLLECTION).Insert(self) }
func FindKeyword(word string) (*Keyword, error) { sess, d, err := db.GetFactory().GetSession() if err != nil { return nil, fmt.Errorf("couldn't get session: %v", err) } defer sess.Close() keyword := &Keyword{} err = d.C(KEYWORD_COLLECTION).Find(bson.M{"kw": word}).One(keyword) return keyword, err }
func FindMovie(title string) (*Movie, error) { sess, d, err := db.GetFactory().GetSession() if err != nil { return nil, fmt.Errorf("couldn't get session: %v", err) } defer sess.Close() movie := &Movie{} err = d.C(MOVIE_COLLECTION).Find( bson.M{ "t": title, }, ).One(movie) return movie, err }
func FindKeywords(words []string) ([]Keyword, error) { sess, d, err := db.GetFactory().GetSession() if err != nil { return nil, fmt.Errorf("couldn't get session: %v", err) } defer sess.Close() keywords := []Keyword{} err = d.C(KEYWORD_COLLECTION).Find( bson.M{ "kw": bson.M{ "$in": words, }, }, ).All(&keywords) return keywords, err }
func FindMovies(titles []string) ([]Movie, error) { sess, d, err := db.GetFactory().GetSession() if err != nil { return nil, fmt.Errorf("couldn't get session: %v", err) } defer sess.Close() movies := []Movie{} err = d.C(MOVIE_COLLECTION).Find( bson.M{ "t": bson.M{ "$in": titles, }, }, ).All(&movies) return movies, err }
func FindMoviesWithPrefix(prefix string) ([]Movie, error) { sess, d, err := db.GetFactory().GetSession() if err != nil { return nil, fmt.Errorf("couldn't get session: %v", err) } defer sess.Close() movies := []Movie{} err = d.C(MOVIE_COLLECTION).Find( bson.M{ "t": bson.RegEx{ Pattern: "^" + prefix, Options: "", }, }, ).Limit(20).All(&movies) return movies, err }