MongoDB installation and configuration

Source: Internet
Author: User
Tags mongodb server

 

I. Install and manage the MongoDB Database System:
1, download: http://downloads.mongodb.org/win32/mongodb-win32-i386-1.4.0.zip
2, extract the mongodb-win32-i386-1.4.0.zip to D:/MongoDB/mongodbwin321.6.0/, and then create a folder in which data is stored in the test database.
3. Start the mingodb server manually:
Open a DOS window, enter the D:/MongoDB/mongodbwin321.6.0/bin directory, and run the following command:
D:/MongoDB/mongodbwin321.6.0/bin> mongod -- dbpath D:/MongoDB/mongodbwin321.6.0/Data
Do not close the DOS window.

The default connection port of MongoDB server is 27017.

4. After the MongoDB server is started, open a DOS window again and enter the D:/MongoDB/mongodbwin321.6.0/bin directory. Run the command cmd.exe and the following information will appear:
D:/MongoDB/mongodbwin321.6.0/bin> Mongo
MongoDB shell version: 1.4.0
URL: Test

Connecting to: Test
Type "exit" to exit
Type "help" for help
>
Cmd.exe is an officially provided command line management client, where you can manage databases and maintain database systems.
5. Run:
> Help is a help command.
> Show dBs; displays the databases preset by MongoDB.
> Use test; open the database
> Show collections; display collection
> DB. Test. Save ({RPG: 100}); save a message to collection test.
> DB. Test. Find (); Retrieve all records
{"_ Id": objectid ("5c558875dd6f010304538537"), "RPG": 100}
Ii. Java MongoDB Testing
1. Download the driver package: mongo-Java-dirver
2.
// Obtain the Database Service
Mongo M = NULL;
Try {
M = new Mongo ("localhost", 27017 );
} Catch (unknownhostexception e ){
E. printstacktrace ();
} Catch (except exception e ){
E. printstacktrace ();
}

// Obtain the database mytest
DB = M. getdb ("mytest ");
Dbcollection collection = dB. getcollection ("test ");

Basicdbobject BDO = new basicdbobject ();
BDO. Put ("key1", "value1 ");
BDO. Put ("key2", "value2 ");
BDO. Put ("key3", "value3 ");
BDO. Put ("key4", "value4 ");
BDO. Put ("key5", "value5 ");
// Duplicate records are not inserted
BDO. Put ("key1", "value1 ");

// Delete all data
Collection. Remove (New basicdbobject ());
Collection. insert (BDO );

// Obtain the names of all tables in the mytest database.
Set <string> colls = dB. getcollectionnames ();

For (string S: colls ){

System. Out. println (s );

}
MongoDB provides the ability to export data to JSON or CSV files.

See http://www.mongodb.org/display/DOCS/Import+Export+Tools for details

Note the following:
If you want to output CSV, You have to specify the fields in the order you want them.

An example is as follows:
Java code
1. Export export-D dba22-C foo-f X, Y, a, B, c -- CSV-o./result.csv
./Configure import-D foo-C t1/data/t1.json
-D indicates the database
-C mark data table
-F fields to be extracted are separated by commas (,).
-O output path
 
 

Echo "/usr/local/Server/MongoDB/bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs -- logappend -- auth -- Port = 27017 ">/etc/rc. local
./Bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs -- logappend -- Port = 27017 -- fork

 

Package CN. Wang. MongoDB;

Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. filenotfoundexception;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java.net. unknownhostexception;
Import java. util. arraylist;
Import java. util. List;

Import CN. Wang. MongoDB. Beans. imagebean;

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. parse exception;
Import com. MongoDB. util. JSON;

Public class mongodbtest {

Public static void main (string [] ARGs ){

// Run ();
List <imagebean> images = getimagebytype ("1 ");
If (null = images ){
System. Out. println ("no record ");
} Else {
For (imagebean image: Images ){
System. Out. println (image );
}
}

/// DB. Authenticate ("opzoon", "123@opzoon.com". tochararray ());
//
//// Query all databases
// For (string name: M. getdatabasenames ()){
// System. Out. println ("dbname:" + name );
//}
//
// Dbcollection collection = dB. getcollection ("test ");
//
// Basicdbobject BDO = new basicdbobject ();
// BDO. Put ("key1", "value1 ");
// BDO. Put ("key2", "value2 ");
// BDO. Put ("key3", "value3 ");
// BDO. Put ("key4", "value4 ");
// BDO. Put ("key5", "value5 ");
//
/// Do not insert duplicate records
// BDO. Put ("key1", "value1 ");
//
//// Delete all data
// Collection. Remove (New basicdbobject ());
// Collection. insert (BDO );
// DB. Authenticate ("opzoon", "opzoon". tochararray ());
// Dbcollection collection = dB. getcollection ("test1 ");

// Basicdbobject data1 = new basicdbobject ();
// Data1.put ("key1", "value1 ");
// Data1.put ("key2", "value2 ");
// Data1.put ("key3", "value3 ");
// Data1.put ("key4", "value4 ");
// Data1.put ("key5", "value5 ");
// Data1.put ("key1", "value1 ");
//
// Basicdbobject data2 = new basicdbobject ();
// Data2.put ("key11", "value11 ");
// Data2.put ("key21", "value21 ");
// Data2.put ("key31", "value31 ");
// Data2.put ("key41", "value41 ");
// Data2.put ("key51", "value51 ");
// Data2.put ("key11", "value11 ");
//
//// Delete all data
// Collection. Remove (New basicdbobject ());
// Collection. insert (data1 );
// Collection. insert (data2 );
//
//
// Basicdbobject data3 = new basicdbobject ();
// Data3.put ("AAA", "BBB ");
// Data3.put ("CCC", "DDD ");
// Collection. Update (New basicdbobject (). append ("key11", "value11 "),
// Data3 );

// Dbcursor cursor = dB. getcollection ("test"). Find ();
// While (cursor. hasnext ()){
// System. Out. println (cursor. Next ());
//}

// Obtain the names of all tables in the mytest database.
// Set <string> colls = dB. getcollectionnames ();
// For (string S: colls ){
// System. Out. println (s );
//}

}

Public static void run (){
File imagesfile = new file ("D: \ images.txt ");
Filereader Fr = NULL;
Bufferedreader BR = NULL;
Try {
Fr = new filereader (imagesfile );
BR = new bufferedreader (FR );

String line;
While (null! = (Line = Br. Readline ())){
Insertmong (line );
}
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (null! = Br ){
Try {
BR. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
BR = NULL;
}

If (null! = Fr ){
Try {
Fr. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Fr = NULL;
}
}
}

Private Static void insertmong (string line ){
String [] DATA = line. Split (",");
String type = data [0];
String typech = data [1];
String theme = data [2];
String themech = data [3];
String name = data [4];
String remarks = data [5];

DB = getmongodb ();

Dbcollection collection = dB. getcollection ("image ");
Basicdbobject BDO = new basicdbobject ();
BDO. Put ("type", type );
BDO. Put ("typech", typech );
BDO. Put ("theme", theme );
BDO. Put ("themech", themech );
BDO. Put ("name", name );
BDO. Put ("Remarks", remarks );

// Duplicate records are not inserted
// BDO. Put ("key1", "value1 ");

Collection. insert (BDO );
}

Private Static dB getmongodb (){
// Obtain the Database Service
Mongo M = NULL;
Try {
M = new Mongo ("172.16.101.253", 27017 );
} Catch (unknownhostexception e ){
E. printstacktrace ();
} Catch (except exception e ){
E. printstacktrace ();
}

// Obtain the database mytest
DB = M. getdb ("Images ");

Return dB;
}

Public static list <imagebean> getimagebytype (string type ){

List <imagebean> images = new arraylist <imagebean> ();
DB = getmongodb ();

Dbcollection coll = dB. getcollection ("image ");
Dbobject OBJ = NULL;
Dbcursor cursor = NULL;
If (null! = Type ){
OBJ = new basicdbobject ();
OBJ. Put ("type", type );
Cursor = Coll. Find (OBJ). Limit (6). Skip (0 );
} Else {
Cursor = Coll. Find ();
}

// Query by PAGE
System. Out. println (cursor. Count ());
While (cursor. hasnext ()){
Imagebean image = new imagebean ();

Dbobject DBO = cursor. Next ();
String data = JSON. serialize (DBO );

System. Out. println (data );

// Convert the string into an object
Parsedata (data, image );

Images. Add (image );
}

Return images. isempty ()? Null: images;
}

Private Static void parsedata (string data, imagebean image ){
Stringbuffer sb = new stringbuffer (data );
SB. deletecharat (0). deletecharat (sb. Length ()-1 );

Data = sb. tostring ();
String [] datas = data. Split (",");
String type = datas [1];
String theme = datas [2];
String name = datas [3];
String remarks = datas [4];

Image. settype (type );
Image. settheme (theme );
Image. setname (name );
Image. setremarks (Remarks );
}
}

Package CN. Wang. MongoDB. beans;

Import java. Io. serializable;

Public class imagebean implements serializable {

Private Static final long serialversionuid =-1962370938260404035l;

Private int ID;
 
Private string name;
 
Private string theme;
 
Private string remarks;
 
Private string type;

Public int GETID (){
Return ID;
}

Public void setid (int id ){
This. ID = ID;
}

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public String gettheme (){
Return theme;
}

Public void settheme (string theme ){
This. theme = theme;
}

Public String getremarks (){
Return remarks;
}

Public void setremarks (string remarks ){
This. Remarks = remarks;
}

Public String GetType (){
Return type;
}

Public void settype (string type ){
This. type = type;
}
 
Public String tostring (){
Return "type:" + this. Type + ","
+ "Theme:" + this. Theme + ","
+ "Name:" + this. Name + ","
+ "Remarks:" + this. remarks;
}
 
}

 

Related Article

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.