Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:24:40+00:00 2026-06-17T05:24:40+00:00

I have the following code (removed error checking to keep it concise) which uses

  • 0

I have the following code (removed error checking to keep it concise) which uses the node-mongodb-native.

var mongo = require('mongodb').MongoClient;
var grid = require('mongodb').GridStore;
var url = 'mongodb://localhost:27017/mydatabase';

mongo.connect(url, function(err, db) {
    var gs = new grid(db, 'myfile.txt', 'w', {
        "metadata": {
            // metadata here
        }   
    }); 
    gs.open(function(err, store) {
        gs.writeFile('~/myfile.txt', function(err, doc) {
            fs.unlink(req.files.save.path, function (err) {
                // error checking etc
            }); 
        }   
    }); 
}); 

If I run that once it works fine and stores the file in GridFS.

Now, if I delete that file on my system and create a new one with the same name, but different contents, and run it though that code again it uploads it. However, it seems to overwrite the file that is already stored in GridFS. _id stays the same, but md5 has been updated to the new value. So even though the file is different, because the name is the same it overwrites the current file in GridFS.

Is there a way to upload two files with the same name? If _id is unique, why does the driver overwrite the file based on file name alone?

I found a similar issue on GitHub, but I am using the latest version of the driver from npm and it does what I explained above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:24:41+00:00Added an answer on June 17, 2026 at 5:24 am

    Like a real filesystem, the filename becomes the logical key in GridFS for reading and writing. You cannot have two files with the same name.

    You’ll need to come up with a secondary index of some sort or a new generated file name.

    For example, add a timestamp to the file name.

    Or, create another collection that maps generated file names to the GridFS structure to whatever it is that you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: I have removed all required error checking in the following snippet. ... void
Currently I have some code like (condensed and removed a bunch of error checking):
I have the following code which we use to compress Strings (with error and
I have the following code if (msg.position == 0) //removed for brevity else if
I have the following code : var Modal = Class.extend({ //--- Default options defaults:
I have the following code which is used to load video information from YouTube
Have the following code which (resides in a dialog box) and serves as an
i have a mongodb database connected to a node.js app via mongodb-native-drivers. I am
I have drawn up the following PHP code which processes the data from a
I have the following code (I simplified it & removed unrelevant parts) public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.