Java operations on MongoDB instances

Source: Internet
Author: User

I finally ran my first Java MongoDB operation example. I don't know why MongoDB will always be locked every time I start the machine. I need to delete the lock file to start it, is it related to your own Windows 7 system? Puzzling!

  1. PackageMongodb;
  2. ImportJava.net. UnknownHostException;
  3. ImportCom. mongodb. BasicDBObject;
  4. ImportCom. mongodb. DB;
  5. ImportCom. mongodb. DBCollection;
  6. ImportCom. mongodb. DBCursor;
  7. ImportCom. mongodb. Mongo;
  8. ImportCom. mongodb. MongoException;
  9. /** 
  10. * Java + MongoDB Hello world Example 
  11. */
  12. Public ClassMongoDb {
  13. Public Static VoidMain (String [] args ){
  14. Try{
  15. // Instantiate a Mongo object and connect to port 27017
  16. Mongo mongo =NewMongo ("Localhost",27017);
  17. // Connect to the database named xiaodb. If the database does not exist, mongodb will automatically create
  18. DB db = mongo. getDB ("Xiaodb");
  19. // Get collection from MongoDB, database named "xiao"
  20. // Obtain the data set named users from Mongodb. If the data set does not exist, Mongodb creates a new
  21. DBCollection collection = db. getCollection ("Users");
  22. // Use the BasicDBObject object to create a document for mongodb and assign a value to it.
  23. BasicDBObject document =NewBasicDBObject ();
  24. Document. put ("Id",1001);
  25. Document. put ("Msg","Hello world mongoDB in Java");
  26. // Save the newly created document to the collection.
  27. Collection. insert (document );
  28. // Create the document to query
  29. BasicDBObject searchQuery =NewBasicDBObject ();
  30. SearchQuery. put ("Id",1002);
  31. // Use the find method of collection to find the document
  32. DBCursor cursor = collection. find (searchQuery );
  33. // Cyclically output results
  34. While(Cursor. hasNext ()){
  35. System. out. println (cursor. next ());
  36. }
  37. System. out. println ("Done");
  38. }Catch(UnknownHostException e ){
  39. E. printStackTrace ();
  40. }Catch(Except exception e ){
  41. E. printStackTrace ();
  42. }
  43. }
  44. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.