如何不让“$”影响使用python或JS在MongoDB中插入文档?

2024-04-25 22:03:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python和JavaScript将数据从一个MongoDB数据库转储到另一个数据库。它将时间戳从unix更改为人类可读的形式。 数据有点像本文件:你知道吗

[{'_id': {'$oid': 'qwer'}, 'playerId': 'abc', 'amount': 150.0, 'newValue': {'$numberLong': '150'}, 'reason': 9.0, 'type': 1.0, 'ts': {'$date': {'$numberLong': '1534423854022'}}}]

我曾经评估回避“$”的问题,因为它说Nodejs和Python的开头都不应该有$

Python代码

json_data = ast.literal_eval(data)
json_dump = json.dumps(json_data)
x= loads(json_dump)
my_collection.insert_many(x)

JS代码


function callback(error, response, body) {
if (!error && response.statusCode == 200) {
dbo.collection("Player").insertMany(body,{"ordered":false},function(err, res) {
if (err) console.log(err);
console.log("All document inserted");
});

mydb.close();
}

我想用python和节点.js两者都有。我需要拉数据和输入的格式完全一样,在以前的数据库。你知道吗

错误消息是:-

Error: key $oid must not start with '$'
    at serializeInto (/Users/user/Desktop/mongo node aggregate/node_modules/bson/lib/bson/parser/serializer.js:911:19)

Tags: 数据代码数据库jsondataifresponsefunction

热门问题