zoukankan      html  css  js  c++  java
  • MongoDB 将Json数据直接写入MongoDB的方法

    Json转Bson

    MongoDB中是以Bson数据格式进行存储的,Json字符串没有办法直接写入MongoDB 可以将Json字符串转换成DBObject或者Document,然后写入MongoDB

    1、将Json字符转换成com.mongodb.DBObject(准确的说是BasicDBObject)

    scala版本

    import com.mongodb.DBObject
    import com.mongodb.casbah.{MongoClient, MongoCollection}
    import com.mongodb.util.JSON
    
    // 构造一个Json字符串
    val json = s"""{
               |  "school_code" : "${school_code}",
               |  "school_name" : "${school_name}",
               |  "teacher_idcard" : "${teacher_idcard}",
               |  "teacher_name" : "${teacher_name}"
               |}
               |""".stripMargin
    
    val collection: MongoCollection = MongoClient("10.4.120.83")("dbName")("collectionName")
    
    val bson: DBObject = JSON.parse(json).asInstanceOf[DBObject]
    
    collection.insert(bson) // mongodb casbah的写法

    java版本

    import com.mongodb.MongoClient;
    import com.mongodb.DBObject;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoDatabase;
    import com.mongodb.util.JSON;
    
    
    // 构造一个Json字符串
    String json = " {" +
                    " 'school_code' : '111111', " +
                    " 'school_name' : '汉东政法大学', " +
                    " 'teacher_idcard' : '0000001', " +
                    " 'teacher_name' : '高育良' " +
                    " } ";
    
    MongoClient mongoClient = new MongoClient("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、将字符串转换成org.bson.Document

    scala版本

    import org.bson.Document
    import com.mongodb.casbah.{MongoClient, MongoCollection}
    import com.mongodb.util.JSON;
    
    // 构造一个Json字符串
    val json = s"""{
               |  "school_code" : "${school_code}",
               |  "school_name" : "${school_name}",
               |  "teacher_idcard" : "${teacher_idcard}",
               |  "teacher_name" : "${teacher_name}"
               |}
               |""".stripMargin
    
    
    val document:Document = Document.parse(json)
    
    // 注意!com.mongodb.casbah.MongoCollection只支持写DBObject的子类,
    // 不支持写入Document类的对象,可以使用com.mongodb.client.MongoCollection
    // 写入Document类的对象,这里能写入是因为用了自定义的隐式转换函数,将
    // Document转换成了DBObject
    
    // 自定义的隐式转换函数
    implicit def document2DBObject(doc: Document): DBObject = JSON.parse(doc.toJson).asInstanceOf[DBObject]
    
    val collection: MongoCollection = MongoClient("10.4.120.83")("dbName")("collectionName")
    
    collection.insert(document)

    Java版本

    import org.bson.Document;
    import com.mongodb.MongoClient;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoDatabase;
    
    // 构造一个Json字符串
    String json = " {" +
                    " 'school_code' : '111111', " +
                    " 'school_name' : '汉东政法大学', " +
                    " 'teacher_idcard' : '0000001', " +
                    " 'teacher_name' : '高育良' " +
                    " } ";
    
    MongoClient mongoClient = new MongoClient("10.4.120.83", 27017);
    
    MongoDatabase database = mongoClient.getDatabase("dbName");
    
    MongoCollection<Document> collection = database.getCollection("collectionName");  
    
    Document document = Document.parse(json);
    
    collection.insertOne(document );
  • 相关阅读:
    POJ 3263 Tallest Cow(线段树)
    面试题54——表示数值的字符串
    ORA-01555错误总结(二)
    《AndroidStudio每日一贴》11. 重构之提炼为常量
    机器学习系统构建
    jsp不通过form和Ajax提交
    南邮算法分析与设计实验2 动态规划法
    cocos2d-x 3.2 之 2048 —— 第五篇
    Cocos2d-x学习资源集锦+有奖抽楼活动
    VMware虚拟机无法识别U盘解决方式
  • 原文地址:https://www.cnblogs.com/rinack/p/7155175.html
Copyright © 2011-2022 走看看