GridFS ClassesTable of Contents
The MongoGridFS classIntroductionUtilities for storing and retrieving files from the database. GridFS is a storage specification all supported drivers implement. Basically, it defines two collections: files, for file metadata, and chunks, for file content. If the file is large, it will automatically be split into smaller chunks and each chunk will be saved as a document in the chunks collection. Each document in the files collection contains the filename, upload date, and md5 hash. It also contains a unique _id field, which can be used to query the chunks collection for the file's content. Each document in the chunks collection contains a chunk of binary data, a files_id field that matches its file's _id, and the position of this chunk in the overall file. For example, the files document is something like:
<?php
<?phpInter-Language CompatibilityYou should be able to use any files created by MongoGridFS with any other drivers, and vice versa. However, some drivers expect that all metadata associated with a file be in a "metadata" field. If you're going to be using other languages, it's a good idea to wrap info you might want them to see in a "metadata" field. For example, instead of:
<?phpuse something like:
<?phpThe MongoGridFS FamilyMongoGridFS represents the files and chunks collections. MongoGridFS extends MongoCollection, and an instance of MongoGridFS has access to all of MongoCollection methods, which act on the files collection:
<?phpAnother example of manipulating metadata:
<?phpYou can also access the chunks collection from an instance of MongoGridFS:
<?phpThere are some methods for MongoGridFS with the same name as MongoCollection methods, that behave slightly differently. For example, MongoGridFS::remove will remove any objects that match the criteria from the files collection and their content from the chunks collection. To store something new in GridFS, there are a couple options. If you have a filename, you can say:
<?phpIf you have a string of bytes that isn't a file, you can also store that using MongoGridFS::storeBytes:
<?phpQuerying a MongoGridFS collection returns a MongoGridFSCursor, which behaves like a normal MongoCursor except that it returns MongoGridFSFiles instead of associative arrays. MongoGridFSFiles can be written back to disc using MongoGridFSFile::write or retrieved in memory using MongoGridFSFile::getBytes. There is currently no method that automatically streams chunks, but it would be fairly easy to write by querying the $grid->chunks collection. MongoGridFSFile objects contain a field file which contains any file metadata. Class synopsisMongoGridFS
extends
class MongoCollection
{
/* Fields */
public
MongoCollection
$chunks
=
NULL
;
protected
string
$filesName
=
NULL
;
protected
string
$chunksName
=
NULL
;/* Methods */
public __construct
( MongoDB
$db
[, string $prefix = "fs"
[, mixed $chunks = "fs"
]] )
public bool|array delete
( mixed
$id
)
public array drop
( void
)
public MongoGridFSCursor find
([ array
$query = array()
[, array $fields = array()
]] )
public MongoGridFSFile findOne
([ mixed
$query = array()
[, mixed $fields = array()
]] )
public MongoGridFSFile get
( mixed
$id
)
public mixed put
( string
$filename
[, array $metadata = array()
[, array $options = array()
]] )
public bool|array remove
([ array
$criteria = array()
[, array $options = array()
]] )
public mixed storeBytes
( string
$bytes
[, array $metadata = array()
[, array $options = array()
]] )
public mixed storeFile
( string|resource
$filename
[, array $metadata = array()
[, array $options = array()
]] )
public mixed storeUpload
( string
}$name
[, array $metadata
] )See Also
The MongoGridFSFile classIntroductionA database file object. Class synopsisMongoGridFSFile
class MongoGridFSFile
{
/* Fields */
public
array
$file
=
NULL
;
protected
MongoGridFS
$gridfs
=
NULL
;/* Methods */
public MongoGridfsFile::__construct
( MongoGridFS
$gridfs
, array $file
)
public string getBytes
( void
)
public string getFilename
( void
)
public resource getResource
( void
)
public int getSize
( void
)
public int write
([ string
}$filename = NULL
] )The MongoGridFSCursor classIntroductionCursor for database file results. Class synopsisMongoGridFSCursor
extends
class MongoCursor
{
/* Fields */
protected
MongoGridFS
$gridfs
=
NULL
;/* Methods */
public __construct
( MongoGridFS
$gridfs
, resource $connection
, string $ns
, array $query
, array $fields
)
public MongoGridFSFile current
( void
)
public MongoGridFSFile getNext
( void
)
public string key
( void
)
} |