折腾:
【已解决】阿里云ECS服务器中已有的MongoDB的用户名密码和端口
期间,需要去在mongo命令行中去试试删除gridfs中的一个文件
之前在:
是mongofiles中删除
而在
【已解决】把本地的音频字幕等数据存储到本地MongoDB数据库中
是通过API去删除
都不是
mongo命令行中的删除
参考:
Delete Documents — MongoDB Manual 3.6
和自己摸索,搞了半天,都没删除掉:
<code>> db.fs.files.find().pretty() { "_id" : ObjectId("5ac193a057511d1db1314790"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-04-02T02:21:20.411Z"), "length" : 1494, "md5" : "6f154eaf54c31bf3c9baf99b7495da97", "filename" : "mp3.py" } > db.fs.files.remove({"id" : "5ac193a057511d1db1314790"}) WriteResult({ "nRemoved" : 0 }) > db.fs.files.remove({"id" : 'ObjectId("5ac193a057511d1db1314790")'}) WriteResult({ "nRemoved" : 0 }) > db.fs.files.remove({"_id" : 'ObjectId("5ac193a057511d1db1314790")'}) WriteResult({ "nRemoved" : 0 }) > df.fs.files.deleteOne({"_id" : 'ObjectId("5ac193a057511d1db1314790")'}) 2018-04-02T11:21:24.447+0800 E QUERY [thread1] ReferenceError: df is not defined : @(shell):1:1 > db.fs.files.deleteOne({"_id" : 'ObjectId("5ac193a057511d1db1314790")'}) { "acknowledged" : true, "deletedCount" : 0 } > db.fs.files.deleteOne({"id" : 'ObjectId("5ac193a057511d1db1314790")'}) { "acknowledged" : true, "deletedCount" : 0 } > db.fs.files.find().pretty() { "_id" : ObjectId("5ac193a057511d1db1314790"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-04-02T02:21:20.411Z"), "length" : 1494, "md5" : "6f154eaf54c31bf3c9baf99b7495da97", "filename" : "mp3.py" } </code>
db.collection.remove() — MongoDB Manual 3.6
mongo gridfs delete file
mongodb – How to delete an image file from GridFS by file metadata? – Stack Overflow
gridfs – Tools for working with GridFS — PyMongo 3.6.1 documentation
api的delete
mongofiles — MongoDB Manual 3.6
mongofiles的delete
mongodb – Mongo GridFS How to delete files older than a given date? – Stack Overflow
node.js – How to remove a file from both chunks and files in GridFS? – Stack Overflow
javascript – Deleting a file from GridFS – Stack Overflow
继续试了半天,没有通过id或_id去删除掉,
最后通过filename用deleteOne删除掉了:
<code>> db.gridfs.find() > use gridfs switched to db gridfs > db.fs.files.find() { "_id" : ObjectId("5ac193a057511d1db1314790"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-04-02T02:21:20.411Z"), "length" : 1494, "md5" : "6f154eaf54c31bf3c9baf99b7495da97", "filename" : "mp3.py" } > db.fs.files.delete({"filename": "mp3.py"}) 2018-04-02T11:31:38.743+0800 E QUERY [thread1] TypeError: db.fs.files.delete is not a function : @(shell):1:1 > db.fs.files.deleteOne({"filename": "mp3.py"}) { "acknowledged" : true, "deletedCount" : 1 } > db.fs.files.find() </code>
【总结】
mongo命令行中,此处对于用id或_id去删除文件,始终没用:
<code>> db.fs.files.deleteOne({"_id" : 'ObjectId("5ac193a057511d1db1314790")'}) { "acknowledged" : true, "deletedCount" : 0 } > db.fs.files.deleteOne({"id" : 'ObjectId("5ac193a057511d1db1314790")'}) { "acknowledged" : true, "deletedCount" : 0 } </code>
最后用filename才删除掉:
<code>> db.fs.files.deleteOne({"filename": "mp3.py"}) { "acknowledged" : true, "deletedCount" : 1 } > db.fs.files.find() </code>
【后记】
折腾:
【未解决】远程MongoDB添加security的authorization连接出错:com.mongodb.MongoCommandExceptions: Command failed with error 13 not authorized on gridfs to execute command
期间,通过参考:
db.collection.deleteOne() — MongoDB Manual 3.6
折腾:
<code>> db.testOps.deleteOne() 2018-04-04T16:59:06.508+0800 E QUERY [thread1] Error: find() requires query criteria : Bulk/this.find@src/mongo/shell/bulk_api.js:786:1 DBCollection.prototype.deleteOne@src/mongo/shell/crud_api.js:351:20 @(shell):1:1 > db.testOps.find() { "_id" : ObjectId("5ac492a0b04d5e0814f6b4b7"), "testKey" : "testValue" } > db.testOps.deleteOne("_id": 'ObjectId("5ac492a0b04d5e0814f6b4b7")') 2018-04-04T17:00:04.039+0800 E QUERY [thread1] SyntaxError: missing ) after argument list @(shell):1:26 > db.testOps.deleteOne({"_id": 'ObjectId("5ac492a0b04d5e0814f6b4b7")'}) { "acknowledged" : true, "deletedCount" : 0 } > db.testOps.deleteOne({"_id": '5ac492a0b04d5e0814f6b4b7'}) { "acknowledged" : true, "deletedCount" : 0 } > db.testOps.deleteOne({"_id": ObjectId("5ac492a0b04d5e0814f6b4b7")}) { "acknowledged" : true, "deletedCount" : 1 } </code>
期间发现:
原来对于delete时,传入的参数,可以且必须直接用ObjectId的写法:
<code>{"_id": ObjectId("5ac492a0b04d5e0814f6b4b7")} </code>
才能删除掉文件。
-》说明是可以通过_id去删除文件的,只不过需要传递参数时,用:
<code>db.testOps.deleteOne({"_id": ObjectId("5ac492a0b04d5e0814f6b4b7")}) </code>
的写法才可以。
转载请注明:在路上 » 【已解决】mongo命令行中如何删除文件