diff options
-rw-r--r-- | tools/files.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/tools/files.cpp b/tools/files.cpp index b35416bd591..e69a0701583 100644 --- a/tools/files.cpp +++ b/tools/files.cpp @@ -37,6 +37,7 @@ public: add_options() ( "local,l", po::value<string>(), "local filename for put|get (default is to use the same name as 'gridfs filename')") ( "type,t", po::value<string>(), "MIME type for put (default is to omit)") + ( "replace,r", "Remove other files with same name after PUT") ; add_hidden_options() ( "command" , po::value<string>() , "command (list|search|put|get)" ) @@ -124,7 +125,20 @@ public: const string& infile = getParam("local", filename); const string& type = getParam("type", ""); - cout << "file object: " << g.storeFile(infile, filename, type) << endl; + BSONObj file = g.storeFile(infile, filename, type); + cout << "added file: " << file << endl; + + if (hasParam("replace")){ + auto_ptr<DBClientCursor> cursor = conn().query(_db+".fs.files", BSON("filename" << filename << "_id" << NE << file["_id"] )); + while (cursor->more()){ + BSONObj o = cursor->nextSafe(); + conn().remove(_db+".fs.files", BSON("_id" << o["_id"])); + conn().remove(_db+".fs.chunks", BSON("_id" << o["_id"])); + cout << "removed file: " << o << endl; + } + + } + cout << "done!"; return 0; } |