Created
August 8, 2014 17:43
-
-
Save czxttkl/10ad7c6224c5f66caca1 to your computer and use it in GitHub Desktop.
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
import java.net.UnknownHostException; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.DBCursor; | |
import com.mongodb.MongoClient; | |
/** | |
* @author Zhengxing Chen | |
* | |
* Test to connect to mongodb and print all documents in metadata.papers | |
*/ | |
public class MongoDB { | |
/** | |
* @param args | |
* @throws UnknownHostException | |
*/ | |
public static void main(String[] args) throws UnknownHostException { | |
// The server is running on 129.10.76.16:27017 | |
MongoClient mongoClient = new MongoClient("129.10.76.16", 27017); | |
// The database name is "metadata" | |
DB db = mongoClient.getDB("metadata"); | |
// The collection name is "papers" | |
DBCollection papersColl = db.getCollection("papers"); | |
// Get the cursor of "papers" | |
DBCursor papersCursor = papersColl.find(); | |
while (papersCursor.hasNext()) { | |
System.out.println(papersCursor.next()); | |
} | |
// Close the cursor | |
papersCursor.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment