-
Notifications
You must be signed in to change notification settings - Fork 6
GridFS Examples
Dannes Wessels edited this page Jul 22, 2015
·
13 revisions
xquery version "3.0";
(:
: Example: list all buckets in mongodb
:
: Script can be run in eXide
:
: User must be either DBA or in group mongodb
:)
import module namespace mongodb = "http://expath.org/ns/mongo/"
at "java:org.exist.mongodb.xquery.MongodbModule";
import module namespace gridfs = "http://expath.org/ns/mongo/gridfs"
at "java:org.exist.mongodb.xquery.GridfsModule";
let $mongoUrl := "mongodb://localhost"
(: connect to mongodb :)
let $mongodbClientId := mongodb:connect($mongoUrl)
return
for $db in mongodb:list-databases($mongodbClientId)
return
for $bucket in gridfs:list-buckets($mongodbClientId, $db)
return
$mongodbClientId || ":" || $db || ":" || $bucket
xquery version "3.0";
(:
: Example: store documents in GridFs
:
: Script can be started from eXide
: User must be either DBA or in group mongodb
:)
import module namespace mongodb = "http://expath.org/ns/mongo/"
at "java:org.exist.mongodb.xquery.MongodbModule";
import module namespace gridfs = "http://expath.org/ns/mongo/gridfs"
at "java:org.exist.mongodb.xquery.GridfsModule";
let $mongoUrl := "mongodb://localhost"
let $database := "mydatabase"
let $bucket := "mybucket"
(: connect to mongodb :)
let $mongodbClientId := mongodb:connect($mongoUrl)
return
(
(: store text fragment as document :)
gridfs:store($mongodbClientId, $database, $bucket,
"filename.txt", "plain/text",
"this is the content"),
(: store XML document as XML doocument :)
gridfs:store($mongodbClientId, $database, $bucket,
"build.xml", "application/xml",
doc("/db/apps/dashboard/build.xml")),
(: store nonXML document as document :)
gridfs:store($mongodbClientId, $database, $bucket,
"Readme.txt", "plain/text",
util:binary-doc("/db/apps/dashboard/Readme.md")),
(: store URL-content as HTML file :)
gridfs:store($mongodbClientId, $database, $bucket,
"filename.html", "text/html",
xs:anyURI('http://localhost:8080/exist/')),
(: store nonXML document, determine mimetype automagically :)
gridfs:store($mongodbClientId, $database, $bucket,
"exide.png", (),
util:binary-doc("/db/apps/eXide/resources/images/exide.png")),
(: close connection :)
mongodb:close($mongodbClientId)
)
xquery version "3.0";
(:
: Example: retrieve document from mongodb, stream result
:
: Script must be stored in database.
:
: User must be either DBA or in group mongodb
:)
import module namespace mongodb = "http://expath.org/ns/mongo/"
at "java:org.exist.mongodb.xquery.MongodbModule";
import module namespace gridfs = "http://expath.org/ns/mongo/gridfs"
at "java:org.exist.mongodb.xquery.GridfsModule";
let $mongoUrl := "mongodb://localhost"
let $database := "mydatabase"
let $bucket := "mybucket"
(: connect to mongodb :)
let $mongodbClientId := mongodb:connect($mongoUrl)
(: store document into mongodb :)
let $objectid := gridfs:store($mongodbClientId, $database, $bucket,
"exide.png", (),
util:binary-doc("/db/apps/eXide/resources/images/exide.png"))
(: get retrieve data from database :)
let $data := gridfs:get-by-objectid($mongodbClientId, $database, $bucket, $objectid, true())
return
(: stream document to user :)
response:stream-binary($data, "image/png", 'exide.png')
xquery version "3.0";
(:
: Example: stream document from mongodb
:
: Script must be stored in database.
:
: User must be either DBA or in group mongodb
:)
import module namespace mongodb = "http://expath.org/ns/mongo/"
at "java:org.exist.mongodb.xquery.MongodbModule";
import module namespace gridfs = "http://expath.org/ns/mongo/gridfs"
at "java:org.exist.mongodb.xquery.GridfsModule";
let $mongoUrl := "mongodb://localhost"
let $database := "mydatabase"
let $bucket := "mybucket"
(: connect to mongodb :)
let $mongodbClientId := mongodb:connect($mongoUrl)
(: store document into mongodb :)
let $objectid := gridfs:store($mongodbClientId, $database, $bucket,
"exide.png", (),
util:binary-doc("/db/apps/eXide/resources/images/exide.png"))
(: stream data directly from mongodb :)
return
gridfs:stream-by-objectid($mongodbClientId, $database, $bucket, $objectid, false())
xquery version "3.0";
(:
: Example: retrieve properties from document in mongodb
:
: User must be either DBA or in group mongodb
:)
import module namespace mongodb = "http://expath.org/ns/mongo/"
at "java:org.exist.mongodb.xquery.MongodbModule";
import module namespace gridfs = "http://expath.org/ns/mongo/gridfs"
at "java:org.exist.mongodb.xquery.GridfsModule";
let $mongoUrl := "mongodb://localhost"
let $database := "mydatabase"
let $bucket := "mybucket"
(: connect to mongodb :)
let $mongodbClientId := mongodb:connect($mongoUrl)
(: store document into mongodb :)
let $objectid := gridfs:store($mongodbClientId, $database, $bucket,
"build.xml", "application/xml",
doc("/db/apps/dashboard/build.xml"))
return
(: report properties :)
gridfs:properties-by-objectid($mongodbClientId, $database, $bucket, $objectid)
xquery version "3.0";
(:
: Example: remove all documents from GridFS
:
: Script can be started from eXide
: User must be either DBA or in group mongodb
:)
import module namespace mongodb = "http://expath.org/ns/mongo/"
at "java:org.exist.mongodb.xquery.MongodbModule";
import module namespace gridfs = "http://expath.org/ns/mongo/gridfs"
at "java:org.exist.mongodb.xquery.GridfsModule";
let $mongoUrl := "mongodb://localhost"
let $database := "mydatabase"
let $bucket := "mybucket"
(: connect to mongodb :)
let $mongodbClientId := mongodb:connect($mongoUrl)
return
(: iterate over all document ids :)
for $objectid in gridfs:list-documents($mongodbClientId, $database, $bucket)//id/text()
return
(: delete document and report id :)
gridfs:remove-by-objectid($mongodbClientId, $database, $bucket, $objectid)