package Mongo;import Java.net.unknownhostexception;import Com.mongodb.basicdbobject;import Com.mongodb.db;import Com.mongodb.dbcollection;import com.mongodb.DBCursor; Import Com.mongodb.mongo;import Com.mongodb.mongoexception;public class Connection {public static void main (string[] args) {Try{mongo m = new Mongo ("localhost", 27017);D b DB = M.getdb ("Company");D bcollection collection = Db.getcollection (" Employees "); Basicdbobject employee = new Basicdbobject (), Employee.put ("name", "Hannah"), Employee.put ("No", 2); Collection.insert ( Employee); Basicdbobject Searchemployee = new Basicdbobject () searchemployee.put ("No", 2);D bcursor cursor = Collection.find ( Searchemployee); while (Cursor.hasnext ()) {System.out.println (Cursor.next ());} System.out.println ("The Search Query has executed!"); }catch (unknownhostexception e) {e.printstacktrace ();} catch (Mongoexception e) {e.printstacktrace ();}}}
{"_id": {"$oid": "5454fa4458404d12637d418c"}, "name": "Hannah", "No": 2}
The Search Query has Executed !
Import java.net.UnknownHostException; Import Java.util.Set; Import Com.mongodb.BasicDBObject; Import Com.mongodb.DB; Import com.mongodb.DBCollection; Import Com.mongodb.DBCursor; Import Com.mongodb.DBObject; Import Com.mongodb.Mongo; Import com.mongodb.MongoException; public class Test {/** * @author Gaogao * @param args * @throws mongoexception * @throws unkno Wnhostexception */public static void main (string[] args) throws Unknownhostexception, Mongoexcepti on {//TODO auto-generated Method Stub//connect local database Mongo m = new Mongo (); Create a database named new_test_db db db = M.getdb ("new_test_db"); Gets the collection in new_test_db (similar to getting a table in a relational database) set<string> cols = Db.getcollectionnames (); Prints out the collection in new_test_db, which should be null for (String s:cols) {System.out.println (s); }//Create a collection called "New_test_col" dbcollection collection = DB.GEtcollection ("New_test_col"); Initializes a base DB object, which is the db object Basicdbobject obj = new Basicdbobject (), which is eventually inserted into the database; Put several key values on the Obj.put ("from", "engage"); Obj.put ("To", "Baby"); Obj.put ("Subject", "The son of the dog loves Bao Zi"); Insert Object Collection.insert (obj); View a record, FindOne () =find (). limit (1); DBObject Dbobj=collection.findone (); Print out the data you just inserted System.out.println (dbobj); Now let's insert the data for the 9 {ranking:i} for (int i=0;i<9;i++) {Collection.insert (New Basicdbobject (). Append ("ran King ", I)); }//Prints the total number of data in the collection, which should be output at System.out.println (Collection.getcount ()); Let's walk through the collection, and the Find () method returns a cursor (the cursor), where the concept and relational database are similar dbcursor cursor=collection.find (); We then use this cursor to traverse the set while (Cursor.hasnext ()) {System.out.println (Cursor.next ())); }//Take a look at some of the slightly more complex query techniques, the first, simple conditional query, query ranking for 1 records basicdbobject Query=new BasicdbobjeCT (); Query.put ("ranking", 1); Cursor=collection.find (query); while (Cursor.hasnext ()) {System.out.println (Cursor.next ()); }//Below is a more complex conditional query that queries ranking greater than 5 less than 9 Records query=new Basicdbobject (); Query.put ("ranking", New Basicdbobject ("$gt", 5). Append ("$lt", 9)); Cursor=collection.find (query); while (Cursor.hasnext ()) {System.out.println (Cursor.next ()); }//finally delete our test database m.dropdatabase ("new_test_db"); } }
MongoDB with Java implementation of additions and deletions to search