MongoDB comes with “com.mongodb.util.JSON” class to convert JSON data directly to a DBObject. For example, data represent in JSON format :
{ 'name' : 'mkyong', 'age' : 30 }
To convert it to DBObject, you can code like this :
DBObject dbObject = (DBObject) JSON.parse("{'name':'mkyong', 'age':30}");
Example
See a full example to convert above JSON data to a DBObject, and save it into MongoDB.
package com.mkyong.core; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; import com.mongodb.util.JSON; /** * Java MongoDB : Convert JSON data to DBObject * */ public class App { public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("yourdb"); DBCollection collection = db.getCollection("dummyColl"); // convert JSON to DBObject directly DBObject dbObject = (DBObject) JSON .parse("{'name':'mkyong', 'age':30}"); collection.insert(dbObject); DBCursor cursorDoc = collection.find(); while (cursorDoc.hasNext()) { System.out.println(cursorDoc.next()); } System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } }
Output
{ "_id" : { "$oid" : "4dc9ebb5237f275c2fe4959f"} , "name" : "mkyong" , "age" : 30} Done
相关推荐
Json-to-MongoDB:将JSON发送到mongodb的简短脚本
Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar
MongoDB权威指南:MongoDB:The Definitive Guide第一版 第二版 中、英文4本合集
深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集
下面小编就为大家带来一篇java操作mongodb时,对象bean和DBObject相互转换的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
MongoDB: The Definitive Guide MongoDB is a powerful, flexible, and scalable generalpurpose database. It combines the ability to scale out with features such as secondary indexes, range queries, ...
sql到mongodb 将SQL表转换为MongoDB集合的Java工具 轻松将 MSSQL 表转换为 MongoDB。 从下载 SQL Server JDBC 驱动程序 将 SQLJDBC... MSSql_Table_To_MongoDB_Collection mssql_table_name number_of_rows_at_a_time
Spring Data MongoDB API。 Spring Data MongoDB 开发文档。
首先,将存储库克隆到新目录中: git clone https://github.com/drozdowo/bulk-json-to-mongodb.git然后,导航至该目录并运行: npm install安装依赖项。 然后,删除./json目录中的“ DELETEME.json”文件,并将其...
mongodb 命令行mongod启动报错 about to fork child process, waiting until server is ready for connections. forked process: 3560 ERROR: child process failed, exited with error number 1 To see additional ...
利用MongoDB与Jackson JSON框架结合实现快速开发应用接口的例子,详情参见文章:http://blog.csdn.net/chaijunkun/article/details/7263804。
MongoDB 是一个可扩展的、高性能的、开源的NoSQL数据库,跟传统的数据库不一样,MongoDB并不是将数据存储在表中,他将数据结构化为一个类似于JSON的文档中。这篇文章就是展示如何使用Java基于MongoDB和Spring Data...
NoSQL and MongoDB in the following few steps: 1: Introduction to Big Data and NoSQL 2: Introduction to MongoDB 3: Understand MongoDB Data Model 4: Getting Started with Installation and coding with ...
The Definitive Guide to MongoDB: A complete guide to dealing with Big Data using MongoDB The Definitive Guide to MongoDB, Third Edition, is updated for MongoDB 3 and includes all of the latest ...
Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for ...
NULL 博文链接:https://study121007.iteye.com/blog/2307592
Manage your data in a database system designed to support modern application development. The updated edition of this authoritative and accessible guide shows you the many advantages of using document...
spring-data-mongodb1.1.0.jar包
6. MongoDB: A Document Store 7. Neo4j: A Graph Database 8. Redis: A Key/Value Store Part IV. Rapid Application Development 9. Persistence Layers with Spring Roo 10. REST Repository Exporter Part V. ...
Java 解析xml 存入mongodb 返回org.json json包