How do we insert image/audio/video files in MongoDB ?
Uploading a video file using GRIDFS with mongofiles utility :
mongofiles -d database put video.mp4
mongofiles -d database get video.mp4
mongofiles -d database delete video.mp4
[dbfryusr@dbversity ~]$ ./mongofiles –port 27010 -d shrdb put /home/dbfryusr/MongoDB.mp4
connected to: 127.0.0.1:27010
added file: { _id: ObjectId(‘56729dea1196355511f0b414’), filename: “/home/dbfryusr/MongoDB.mp4”, chunkSize: 261120, uploadDate: new Date(1450352107882), md5: “314a85e4aac148f6f588d1deabd2a716”, length: 18550241 }
done!
[dbfryusr@dbversity ~]$./mongo –port 27010
rs1:PRIMARY> db.fs.files.find()
{ “_id” : ObjectId(“56729b01bd9092f0b32db24e”), “filename” : “/home/dbfryusr/MongoDB.mp4”, “chunkSize” : 261120, “uploadDate” : ISODate(“2015-12-17T11:22:44.032Z”), “md5” : “314a85e4aac148f6f588d1deabd2a716”, “length” : 18550241 }
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> db.fs.chunks.findOne()
{
“_id” : ObjectId(“56729b016910bab988a65908”),
“files_id” : ObjectId(“56729b01bd9092f0b32db24e”),
“n” : 0,
“data” : BinData(0,”AAAAGGZ0eXBtcDQyAAAAAGlzb21tcDQyAAFaFm1vb3YAAABsbXZoZAAAAADKwjCQzpPdBgAAAlgAAQbYAAEAAAEAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAVaW9kcwAAAAAQBwBP//8pFf8AAIzSdHJhawAAAFx0a2hkAAAAD3wlsIDKwjCQAAAAAQAAAAAAAQbYAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAUAAAAC0AAAAACMbm1kaWEAAAAgbWRoZAAAAAB8JbCAysIwkAAAA+gAAbYTVcQAAAAAAC1oZGxyAAAAAAAAAAB2aWRlAAAAAAAAAAAAAAAAVmlkZW9IYW5kbGVyAAAAjBltaW5mAAAAFHZtaGQAAAABAAAAAAAAAAAAAAAkZGluZgAAABxkcmVmAAAAAAAAAAEAAAAMdXJsIAAAAAEAAIvZc3RibAAAAKlzdHNkAAAAAAAAAAEAAACZYXZjMQAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAUAAtAASAAAAEgAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABj//wAAAC9hdmNDAWQAH//hABhnZAAfrCSIBQBboQAAAwPpAADqYA8YMqABAARo7jywAAAAFGJ0cnQAAQukADlMOAARxcAAAE0gc3R0cwAAAAAAAAmiAAAAAQAAACEAAAABAAAAIgAAAAIAAAAhAAAAAQAAACIAAAABAAAAIQAAAAEAAAAiAAAAAgAAACEAAAABAAAAIgAAAAIAAAAhAAAAAQAAACIAAAABAAAAIQAAAAEAAAAiAAAAAgAAACEAAAABAAAAIgAAAAIAAAAhAAAAAQAAACIAAAACAAAAIQAAAAEAAAAiAAAAAQAAACEAAAABAAAAIgAAAAIAAAAhAAAAAQAAACIAAAACAAAAIQAAAAEAAAAiAAAAAgAAACEAAAABAAAAIgAAAAEAAAAhAAAAAQAAACIAAAACAAAAIQAAAAEAAAAiAAAAAgAAACEAAAABAAAAIgAAAAEAAAAhAAAAAQAAACIAAAACAAAAIQAAAAEAAAAiAAAAAgAAACEAAAABAAAAIgAAAAIAAAAhAAAAAQAAACIAAAA
Uploading a Image file using GRIDFS with mongofiles utility :
[ user @ dbfry.com : . ] ./mongofiles -d my_newimage_db put /home/dbversity/bw-linux.jpg
2015-12-17T06:47:00.854-0500 [initandlisten] connection accepted from 127.0.0.1:48997 #1 (1 connection now open)
connected to: 127.0.0.1
2015-12-17T06:47:00.856-0500 [conn1] allocating new ns file /opt/mongodb/my_newimage_db.ns, filling with zeroes…
2015-12-17T06:47:00.931-0500 [FileAllocator] allocating new datafile /opt/mongodb/my_newimage_db.0, filling with zeroes…
2015-12-17T06:47:01.178-0500 [FileAllocator] done allocating datafile /opt/mongodb/my_newimage_db.0, size: 64MB, took 0.247 secs
2015-12-17T06:47:01.179-0500 [conn1] build index on: my_newimage_db.fs.files properties: { v: 1, key: { _id: 1 }, name: “_id_”, ns: “my_newimage_db.fs.files” }
2015-12-17T06:47:01.179-0500 [conn1] added index to empty collection
2015-12-17T06:47:01.179-0500 [conn1] build index on: my_newimage_db.fs.files properties: { v: 1, key: { filename: 1 }, name: “filename_1”, ns: “my_newimage_db.fs.files” }
2015-12-17T06:47:01.179-0500 [conn1] added index to empty collection
2015-12-17T06:47:01.179-0500 [conn1] insert my_newimage_db.system.indexes ninserted:1 keyUpdates:0 numYields:0 locks(micros) w:323684 323ms
2015-12-17T06:47:01.180-0500 [conn1] build index on: my_newimage_db.fs.chunks properties: { v: 1, key: { _id: 1 }, name: “_id_”, ns: “my_newimage_db.fs.chunks” }
2015-12-17T06:47:01.180-0500 [conn1] added index to empty collection
2015-12-17T06:47:01.180-0500 [conn1] build index on: my_newimage_db.fs.chunks properties: { v: 1, unique: true, key: { files_id: 1, n: 1 }, name: “files_id_1_n_1”, ns: “my_newimage_db.fs.chunks” }
2015-12-17T06:47:01.180-0500 [conn1] added index to empty collection
added file: { _id: ObjectId(‘5672a0b4efd71aadfd5ab514’), filename: “/home/dbversity/bw-linux.jpg”, chunkSize: 261120, uploadDate: new Date(1450352821186), md5: “e92ad40955b076122f7a0cce2fcfa38a”, length: 92174 }
done!
[ user @ dbfry.com : . ]
[ user @ dbfry.com : . ]
[ user @ dbfry.com : . ] ./mongo
MongoDB shell version: 2.6.9
connecting to: test
>
>
> show dbs
admin (empty)
local 0.078GB
my_newimage_db 0.078GB
>
> use my_newimage_db
switched to db my_newimage_db
>
>
> show collections
fs.chunks
fs.files
system.indexes
>
>
> db.fs.chunks.count()
1
> db.fs.files.count()
1
>
>
> db.fs.chunks.findOne()
{
“_id” : ObjectId(“5672a0b5122516ed19071165”),
“files_id” : ObjectId(“5672a0b4efd71aadfd5ab514”),
“n” : 0,
“data” : BinData(0,”/9j/4AAQSkZJRgABAQEAlgCWAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQICAQECAQEBAgICAgICAgICAQICAgICAgICAgL/wAALCAKnA+YBAREA/8QAHwABAAAFBQEAAAAAAAAAAAAAAAECAwQKBQcICQsG/8QAdBAAAQMCBAIGBQcGBggPCgkNAQACAwQRBQYSIQcxCBMiQVFhCQoUMnEjQlKBkaGxFTNiwdHwFiRDcpLhFzRTdYKitLUaJSY1Njc4VXR2k7K31vEYOURUY2SzwtLiGSdFRmV3g4aXtsPXSFZXc5RmhIWW1P/aAAgBAQAAPwDP4REREREREREREREREREREREREREREREREREREREREREREREREREREREREREVB+tzw3QSwczcdom32W3Vu6VkjmwwTtjeDd7CxziQeQu5u291LHMGPEboS2XVa2snU24Al2GwJ7u5QlA9obPLEdLWBofd2x1lwFm8+fgvmcWzbgeX5JZcXxiHDo3PeY+iIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIi//9k=”)
}
>
Removing the file on the local disk and retrieving it from Database using get method
[ user @ dbfry.com : . ] ll -lhtr /home/dbversity/bw-linux.jpg
-rw-r–r– 1 dbversity mongod 91K Dec 17 23:33 /home/dbversity/bw-linux.jpg
[ user @ dbfry.com : . ]
[ user @ dbfry.com : . ] rm -rf /home/dbversity/bw-linux.jpg
[ user @ dbfry.com : . ] ./mongofiles -d my_newimage_db get /home/dbversity/bw-linux.jpg
done write to: /home/dbversity/bw-linux.jpg
[ user @ dbfry.com : . ]
[ user @ dbfry.com : . ]
[ user @ dbfry.com : . ] ll -lhtr /home/dbversity/bw-linux.jpg
-rw-r–r– 1 user user 91K Dec 17 23:34 /home/dbversity/bw-linux.jpg
[ user @ dbfry.com : . ]
Deleting it from Database :
[ root @ dbfry.com : ] ./mongofiles -d my_newimage_db delete /home/dbversity/bw-linux.jpg
2015-12-17T23:48:32.885-0500 [initandlisten] connection accepted from 127.0.0.1:54340 #7 (1 connection now open)
connected to: 127.0.0.1
done!
[ root @ dbfry.com : ] 2015-12-17T23:48:32.900-0500 [conn7] end connection 127.0.0.1:54340 (0 connections now open)
[ root @ dbfry.com : ]
[ root @ dbfry.com : ] ./mongo
MongoDB shell version: 2.6.9
connecting to: test
2015-12-17T23:48:46.128-0500 [initandlisten] connection accepted from 127.0.0.1:54342 #8 (1 connection now open)
>
> use my_newimage_db
switched to db my_newimage_db
> db.fs.chunks.findOne()
null
>
how about uploading a file without gridfs?
No, not possible without GridFS i.e., mngodfiles utility