GridFS存储文件

MongoDB针对于文件,尤其是大文件,专门设计了一套接口,叫做:GridFS

此处总结一下GridFS的使用心得。

TODO:

一些供参考的资料:

一些GirdFS的心得

通过id查找gridfs中file文件

注意不是:

> db.fs.files.find({"id": "5b556ee47f4d38ba6a189222"})
> db.fs.files.find({"_id": "5b556ee47f4d38ba6a189222"})

而是ObjectId("5b556ee47f4d38ba6a189222")

> db.fs.files.find({"_id": ObjectId("5b556ee47f4d38ba6a189222")})
{ "_id" : ObjectId("5b556ee47f4d38ba6a189222"), "contentType" : "audio/mpeg", "chunkSize" : 261120, "metadata" : { "song" : { "singers" : [ "ChuChu TV" ] }, "series" : { "number" : 0, "name" : "" }, "topics" : [ ], "storybook" : { "publisher" : "", "isFiction" : "未知", "lexileIndex" : "", "awards" : "", "authors" : [ ], "foreignCountry" : "" }, "keywords" : { "fromName" : [ "Finger", "Family" ], "other" : [ ], "fromContent" : [ "Daddy Finger" ] }, "name" : "Finger Family", "resourceType" : "song", "mainActors" : [ ], "contentAbstract" : "", "isSeries" : false, "fitAgeStart" : 3, "fitAgeEnd" : 6, "fileInfo" : { "isAudio" : true, "contentType" : "audio/mpeg", "name" : "Finger Family.mp3", "suffix" : "mp3" } }, "filename" : "Finger Family.mp3", "length" : 2604280, "uploadDate" : ISODate("2018-07-23T06:00:04.925Z"), "md5" : "946564effc4c0a835c55564377e7d819" }
>

删除文件

举例:

> db.fs.files.deleteOne({"_id": ObjectId("5b556ee47f4d38ba6a189222")})
{ "acknowledged" : true, "deletedCount" : 1 }

格式化带缩进美观的输出结果

后面加上:.pretty()

> db.fs.files.find({"_id": ObjectId("5b556ee47f4d38ba6a189222")}).pretty()
{
    "_id" : ObjectId("5b556ee47f4d38ba6a189222"),
    "contentType" : "audio/mpeg",
    "chunkSize" : 261120,
    "metadata" : {
        "song" : {
            "singers" : [
                "ChuChu TV"
            ]
        },
        "series" : {
            "number" : 0,
            "name" : ""
        },
        "topics" : [ ],
        "storybook" : {
            "publisher" : "",
            "isFiction" : "未知",
            "lexileIndex" : "",
            "awards" : "",
            "authors" : [ ],
            "foreignCountry" : ""
        },
        "keywords" : {
            "fromName" : [
                "Finger",
                "Family"
            ],
            "other" : [ ],
            "fromContent" : [
                "Daddy Finger"
            ]
        },
        "name" : "Finger Family",
        "resourceType" : "song",
        "mainActors" : [ ],
        "contentAbstract" : "",
        "isSeries" : false,
        "fitAgeStart" : 3,
        "fitAgeEnd" : 6,
        "fileInfo" : {
            "isAudio" : true,
            "contentType" : "audio/mpeg",
            "name" : "Finger Family.mp3",
            "suffix" : "mp3"
        }
    },
    "filename" : "Finger Family.mp3",
    "length" : 2604280,
    "uploadDate" : ISODate("2018-07-23T06:00:04.925Z"),
    "md5" : "946564effc4c0a835c55564377e7d819"
}
>

results matching ""

    No results matching ""