mongo DB Install
download OS and Source in use
For java use library jar file download https://github.com/mongodb/mongo-java-driver/downloads
2. When get all file
Extract OS and Source file
Create floder to store C: my_mongo_dir
Create a data directory
By default MongoDB will store data in \data\db, but it won't automatically create that folder, so we do so here:
C:\> mkdir \data
C:\> mkdir \data\db
C:\> mkdir \data
C:\> mkdir \data\db
3. Run
Run and connect to the server
The important binaries for a first run are:
· mongod.exe - the database server. Try mongod --help to see startup options.
· mongo.exe - the administrative shell
To run the database, click mongod.exe in Explorer, or run it from a CMD window.
C:\> cd \my_mongo_dir\bin
C:\my_mongo_dir\bin> mongod
Note: It is also possible to run the server as a Windows Service. But we can do that later.
Now, start the administrative shell, either by double-clicking mongo.exe in Explorer, or from the CMD prompt. By default mongo.exe connects to a mongod server running on localhost and uses the database named test. Run mongo --help to see other options.
C:\> cd \my_mongo_dir\bin
C:\my_mongo_dir\bin> mongo
> // the mongo shell is a javascript shell connected to the db
> // by default it connects to database 'test' at localhost
> 3+3
6
> db
test
> // the first write will create the db:
> db.foo.insert( { a : 1 } )
> db.foo.find()
{ _id : ..., a : 1 }
> show dbs
...
> show collections
...
> help
Java Code Example
package ball;
/**
* Simple Example that shows creating a people (id:number, name:string,
* gender:string) collection and then adding, finding, updating and deleting.
* You need to download and install MongoDB (www.mongodb.org) and run the
* server. You’ll also need to have the mongo-1.2.jar in your class path.
*/
import java.net.UnknownHostException;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* @author vsbabu
*/
public class MongoManager {
public MongoManager(){
RunCommand();
Mongo();
}
private void Mongo() {
System.out.println("Start ");
try {
Mongo m = new Mongo("localhost", 27017);
DB db = m.getDB("sampledb");
DBCollection coll = db.getCollection("people");
// clear records if any
DBCursor cur = coll.find();
while (cur.hasNext())
coll.remove(cur.next());
// create a unique ascending index on id;
//doesn’t seem to work? it works from python and javascript
coll.ensureIndex(new BasicDBObject("id", 1).append("unique", true));
coll.createIndex(new BasicDBObject("name", 1));
coll.insert(makePersonDocument(6655, "James", "male"));
coll.insert(makePersonDocument(6797, "Bond", "male"));
coll.insert(makePersonDocument(6643, "Cheryl", "female"));
coll.insert(makePersonDocument(7200, "Scarlett", "female"));
coll.insert(makePersonDocument(6400, "Jacks", "male"));
System.out.println("Total Records : " + coll.getCount());
cur = coll.find();
printResults(cur, "Find All Records");
cur = coll.find(new BasicDBObject("id", 6655));
printResults(cur, "Find id = 6655");
cur = coll.find(new BasicDBObject()
.append("id", new BasicDBObject("$lte", 6700)));
printResults(cur, "Find id <= 6700");
cur = coll.find(new BasicDBObject()
.append("id", new BasicDBObject("$lte", 6700))
.append("gender", "male"));
printResults(cur, "Find id <= 6700 and gender = male");
cur = coll.find(new BasicDBObject()
.append("name", Pattern.compile("^ja.*?s$", Pattern.CASE_INSENSITIVE)))
.sort(new BasicDBObject("name", -1));
printResults(cur, "Find name like Ja%s and sort reverse by name");
cur = coll.find(new BasicDBObject()
.append("gender", "female"))
.sort(new BasicDBObject("id", -1))
.limit(2);
printResults(cur, "Get top 2 (by id) ladies");
//let us reduce every body’s phone numbers by 10; add Sir to males, Mme to ladies
cur = coll.find();
while(cur.hasNext()) {
BasicDBObject set = new BasicDBObject("$inc", new BasicDBObject("id", -10));
if ("male".equals(cur.next().get("gender")))
set.append("$set", new BasicDBObject("name", "Sir ".concat((String) cur.curr().get("name"))));
else
set.append("$set", new BasicDBObject("name", "Mme ".concat((String) cur.curr().get("name"))));
coll.update(cur.curr(), set);
}
cur = coll.find();
printResults(cur, "All, after id and name update");
}
catch (UnknownHostException ex) {
ex.printStackTrace();
}
catch (MongoException ex) {
ex.printStackTrace();
}
}
private void RunCommand() {
System.out.println("Run");
try {
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler C:/mongodb/bin");
p.waitFor();
JOptionPane.showMessageDialog(null, "Open mongod.exe");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new MongoManager();
}
private static void printResults(DBCursor cur, String message) {
System.out.println("<<<<<<<<<< " + message + " >>>>>>>>>>>>");
while (cur.hasNext()) {
System.out.println(cur.next().get("id") + "," + cur.curr().get("name") + "," + cur.curr().get("gender"));
}
}
private static BasicDBObject makePersonDocument(int id, String name, String gender) {
BasicDBObject doc = new BasicDBObject();
doc.put("id", id);
doc.put("name", name);
doc.put("gender", gender);
return doc;
}
}
/*
Output will look like below
Total Records : 5
<<<<<<<<<< Find All Records >>>>>>>>>>>>
6655,James,male
6797,Bond,male
6643,Cheryl,female
7200,Scarlett,female
6400,Jacks,male
<<<<<<<<<< Find id = 6655 >>>>>>>>>>>>
6655,James,male
<<<<<<<<<< Find id <= 6700 >>>>>>>>>>>>
6400,Jacks,male
6643,Cheryl,female
6655,James,male
<<<<<<<<<< Find id <= 6700 and gender = male >>>>>>>>>>>>
6400,Jacks,male
6655,James,male
<<<<<<<<<< Find name like Ja%s and sort reverse by name >>>>>>>>>>>>
6655,James,male
6400,Jacks,male
<<<<<<<<<< Get top 2 (by id) ladies >>>>>>>>>>>>
7200,Scarlett,female
6643,Cheryl,female
<<<<<<<<<< All, after id and name update >>>>>>>>>>>>
6645,Sir James,male
6787,Sir Bond,male
6633,Mme Cheryl,female
7190,Mme Scarlett,female
6390,Sir Jacks,male
*/
ไม่มีความคิดเห็น:
แสดงความคิดเห็น