Mongoimport json 文件更新或覆盖..?
Mongoimport json file update or overwritte ..?
我有名称为 "Project" 的数据库和名称为 "sample" 的集合,然后我使用 mongoimport
命令插入了一个 JSON 文件。
现在我编辑了同一个 JSON 文件。因此,如果想将相同的 JSON 文件导入到集合中,那么我将面临创建多个实例并且未进行任何更新的问题。
是否有任何方法可以使用 mongoimport
命令更新或覆盖 mongodb 中已经存在的数据?
请注意,我也尝试使用 --mode=upsert
标志:
./mongoimport --db Project --collection sample --mode=upsert --file /home/rule.json
默认行为表示如果已经存在则跳过
所以默认情况下它不会覆盖现有数据。
但您可以使用 --upsert
标志更新它。
对于MongoDB v3.x,
--mode=upsert
--drop 标志也可以与 mongoimport 命令一起用于 overwrite/update 现有数据。
--drop
./mongoimport --db Project --collection sample --drop --file /home/UCSC_rule.json
我给出这个解决方案是因为我尝试使用 --upsert 标志,但我看不到现有数据的任何变化,而是创建了新实例。
根据 mongo 文档,--mode=upsert 在您的情况下不起作用的原因是默认情况下,mongoimport 使用 _id 字段。所以 --drop 应该是正确答案。
--模式=upsert:
By default, mongoimport uses the _id field to match documents in the
collection with documents in the import file. To specify the fields
against which to match existing documents for the upsert and merge
modes, use --upsertFields.
--掉落:
Modifies the import process so that the target instance drops the
collection before importing the data from the input.
如果 _id 不匹配,--mode upsert
将创建一个新记录。 mongodump/mongorestore
将这些 _id 保存在不同机器上的数据库中
--drop
将删除整个 collection!
要覆盖一个或多个记录而不删除其他现有记录,请确保在使用 --upsert
时 ID 匹配
使用 MongoDb 版本 3.6.8 以下解决方案对我有效
mongoimport --db <db name> --collection <collection name> --upsert --upsertFields <field name> --file <path to json file> --jsonArray
其中“字段名称”是 MongoDb 用于比较的键的名称,而不是默认字段“_id”。
我有名称为 "Project" 的数据库和名称为 "sample" 的集合,然后我使用 mongoimport
命令插入了一个 JSON 文件。
现在我编辑了同一个 JSON 文件。因此,如果想将相同的 JSON 文件导入到集合中,那么我将面临创建多个实例并且未进行任何更新的问题。
是否有任何方法可以使用 mongoimport
命令更新或覆盖 mongodb 中已经存在的数据?
请注意,我也尝试使用 --mode=upsert
标志:
./mongoimport --db Project --collection sample --mode=upsert --file /home/rule.json
默认行为表示如果已经存在则跳过 所以默认情况下它不会覆盖现有数据。
但您可以使用 --upsert
标志更新它。
对于MongoDB v3.x,
--mode=upsert
--drop 标志也可以与 mongoimport 命令一起用于 overwrite/update 现有数据。
--drop
./mongoimport --db Project --collection sample --drop --file /home/UCSC_rule.json
我给出这个解决方案是因为我尝试使用 --upsert 标志,但我看不到现有数据的任何变化,而是创建了新实例。
根据 mongo 文档,--mode=upsert 在您的情况下不起作用的原因是默认情况下,mongoimport 使用 _id 字段。所以 --drop 应该是正确答案。
--模式=upsert:
By default, mongoimport uses the _id field to match documents in the collection with documents in the import file. To specify the fields against which to match existing documents for the upsert and merge modes, use --upsertFields.
--掉落:
Modifies the import process so that the target instance drops the collection before importing the data from the input.
--mode upsert
将创建一个新记录。 mongodump/mongorestore
--drop
将删除整个 collection!
要覆盖一个或多个记录而不删除其他现有记录,请确保在使用 --upsert
使用 MongoDb 版本 3.6.8 以下解决方案对我有效
mongoimport --db <db name> --collection <collection name> --upsert --upsertFields <field name> --file <path to json file> --jsonArray
其中“字段名称”是 MongoDb 用于比较的键的名称,而不是默认字段“_id”。