Skip to content
Dannes Wessels edited this page Jul 22, 2015 · 13 revisions

List buckets

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

Store files

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)
    )

Get by objectId

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')

Stream by objectId

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())

Get document properties

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)

Remove files

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)
Clone this wiki locally