Use Gridfs-stream to store files encountered by a pit.

Source: Internet
Author: User

Some time ago read a coder write with Gridfs-stream to store files, feel good on their own with the Gridfs-stream module to write a file storage service, but found that the stored files are always deleted, I call the GFS's remove API, Delete the _id that the condition is used for.

  Gfs.remove ({_id: ' xxxxxx '}, callback);

Resistance to look at the source code, found that the direct cause of the problem is because I do not use MongoDb ObjectId as a result of _id. I'm using a string of my own definition.

Looking back at the Gridfs-stream document, I finally found in a note in the sample code

_id: ' 50e03d29edfdc00d34000001 ',//a MongoDb ObjectId

Immediately 10,000 grass mud horse ....

If you must customize the string as a file storage _id, there is a way, but to modify the source of the MongoDB module, in the MongoDB module \lib\gridfs\grid_store.js file has a unlinkstatic method, There's a sentence inside

New Gridstore (db, Names, "W", Options)

Switch

New Gridstore (db, names, ', ' w ', options)

  

Can. Specific reasons to see the source of their own it.

Use Gridfs-stream to store files encountered by a pit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.