Python 将JSON导入Mongo,并将_id作为JSON的文件名?
问题:Python 将JSON导入Mongo,并将_id作为JSON的文件名?,python,json,python-3.x,mongodb,pymongo,Python,Json,Python 3.x,Mongodb,Pymongo,问题: mongoimport --db dbName --collection myCollection --file 00329f9608793cdf176032630ebc42ef.json --jsonArray 我有一大堆json文件,格式有点像这样 cat 00329f9608793cdf176032630ebc42ef.json { "engine": {}, "engine1": "not_found", "engine2": "not_found", "eng
mongoimport --db dbName --collection myCollection --file 00329f9608793cdf176032630ebc42ef.json --jsonArray
我有一大堆json文件,格式有点像这样
cat 00329f9608793cdf176032630ebc42ef.json
{
"engine": {},
"engine1": "not_found",
"engine2": "not_found",
"engine3": "not_found",
"engine4": "not_found",
"engine5": "not_found"
}
现在我已经创建了一个文件夹,其中没有大量这种格式的json文件。现在我想将每个json导入我创建的mongo集合中,当我尝试插入json文件时,_id是用随机散列创建的,这不是预期的行为,我想要的是获取json的文件名并用文件名创建_id
假设应该采用00329f9608793cdf176032630ebc42ef.json,并且应该像00329f9608793cdf176032630ebc42ef那样创建id
我的尝试:
mongoimport --db dbName --collection myCollection --file 00329f9608793cdf176032630ebc42ef.json --jsonArray
需要什么?
有没有办法处理我的情况,文件名应该存储为_id,而不是任何随机散列。将以下键值映射添加到原始json文件可能是正确的方向
"_id": {"$oid":"your_filename_here"}