java操作mysql数据库的代码我们已经了如指掌了,增删改查,java对mongodb数据库也是类似的操作,先是数据库连接,再是进行操作。
首先我们进入进入admin数据库,然后建立自己的数据库testMongoDb,进入admin数据库后,就可以直接进入testMongoDb,因为用户可以进入系统的数据库,就是超级管理员,use testMongoDb后,为该数据库设置用户名和密码,db.addUser('root','root'),这样我们在程序中连该数据库,并实现增删改查,代码如下所示。
代码如下所示:
复制代码 代码如下:
package com.mkyong.core;
import java.net.UnknownHostException;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* Java + MongoDB Hello world Example
*
*/
public class App {
public static void main(String[] args) {
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
//MongoClient mongo = new MongoClient("localhost", 27017);
Mongo mongo = new Mongo("127.0.0.1",27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("testMongoDb");
//database username root and password root
boolean ok = db.authenticate("root","root".toCharArray());
if(ok){
System.out.println("db connection success!");
}{
System.out.println("db connection fail !");
}
/**** Get collection / table from 'testMongoDb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("user");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("name", "mkyong");
document.put("age", 30);
document.put("createdDate", new Date());
table.insert(document);
/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "mkyong");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/**** Update ****/
// search document where name="mkyong" and update it with new values
BasicDBObject query = new BasicDBObject();
query.put("name", "mkyong");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "mkyong-updated");
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
table.update(query, updateObj);
/**** Find and display ****/
BasicDBObject searchQuery2
= new BasicDBObject().append("name", "mkyong-updated");
DBCursor cursor2 = table.find(searchQuery2);
while (cursor2.hasNext()) {
System.out.println(cursor2.next());
}
/**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
控制台输入结果如下:
复制代码 代码如下:
db connection success!
db connection fail !
{ "_id" : { "$oid" : "544073c4d58dfa6e469555ba"} , "name" : "mkyong" , "age" : 30 , "createdDate" : { "$date" : "2014-10-17T01:41:24.479Z"}}
{ "_id" : { "$oid" : "543e154bd58d704982fd38f0"} , "name" : "mkyong-updated" , "age" : 30 , "createdDate" : { "$date" : "2014-10-15T06:33:47.321Z"}}
{ "_id" : { "$oid" : "5440719dd58d08a207605c8e"} , "name" : "mkyong-updated" , "age" : 30 , "createdDate" : { "$date" : "2014-10-17T01:32:13.922Z"}}
{ "_id" : { "$oid" : "544073c4d58dfa6e469555ba"} , "name" : "mkyong-updated" , "age" : 30 , "createdDate" : { "$date" : "2014-10-17T01:41:24.479Z"}}
Done
源码下载:http://xiazai.jb51.net/201503/other/mongodb_helloworld.zip
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]