Skip to content

3. GridFS File Storage

Jon Clausen edited this page Jan 1, 2016 · 19 revisions

CBMongoDB includes an interface and Active Entity implementation for using MongoDB's GridFS for relational, distributed file storage. Let's say, for example, that we want to associate profile images to our people collection.

Let's create a relational entity that will be responsible for storing and retrieving profile pictures. Our component should extend cbmongodb.models.FileEntity. An additional component attribute may be specified with the name of the bucket used to store the file data. By default, Mongo uses a bucket named fs.

component name="ProfileImages" extends="cbmongodb.models.FileEntity" collection="profileImages" bucket="images" accessors=true{
	//normalize our person information, in case we need it for captions and alt information.
	//Note that we are using auto-normalization of the first_name and last_name keys on our person entity
	property name="person" schema=true normalize="Person" on="person.id" keys="first_name,last_name";
		property name="person.id" schema=true required=true;	
}

Because the querying syntax for GridFS files differs from our standard query syntax, entities use a standard MongoDB collection which connects to the GridFS file object. Every FileEntity schema contains a property fileId which allows for a one-to-one relationship to the GridFS file.

Clone this wiki locally