This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/database.go

62 lines
1 KiB
Go
Raw Normal View History

2012-07-27 16:03:09 +02:00
package main
2012-08-19 02:29:34 +02:00
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
2012-07-27 16:03:09 +02:00
type Book struct {
2012-08-19 02:29:34 +02:00
Id string `bson:"_id"`
2012-07-30 23:23:38 +02:00
Title string
Author []string
Contributor string
Publisher string
Description string
Subject []string
Date string
Lang []string
Type string
Format string
Source string
Relation string
Coverage string
Rights string
Meta string
Path string
Cover string
CoverSmall string
2012-08-15 15:12:59 +02:00
Keywords []string
2012-07-27 16:03:09 +02:00
}
2012-08-19 02:29:34 +02:00
2012-08-21 11:25:02 +02:00
/* optional parameters: length and start index
*
* Returns: list of books, number found and err
*/
func GetBook(coll *mgo.Collection, query bson.M, r ...int) (books []Book, num int, err error) {
var start, length int
if len(r) > 0 {
length = r[0]
if len(r) > 1 {
start = r[1]
}
}
q := coll.Find(query)
num, err = q.Count()
if err != nil {
return
}
if start != 0 {
q = q.Skip(start)
}
if length != 0 {
q = q.Limit(length)
}
err = q.All(&books)
2012-08-19 02:29:34 +02:00
for i, b := range books {
books[i].Id = bson.ObjectId(b.Id).Hex()
}
2012-08-21 11:25:02 +02:00
return
2012-08-19 02:29:34 +02:00
}