https://github.com/bertanasco/HelloMongoDB
pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mobileTAO.mongoDB.hello; | |
import com.mongodb.Mongo; | |
import com.mongodb.MongoException; | |
import com.mongodb.WriteConcern; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.DBObject; | |
public class HelloMongoDB { | |
public static void main(String [] args){ | |
//create mongoDB connection | |
try { | |
Mongo mongoConnection = new Mongo("localhost", 27017); // default port | |
//get database. if database does not exist | |
//mongoDB would automatically create one for you | |
DB mongoDB = mongoConnection.getDB("mongoDB"); | |
//authentication -- optional | |
//boolean auth = mongoDB.authenticate(username, password); | |
DBCollection collection = mongoDB.getCollection("testmongoDB"); | |
//create document | |
BasicDBObject document = new BasicDBObject(); | |
document.put("name", "dilasasiko"); | |
document.put("message", "helloMongoDB"); | |
//insert document to collection | |
collection.insert(document); | |
mongoConnection.setWriteConcern(WriteConcern.SAFE); | |
//query for the inserted document | |
DBObject findOneResult = collection.findOne(); | |
System.out.println(findOneResult); | |
} | |
catch (MongoException mongoexcp){ | |
mongoexcp.printStackTrace(); | |
} | |
catch (Exception e){ | |
e.printStackTrace(); | |
} | |
System.out.println(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>MongoDB</groupId> | |
<artifactId>MongoDB</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>MongoDB</name> | |
<description>MongoDB</description> | |
<dependencies> | |
<dependency> | |
<groupId>org.mongodb</groupId> | |
<artifactId>mongo-java-driver</artifactId> | |
<version>2.9.1</version> | |
</dependency> | |
</dependencies> | |
<repositories> | |
<repository> | |
<url>http://repo1.maven.org/maven2 </url> | |
<id>mvn repository</id> | |
</repository> | |
</repositories> | |
</project> |
Output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ "_id" : { "$oid" : "50aa73d6520f1e9b409c2eff"} , "name" : "dilasasiko" , "message" : "helloMongDB"} |
Reference
http://www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-MakingAConnection
Happy coding :D Zzzzzzzz
2 comments:
OMG this is so cool!!!
spammer! :D
Post a Comment