带有字符串_id和upsert的Mongoimport CSV文件 [英] Mongoimport csv files with string _id and upsert
问题描述
我正在尝试使用mongoimport来更新_id中具有字符串值的数据. 由于id看起来像整数(即使它们用引号引起来),因此mongoimport会将其视为整数并创建新记录,而不是升序处理现有记录.
I'm trying to use mongoimport to upsert data with string values in _id. Since the ids look like integers (even though they're in quotes), mongoimport treats them as integers and creates new records instead of upserting the existing records.
我正在运行的命令:
mongoimport --host localhost --db database --collection my_collection --type csv --file mydata.csv --headerline --upsert
mydata.csv中的示例数据:
Example data in mydata.csv:
{ "_id" : "0364", someField: "value" }
结果是mongo插入这样的记录:{ "_id" : 364, someField: "value" }
而不是使用_id "0364"
更新记录.
The result would be for mongo to insert a record like this: { "_id" : 364, someField: "value" }
instead of updating the record with _id "0364"
.
有人知道如何使_id
视为字符串吗?
Does anyone know how to make it treat the _id
as strings?
不起作用的东西:
- 使用双双引号" 0364",双和单引号'0364'"或'"0364"'包围数据
- 将空字符串附加到值:
{ "_id" : "0364" + "", someField: "value" }
推荐答案
不幸的是,现在还没有一种方法可以将类似数字的字符串解释为字符串:
Unfortunately there is not now a way to force number-like strings to be interpreted as strings:
https://jira.mongodb.org/browse/SERVER-3731
您可以使用Python或其他您喜欢的语言编写脚本,如下所示:
You could write a script in Python or some other language with which you're comfortable, along the lines of:
import csv, pymongo
connection = pymongo.Connection()
collection = connection.mydatabase.mycollection
reader = csv.DictReader(open('myfile.csv'))
for line in reader:
print '_id', line['_id']
upsert_fields = {
'_id': line['_id'],
'my_other_upsert_field': line['my_other_upsert_field']}
collection.update(upsert_fields, line, upsert=True, safe=True)
这篇关于带有字符串_id和upsert的Mongoimport CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!