#1 2015-03-25 08:17:06

htits2008
Member
Registered: 2015-03-25
Posts: 34

How to save a File to MongoDB with mORMot Framework?

How to save a File to MongoDB with mORMot Framework?
I Use TMongoClient to link MongoDB like sample in Documentation. But the Sample don't  tell me how to save a  Pictrue , file or stream  to MongoDB.
Could You tell how to do? and get it back.
----------------------------------------------
here is the Sample.
---------------------------------------------
var Coll: TMongoCollection;
    doc: variant;
    i: integer;
begin
  Coll := DB.CollectionOrCreate[COLL_NAME];
  TDocVariant.New(doc);
  for i := 1 to 10 do
  begin
    doc.Clear;
    doc.Name := 'Name '+IntToStr(i+1);
    doc.Number := i;
    Coll.Save(doc);
    writeln('Inserted with _id=',doc._id);
  end;
end;
-----------------------------
http://synopse.info/files/html/Synopse% … #TITLE_187

Offline

#2 2015-03-25 13:29:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: How to save a File to MongoDB with mORMot Framework?

For complex BLOB types, you should better use a TBsonVariant instead of a TDocVariant.
Our BSON variant is able to store BLOB kind of fields.

But there is no GridFS support yet.
So you have the limitation of 16 MB of size per document.
If you want to store huge content, do not use MongoDB, but a regular file system over the network.

Offline

#3 2015-03-26 00:28:13

htits2008
Member
Registered: 2015-03-25
Posts: 34

Re: How to save a File to MongoDB with mORMot Framework?

thinks !

Offline

#4 2015-03-26 17:23:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: How to save a File to MongoDB with mORMot Framework?

In fact, the easiest may be to use a TDocVariant to create the document, and then use BsonVariantType.FromBinary() to generate a BLOB value:

var Coll: TMongoCollection;
    doc,blob: variant;
    i: integer;
begin
  Coll := DB.CollectionOrCreate[COLL_NAME];
  TDocVariant.New(doc);
  for i := 1 to 10 do
  begin
    doc.Clear;
    doc.Name := 'Name '+IntToStr(i+1);
    doc.Number := i;
    BsonVariantType.FromBinary(StringFromFile(PathFolder+doc.Name),bbtGeneric,blob);
    doc.FileContent := blob;
    Coll.Save(doc);
    writeln('Inserted with _id=',doc._id);
  end;
end;

The limit of 16 MB size for the whole document (including the blob) still applies.

Offline

#5 2015-03-30 13:00:54

leeonsoft
Member
Registered: 2014-09-07
Posts: 4

Re: How to save a File to MongoDB with mORMot Framework?

mORMot  Can Save MongoDB GridFS  ? how to do ?

Offline

#6 2015-03-30 13:03:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: How to save a File to MongoDB with mORMot Framework?

This is not directly supported yet.

Offline

#7 2015-03-30 13:49:13

leeonsoft
Member
Registered: 2014-09-07
Posts: 4

Re: How to save a File to MongoDB with mORMot Framework?

can you plan to support ? tks !

Offline

#8 2015-03-30 17:59:05

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: How to save a File to MongoDB with mORMot Framework?

Could you create a feature request ticket, with a link to this forum thread, please?
Thanks!

Offline

#9 2015-03-31 02:47:17

leeonsoft
Member
Registered: 2014-09-07
Posts: 4

Re: How to save a File to MongoDB with mORMot Framework?

Offline

#10 2015-05-13 11:03:53

sh17
Member
Registered: 2011-08-09
Posts: 31

Re: How to save a File to MongoDB with mORMot Framework?

+1 for GridFS please

Offline

#11 2016-02-17 07:30:51

sh17
Member
Registered: 2011-08-09
Posts: 31

Re: How to save a File to MongoDB with mORMot Framework?

What are the difficults to support GridFS? Aside from that, who programmed it.

Offline

#12 2016-02-17 10:01:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: How to save a File to MongoDB with mORMot Framework?

We do not need GridFS here at all - since our blobs are smaller than the 16 MB limit.
And we have some doubts about its practical impact with our ORM/ODM, which usually contain all its data in memory during process, so the 16 MB limit seems fair enough.
Big files benefit from using a dedicated distributed file system (like Hadoop, or even ZFS), and not MongoDB GridFS, which is IMHO a half-backed feature: you store huge binary in the middle of collections...
See https://www.compose.io/articles/gridfs- … -and-cons/
This is why the feature is not implemented yet.

There are no technical difficulty at SynMongoDB.pas level.
Just missing time to add this feature.
Any patch is welcome!

Offline

#13 2016-02-17 17:43:31

sh17
Member
Registered: 2011-08-09
Posts: 31

Re: How to save a File to MongoDB with mORMot Framework?

thank you @ab, for your explanations. I would have to check that again. But I think we exceed the 16 MB limit also not. And the files are rarely changed.

Offline

Board footer

Powered by FluxBB