Reputation: 103
I am writing a ruby(v. 2.3.0) script to import JSON file into MongoDB(v. 3.0.8). I tried three ways to insert JSON hash into one of the collections on MongoDB, but all the attempts got errors.
1)
Code:
coll = db.collections(my_collection)
coll.insert_one(json_hash)
Error:
$oid is not valid for storage. (52) (Mongo::Error::OperationFailure)
2) Just used insert_many
, instead of insert_one
.
Code:
coll = db.collections(my_collection)
coll.insert_many(json_hash)
Error:
Invalid document format for bulk insert_one operation: ["_id", {"$oid"=>"5786e89f536a733ef63c58e0"}]. (Mongo::Error::InvalidBulkOperation)
3) As I have seen these two errors, I thought the problem was with "$oid"
. So, I removed "$oid"
from the value that the key is "_id"
in the JSON file.
Code:
new_hash = {}
object.each do |obj|
obj.each do |key, value|
if key == "_id"
value = value["$oid"]
end
new_hash.store(key, value.to_s)
end
end
However, after trying to insert ["_id", "5786e89f536a733ef63c58e0"]
, I got
Error:
Mongo::Error::BulkWriteError (Mongo::Error::BulkWriteError)
How could I pass those errors?
Thank you in advance.
Upvotes: 1
Views: 923
Reputation: 103
The error was caused by this line.
coll.insert_many(json_hash)
json_hash
couldn't be inserted in database directly. It should be wrriten in the given format: Mongo::Collection#insert_one.
Finally, I changed the code like,
object.each do |row|
coll.insert_one({name: row[0], lat: row[1], lng: row[2], length: row[3]})
end
It worked perfectly.
Upvotes: 1