[转]MongoDB学习笔记

一、 mongoDB的 官方网站是http://www.mongodb.org/ 到此下载数据库
下载Windows 32-bit版本并解压缩,程序文件都在bin目录中,其它两个目录分别是C++调用是的头文件和库文件。bin目录中包含如下几个程序:
1. mongo.exe,命令行客户端工具。
2. mongod.exe,数据库服务程序。
3. mongodump.exe,数据库备份程序。
4. mongoexport.exe,数据导出工具。
5. mongofiles.exe,GridFS工具。
6. mongoimport.exe,数据导入工具。
7. mongorestore.exe,数据库恢复工具。
8. mongos.exe,貌似是性能检测工具。

二、使用mongo工具shell
1.
运行 Mongodb服务
创建D:\Database\mongo\db\
文件夹作为数据库文件夹
打开命令行,切换到bin目录,执行:
mongod.exe –dbpath D:\Database\monbo\db\
启动数据库服务程序。可以看到类似下面的提示:

Sun Dec 13 01:28:48 Mongo DB : starting : pid = 0 port = 27017 dbpath = D:\Datab
ase\mongo\db\ master = 0 slave = 0 64-bit
Sun Dec 13 01:28:49 db version v1.2.0, pdfile version 4.5
Sun Dec 13 01:28:49 git version: 2add01f37ddf5a262dbc66f81353e88f38723dc6
Sun Dec 13 01:28:49 sys info: windows (6, 0, 6002, 2, ‘Service Pack 2′) BOOST_LI
B_VERSION=1_39
Sun Dec 13 01:28:49 waiting for connections on port 27017

2.使用命令
//切换到指定数据库,如果数据库不存在会自动创建
use dbName
//向数据库,指定数据集插入数据,如果数据集不存在会自动创建
db.collectionName.save({aa,11})
//查找数据
db.collectionName .findOne()

3.java 使用代码例子
import java.net.UnknownHostException;
import com.mongodb.Mongo;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoException;


public class TestMongDBCase {

 /**
  * @param args
  * @throws MongoException 
  * @throws UnknownHostException 
  * 
  */
 private static String host="localhost";
 private static int port=27017;
 private static String dbName="test";
 
 public static DBCollection getConnection(String host,int port,String dbName) 
     throws UnknownHostException, MongoException{
  Mongo db = new Mongo(host, port);
  DBCollection coll=db.getDB(dbName).getCollection("testCollection");
  System.out.println(coll );
  return coll;
  
 }
 
 public void getOneData () throws  Exception{
  DBCollection coll=getConnection(host, port, dbName);
  DBObject myDoc = coll.findOne();
  System.out.println(myDoc);
 }
 
 public void getAllData () throws  Exception{
  DBCollection coll=getConnection(host, port, dbName);
  DBCursor myDoc = coll.find();
  while(myDoc.hasNext()){
   System.out.println(myDoc.next());
  }
 }
 
 
 public void getDeleteData () throws  Exception{
  DBCollection coll=getConnection(host, port, dbName);
  BasicDBObject delete = new BasicDBObject();
  delete.put("name", "MongoDB");
   coll.remove(delete);
   
 }
 public void getQuery () throws  Exception{
  DBCollection coll=getConnection(host, port, dbName);
  BasicDBObject query = new BasicDBObject();
  query.put("name", "MongoDB");
  DBCursor cur = coll.find(query);
  while(cur.hasNext()){
   System.out.println(cur.next());
  }
 }
 
 public void addData () throws  Exception{
  BasicDBObject doc = new BasicDBObject();
  doc.put("name", "MongoDB");
  doc.put("type", "database");
  doc.put("count", 1);
  BasicDBObject info = new BasicDBObject();
  info.put("x", 203);
  info.put("y", 102);
  doc.put("info", info);
  DBCollection coll=getConnection(host, port, dbName);
  coll.insert(doc);
 }
 
 
 public static void main(String[] args) throws Exception {
  new TestMongDBCase ().getDeleteData();
  new TestMongDBCase ().getQuery();
  new TestMongDBCase ().getAllData();
  new TestMongDBCase ().getOneData();
  new TestMongDBCase ().addData();
  
    
 }

}

 

你可能感兴趣的:(mongodb,windows,.net,git,sun)