MongoDB method of writing JSON data directly to MongoDB

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.