Created
July 27, 2014 13:04
-
-
Save honux77/cfe53b21ecb15990062b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import java.net.UnknownHostException; | |
| import java.util.Set; | |
| import com.mongodb.BasicDBObject; | |
| import com.mongodb.DBCollection; | |
| import com.mongodb.DBCursor; | |
| import com.mongodb.DBObject; | |
| import com.mongodb.MongoClient; | |
| import com.mongodb.DB; | |
| public class Test { | |
| public static void main(String[] args) throws UnknownHostException { | |
| System.out.println("mongoDB test"); | |
| MongoClient mongoClient = new MongoClient( | |
| "192.168.56.101", 27017); | |
| DB db = mongoClient.getDB("popidb"); | |
| //get list collection | |
| Set<String> colls = db.getCollectionNames(); | |
| for(String s: colls) { | |
| System.out.println(s); | |
| } | |
| //get collection | |
| DBCollection coll = db.getCollection("memo"); | |
| //inserting a document | |
| BasicDBObject doc = new BasicDBObject("name", "mongoDB"); | |
| doc.append("no", 1); | |
| BasicDBObject body = new BasicDBObject("title", "Hello"); | |
| body.append("main", "Weather is very nice!"); | |
| doc.append("text", body); | |
| coll.insert(doc); | |
| System.out.println("find one\n\n"); | |
| //findone | |
| DBObject one = coll.findOne(); | |
| System.out.println(one); | |
| System.out.println("find all\n\n"); | |
| //findall | |
| DBCursor cursor = coll.find(); | |
| while(cursor.hasNext()) { | |
| System.out.println(cursor.next()); | |
| } | |
| //cursor.close(); | |
| System.out.println("find some\n\n"); | |
| //find some | |
| BasicDBObject temp = new BasicDBObject("$regex","M.*"); | |
| cursor = coll.find(new BasicDBObject("name", temp)); | |
| while(cursor.hasNext()) { | |
| System.out.println(cursor.next()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment