I. MONGODB installation and start-up
Widndows and Linux are basically the same
1. Download the database,
Linux:mongodb-linux-x86_64-ubuntu1404-3.0.3.tgz
2. Unzip the file and place it in the appropriate location
tar-vxf mongodb-linux-x86_64-ubuntu1404-3.0.3.tgz
MV mongodb-linux-x86_64-ubuntu1404-3.0.3 /usr/local/mongodb
3. Create the necessary directories and files
Database directory:/usr/local/mongodb/data/db
Log file:/usr/local/mongodb/data/log/mongodb.log
After the end of the directory and file authorization, otherwise the service will not open up.
4. Set the database location and log location and turn on the service:
bin (Linux)->./mongod--port 27017--dbpath/usr/local/mongodb/data/db--logpath/usr/local/mongodb/data/log/ Mongodb.log
Note the file and file read/write permissions under Linux. Of course, there are a lot of open services, not listed.
bin (Win)->mongod--port 27017--dbpath "D:\Program files\mongodb\server\3.0\data\db"--logp Ath "D:\Program files\mongodb\server\3.0\data\log\mongodb.log"
5. Connect to the database:
Bin (Linux)->./mongo
Bin (Win)->mongo.exe
Note: Creating directories under Windows and log files no longer describes the operation process, very easy. Another point is that after the service is started, do not shut down the start of the service interface, or CTRL + C, when connecting to the database needs a new form. Of course, you can also let the service background execution command: Nohup./mongod--port 27017--dbpath/usr/local/mongodb/data/db--logpath/usr/local/mongodb/data/log/mongodb.log &
6. Client connection to MongoDB via graphical interface
There are more options under Windows, including Robomongo,mongovue, and so on, available under Linux Robomongo. Go directly to the official website to download and install it. Robomongo's advantage is the ability to directly in the software to knock the shell command, but also prompted; The advantage of Mongovue is that the query section is clear, without knocking the complete complex shell commands.
Other script commands for 7.mongodb
Backup: mongodump/Recovery: Mongorestore
Import data:mongoimport-d dbname-c collectionname--file FilePath; and other very many optional references
Export data: mongoexport-d dbname-c collectionname-o filepaht; There are a number of other optional references
Import and export data are JSON-formatted files by default and, of course, to accommodate data interactions with other databases. You can choose to import the exported file as a CVS file.
Two. Simple use of MongoDB
Using MongoDB for CRUD operations is through the shell statement that runs the response. So it's essentially the use of shell statements. There are several simple objects that you need to know about MongoDB: Db,collection,document.
DB: Database object, equivalent to a database object in a relational database
Collection: Collection object, equivalent to a table in a relational database
Document: Documents object. Equivalent to a row of records in a table in a relational database
1, the operation of the database:
>db;//View the database that is currently in use
>show dbs;//Show All the databases
>use dbname;//uses a database, assuming the database does not exist, creating the database, so creating the database is also using this command
>DB stats ();//View database Current state information
>db.dropdatabase ();//delete the current database
2, the operation of the collection:
>db.createcollection (collectionname,option);//collectionname is the collection name, option is an optional number of parameters, including the database size, and so on.
>db.collectionname.drop ();//delete collection. Where CollectionName is the name of the collection
3, the operation of the document:
>db.collectionname.insert (document);//Insert data into the collection, where document (JSON) is a JSON object or an array
>db.collectionname.find (condition);d b.collectionname.findone ();d b.collectionname.find (). Pertty ();
Query records, where condition (Json) is the filter condition. Assume that all is not added. Pertty () represents the output format optimization, and find has a very much more specific processing to describe later.
>db.collectionname.update (condition,updatedate);//update record. Condition (JSON) is the filter conditional JSON format.
Updatedate for the data you want to update
>db.mycol.update ({"A": 1},{$set: {"B": "B"}), and change the B field of the object in the MyCol collection to "B" for a field 1
>db.collectionname.remove (Condition,justone);//delete record, in which condition (Json) is the filter condition. Justone (Boolean) indicates whether to delete only the first article, assuming that it is not added, to empty the collection
>db.collectionname.find (). Skip (num). Limit (num),///paging query, where NUM is the start page and the size of each page.
>db.collectionname.find (Condition,{key:ishead});//Projection, the second JSON indicates whether a field is hidden, Ishead (0/1) represents
>db.collectionname.find (). Sort ({key:upordown});//result sort, Key is sort Keyword,upordown ( -1/1) for ascending or descending
>db.collectionname.ensureindex ({key:upordown});//Index. Key is an indexed field. Upordown ( -1/1) indicates ascending or descending
>db.collectionname.aggregate (options);//aggregation function. Options (Jsonarray) are available in a variety of options.
Db.mycol.aggregate ([{$group: {field: "$by _field", total:{$sum: 1}}]);//Group by By_field field, Show Field column and Total column
Three. Java Operation MongoDB:
When the Java operation MONGO. The first thing you need is jar package support. Assume that using Mave, dependencies such as the following:
<dependency> <groupId>org.springframework.data</groupId> <artifactId> Spring-data-mongodb</artifactid> <version>1.3.0.RELEASE</version></dependency>< dependency> <groupId>org.mongodb</groupId> <artifactid>mongo-java-driver</ Artifactid> <version>2.11.1</version></dependency>
A description of several basic Java objects such as the following:
Mongo: Database Connection object
Mongo Monge = new Mongo ("localhost", portl);
DB: Database object
DB db = Monge.getdb ("DbName");
Dbcollection: Collection Object
Dbcollection collection = Db.getcollection ("CollectionName");
Basicdbobject: Document Object
Basicdbobject basicdbobject = new Basedbobject ();
DBObject: Super Class for Document objects
DBObject dbobject = (dbobject) json.parse ("JSON");
Dbcursor: Cursors during Operation
dbcursor cursor = Collection.find ();
dbcursor cursor = collection.find ("Conditionjson");
Use code in detail such as the following:
@Testpublic void Testmongo () throws unknownhostexception{Mongo Mongo = new Mongoclient ("127.0.0.1", 27017);//Database connection DB db = Mongo.getdb ("test");//Database object Dbcollection coll1 = db.getcollection ("coll1");//Get Collection Object Dbcollection coll2 = Db.createcollection ("Coll2", null); set<string> Collset = Db.getcollectionnames (); DBObject Doc1 = new Basicdbobject ("a", 1);//Database Document Object Doc1.put ("B", "B"); DBObject doc2 = (dbobject) json.parse ("{\" a\ ": 2,\" b\ ": \" B\ "}"); Inserting Data Coll1.insert (Doc1, DOC2); Change data dbobject update = (dbobject) json.parse ("{\" a\ ": 2,\" b\ ": \" Updateb\ "}"); Coll1.update (New Basicdbobject ("A", 2), update, True, false); map<string, object> map = new hashmap<string, object> (); Map.put ("C", "Updatec"); Map.put ("D", 4); Coll1.update (New Basicdbobject ("a", 1), New Basicdbobject (map)); Query Document Object DBObject ref = (DBObject) json.parse ("{\" b\ ": \" Updateb\ "}"); dbcursor cursor = Coll1.find (ref); while (CURSOR.HASNExt ()) {DBObject obj = Cursor.next (); int Valuea = (Integer) obj.get ("a"); String valueb = (string) obj.get ("B"); System.out.println (Valuea + "-" + Valueb); System.out.println (obj); }//Delete data Coll1.remove (new Basicdbobject ("a", 1)); Coll1.drop ();//Clear Data}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
MongoDB Installation and use