Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Actually your question has many implications on many levels. Basically on MongoDB level one may save binary data in fields of binary type of ordinary documents. I believe it is working now in mongo_dart. In objectory_console (server side version of objectory which communicate with mongodb directly via mongo_dart) I think it would work too. Your model object would be like</p> <pre class="lang-dart prettyprint-override"><code>class UploadedFile extends PersistentObject { String get fileName =&gt; getProperty('fileName'); set fileName(String value) =&gt; setProperty('fileName',value); BsonBinary get content =&gt; getProperty('content'); set content(BsonBinary value) =&gt; setProperty('content',value); .... .... } </code></pre> <p>BsonBinary has named constructor <code>BsonBinary.from(List from)</code> which efficient but works only with dart:io (on server side) For browser based objectory currently you may use named constructor <code>BsonBinary.fromHexString()</code>. I'm afraid this woud have significant incurred overhead. </p> <p>Another consideration is limit on total document size in MongoDB, which is 16mb. So your files must be less then that size.</p> <p>Files of bigger size one store in MongoDB through GridFS layer, GridFS support was added to mongo_dart recently.</p> <p>Usage example from <a href="https://github.com/vadimtsushko/mongo_dart/blob/master/test/gridfs_tests.dart" rel="nofollow">https://github.com/vadimtsushko/mongo_dart/blob/master/test/gridfs_tests.dart</a> :</p> <pre class="lang-dart prettyprint-override"><code>Future testFileToGridFSToFile() { GridFS.DEFAULT_CHUNKSIZE = 30; GridIn input; String dir = path.dirname(path.fromUri(Platform.script)); var inputStream = new File('$dir/gridfs_testdata_in.txt').openRead(); Db db = new Db('${DefaultUri}mongo_dart-test'); return db.open().then((c){ var gridFS = new GridFS(db); clearFSCollections(gridFS); input = gridFS.createFile(inputStream, "test"); return input.save(); }).then((c) { var gridFS = new GridFS(db); return gridFS.getFile('test'); }).then((GridOut gridOut) { return gridOut.writeToFilename('$dir/gridfs_testdata_out.txt'); }).then((c){ List&lt;int&gt; dataIn = new File('$dir/gridfs_testdata_in.txt').readAsBytesSync(); List&lt;int&gt; dataOut = new File('$dir/gridfs_testdata_out.txt').readAsBytesSync(); expect(dataOut, orderedEquals(dataIn)); db.close(); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload