mongo:使用终端删除集合

Jea*_*eri 5 bash command-line-interface mongodb

我想将数据导入几个集合.这就是我的工作

$> mongoimport -d myDB -c myCollection --file file.json       
Run Code Online (Sandbox Code Playgroud)

问题是,这只有在集合为空时才有效,否则会出现错误

Mon Mar 31 10:45:52.696 E11000 duplicate key error index:.....
Run Code Online (Sandbox Code Playgroud)

因为我想在bash脚本中执行此操作,所以我需要先清除/删除集合.我怎样才能在bash脚本中执行此操作?

Nei*_*unn 9

直接从手册页面,使用--drop选项:

- 下降

修改导入过程,以便在从输入导入集合之前,目标实例删除每个集合.

因此,您可以删除要导入的集合并开始新的集合.

mongoimport -d myDB -c myCollection --drop --file file.json  
Run Code Online (Sandbox Code Playgroud)


mad*_*mxg 8

db.collection.drop()在集合上调用方法以将其从数据库中删除。

在bash中使用 mongo <dbname> --eval "db.<collectionName>.drop()"