JSON turn Bson
MongoDB is stored in the Bson data format, the JSON string has no way to write directly to MongoDB can convert the JSON string into DBObject or document, and then write to MongoDB
1. Convert the JSON character to Com.mongodb.DBObject (exactly basicdbobject) Scala version
Import Com.mongodb.DBObjectimport Com.mongodb.casbah. {mongoclient, Mongocollection}import Com.mongodb.util.JSON//constructs a JSON stringVal JSON = s"""{|"School_code":"${school_code}", |"School_name":"${school_name}", |"Teacher_idcard":"${teacher_idcard}", |"Teacher_name":"${teacher_name}"|} |""". StripmarginVal collection:mongocollection= Mongoclient ("10.4.120.83")("DbName")("CollectionName") Val Bson:dbobject=Json.parse (JSON). Asinstanceof[dbobject]collection.insert (Bson)//the notation of MongoDB Casbah
Java version
Import Com.mongodb.mongoclient;import Com.mongodb.dbobject;import com.mongodb.client.mongocollection;import Com.mongodb.client.mongodatabase;import Com.mongodb.util.JSON;//constructs a JSON stringString JSON =" {"+"' school_code ': ' 111111 ',"+"' school_name ': ' Handong University of Political Science ',"+"' teacher_idcard ': ' 0000001 ',"+"' teacher_name ': ' Gao Yu Liang '"+" } "; Mongoclient mongoclient=NewMongoclient ("10.4.120.83",27017); Mongodatabase Database= Mongoclient.getdatabase ("DbName"); Mongocollection<DBObject> collection = Database.getcollection ("CollectionName", DBObject.class); DBObject Bson=(DBObject) json.parse (JSON); Collection.insertone (Bson);
2. Convert string to Org.bson.Documentscala version
Import Org.bson.Documentimport Com.mongodb.casbah. {mongoclient, mongocollection}import Com.mongodb.util.JSON;//constructs a JSON stringVal JSON = s"""{|"School_code":"${school_code}", |"School_name":"${school_name}", |"Teacher_idcard":"${teacher_idcard}", |"Teacher_name":"${teacher_name}"|} |""". StripmarginVal document:document=Document.parse (JSON)//Watch out! Com.mongodb.casbah.MongoCollection only supports writing dbobject subclasses,//objects written to the document class are not supported and can be used com.mongodb.client.MongoCollection//writes the object of the document class, where it can be written because a custom implicit conversion function is used to//document converted into a dbobject//Custom implicit conversion functionsImplicitdef document2dbobject (doc:document): DBObject =Json.parse (Doc.tojson). Asinstanceof[dbobject]val collection:mongocollection= Mongoclient ("10.4.120.83")("DbName")("CollectionName") Collection.insert (document)
Java version
Import Org.bson.document;import Com.mongodb.mongoclient;import com.mongodb.client.mongocollection;import Com.mongodb.client.MongoDatabase;//constructs a JSON stringString JSON =" {"+"' school_code ': ' 111111 ',"+"' school_name ': ' Handong University of Political Science ',"+"' teacher_idcard ': ' 0000001 ',"+"' teacher_name ': ' Gao Yu Liang '"+" } "; Mongoclient mongoclient=NewMongoclient ("10.4.120.83",27017); Mongodatabase Database= Mongoclient.getdatabase ("DbName"); Mongocollection<Document> collection = Database.getcollection ("CollectionName"); Document Document=Document.parse (JSON); Collection.insertone (Document);
MongoDB method of writing JSON data directly to MongoDB