Relationship on mongodb between gridfs and collection

101 views
Skip to first unread message

Dika Andharu

unread,
Feb 6, 2015, 7:48:07 AM2/6/15
to mongod...@googlegroups.com
I want to ask about relationship on mongodb. I have project to make members list with document stored. So, every members have _id and name in the collection(Member) on my dbs(Data) and can store files like document(word,excel,pdf). But if I store document, it would saved on the gridfs(fs.files&fs.chunks) and it won't saved on my collection(Member). And my question is how to make relationship on mongodb between collection and gridfs(fs.files&fs.chunks) like other relational database with primary key and foreign key ?

Tugdual Grall

unread,
Feb 6, 2015, 12:10:19 PM2/6/15
to mongod...@googlegroups.com
Hello,

You just have to manage it the same way you manage references between 2 "documents".
(oversimplistic example below)

members
{
  _id : "user001",
 name : "john doe"
 files : [ "johnresume.doc" ]
}

// and you have save the document in gridfs :
GridFS gridfs = new GridFS(db, "downloads");
GridFSInputFile gfsFile = gridfs.createFile(file); << you binary content
gfsFile.setFilename("johnresume.doc");
gfsFile.save();

Like that you can find all the files for a members, so you manage the reference in your code.
You can also use some meta data in GridFS to extend with some custom attributes (setMetada() in Java)

Regards
Tug
@tgrall

Dika Andharu

unread,
Feb 6, 2015, 8:22:04 PM2/6/15
to mongod...@googlegroups.com
that's with java ? but I wanna make relationship mongodb and php. Do you know it ?
Reply all
Reply to author
Forward
0 new messages