Last Updated: 3/9/2026
Collection operations
Collections are usually managed via the Dashboard interface, but there are some situations where you may want to create or edit a collection programmatically (usually as part of a DB migration). You can find all available Collection related operations and methods in core.App and core.Collection , but below are listed some of the most common ones:
Fetch collections
Fetch single collection
All single collection retrieval methods return nil and sql.ErrNoRows error if no collection is found.
,:=. FindCollectionByNameOrId("example")
Fetch multiple collections
All multiple collections retrieval methods return empty slice and nil error if no collections are found.
,:=. FindAllCollections(),:=. FindAllCollections(.,.)
Custom collection query
In addition to the above query helpers, you can also create custom Collection queries using CollectionQuery() method. It returns a SELECT DB builder that can be used with the same methods described in the Database guide.
import("github.com/pocketbase/dbx""github.com/pocketbase/pocketbase/core")... func FindSystemCollections(.)([]*., error){:=[]*.{}:=. CollectionQuery(). AndWhere(.{"system": true}). OrderBy("created DESC"). All(&) if!= nil{return nil,} return, nil}
Collection properties
string string string// "base", "view", "auth" bool// !prevent collection rename, deletion and rules change of internal collections like _superusers..[string]..// CRUD rules* string* string* string* string* string// "view" type specific options// (see https://github.com/pocketbase/pocketbase/blob/master/core/collection_model_view_options.go) string// "auth" type specific options// (see https://github.com/pocketbase/pocketbase/blob/master/core/collection_model_auth_options.go)* string* string.............
Field definitions
core.BoolFieldcore.NumberFieldcore.TextFieldcore.EmailFieldcore.URLFieldcore.EditorFieldcore.DateFieldcore.AutodateFieldcore.SelectFieldcore.FileFieldcore.RelationFieldcore.JSONFieldcore.GeoPointField
Create new collection
import("github.com/pocketbase/pocketbase/core""github.com/pocketbase/pocketbase/tools/types")...// core.NewAuthCollection("example")// core.NewViewCollection("example"):=. NewBaseCollection("example")// set rules. =. Pointer("@request.auth.id != ''"). =. Pointer("@request.auth.id != '' && @request.body.user = @request.auth.id"). =. Pointer( @request.auth.id != ” && user = @request.auth.id && (@request.body.user:isset = false || @request.body.user = @request.auth.id) )// add text field.. Add(&.{: "title",: true,: 100,})// add relation field,:=. FindCollectionByNameOrId("users") if!= nil{return}.. Add(&.{: "user",: true,: 100,: true,:.,})// add autodate/timestamp fields (created/updated).. Add(&.{: "created",: true,}).. Add(&.{: "updated",: true,: true,})// or: collection.Indexes = []string{"CREATE UNIQUE INDEX idx_example_user ON example (user)"}. AddIndex("idx_example_user", true, "user", "")// validate and persist// (use SaveNoValidate to skip fields validation) =. Save() if!= nil{return}
Update existing collection
import("github.com/pocketbase/pocketbase/core""github.com/pocketbase/pocketbase/tools/types")...,:=. FindCollectionByNameOrId("example") if!= nil{return}// change rule. =. Pointer("@request.auth.id != ''")// add new editor field.. Add(&.{: "description",: true,})// change existing field// (returns a pointer and direct modifications are allowed without the need of reinsert):=.. GetByName("title").(*.). = 10// or: collection.Indexes = append(collection.Indexes, "CREATE INDEX idx_example_title ON example (title)"). AddIndex("idx_example_title", false, "title", "")// validate and persist// (use SaveNoValidate to skip fields validation) =. Save() if!= nil{return}
Delete collection
,:=. FindCollectionByNameOrId("example") if!= nil{return} =. Delete() if!= nil{return}