注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

mongodb 之初体验  

2010-04-14 19:43:17|  分类: 非关系数据库 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
mongodb的官网
http://www.mongodb.org/display/DOCS/Home

1.在http://www.mongodb.org/display/DOCS/Downloads下载Windows 32 bit 1.4的版本
2.在windows下的安装文档在 http://www.mongodb.org/display/DOCS/Quickstart
现在c盘建立 c:\data\db 的文件
解压刚才下载的zip包

打开 cmd  cd到mongo的bin目录下 执行mongod

以下是cmd的过程
----------------------------------------------------------------------------------------------------------
C:\mongodb-win32-i386-1.4.0>cd bin
------
C:\mongodb-win32-i386-1.4.0\bin>mongod
mongod --help for help and startup options
Wed Apr 14 12:58:46 Mongo DB : starting : pid = 0 port = 27017 dbpath = /data/
/ master = 0 slave = 0  32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of da

**       see http://blog.mongodb.org/post/137788967/32-bit-limitations for mor

Wed Apr 14 12:58:46 db version v1.4.0, pdfile version 4.5
Wed Apr 14 12:58:46 git version: 514f8bbab657c1dc110d45eea6ea33de296dbb26
Wed Apr 14 12:58:46 sys info: windows (5, 1, 2600, 2, 'Service Pack 3') BOOST_
B_VERSION=1_35
Wed Apr 14 12:58:46 waiting for connections on port 27017
Wed Apr 14 12:58:46 web admin interface listening on port 28017
Wed Apr 14 13:03:24 connection accepted from 127.0.0.1:4110 #1
Wed Apr 14 13:05:00 building new index on { _id: 1 } for local.foo
Wed Apr 14 13:05:00 Buildindex local.foo idxNo:0 { name: "_id_", ns: "local.fo
, key: { _id: 1 } }
Wed Apr 14 13:05:00 done for 0 records 0.031secs
FlushViewOfFile failed 33
Wed Apr 14 13:14:40 building new index on { _id: 1 } for mydb.things
Wed Apr 14 13:14:40 Buildindex mydb.things idxNo:0 { name: "_id_", ns: "mydb.t
ngs", key: { _id: 1 } }
Wed Apr 14 13:14:40 done for 0 records 0.046secs
FlushViewOfFile failed 33
Wed Apr 14 19:32:06 connection accepted from 172.17.17.31:3938 #2
Wed Apr 14 19:32:06 MessagingPort recv() errno:0 No error 172.17.17.31:3938
Wed Apr 14 19:32:06 end connection 172.17.17.31:3938
Wed Apr 14 19:36:26 connection accepted from 172.17.17.31:3951 #3
Wed Apr 14 19:36:26 connection accepted from 172.17.17.31:3952 #4
Wed Apr 14 19:36:26 MessagingPort recv() errno:0 No error 172.17.17.31:3952
Wed Apr 14 19:36:26 end connection 172.17.17.31:3952
Wed Apr 14 19:36:26 MessagingPort recv() errno:0 No error 172.17.17.31:3951
Wed Apr 14 19:36:26 end connection 172.17.17.31:3951

C:\mongodb-win32-i386-1.4.0\bin>mongod
mongod --help for help and startup options
Wed Apr 14 12:58:46 Mongo DB : starting : pid = 0 port = 27017 dbpath = /data/
/ master = 0 slave = 0  32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of da

**       see http://blog.mongodb.org/post/137788967/32-bit-limitations for mor

Wed Apr 14 12:58:46 db version v1.4.0, pdfile version 4.5
Wed Apr 14 12:58:46 git version: 514f8bbab657c1dc110d45eea6ea33de296dbb26
Wed Apr 14 12:58:46 sys info: windows (5, 1, 2600, 2, 'Service Pack 3') BOOST_
B_VERSION=1_35
Wed Apr 14 12:58:46 waiting for connections on port 27017
Wed Apr 14 12:58:46 web admin interface listening on port 28017
Wed Apr 14 13:03:24 connection accepted from 127.0.0.1:4110 #1
Wed Apr 14 13:05:00 building new index on { _id: 1 } for local.foo
Wed Apr 14 13:05:00 Buildindex local.foo idxNo:0 { name: "_id_", ns: "local.fo
, key: { _id: 1 } }
Wed Apr 14 13:05:00 done for 0 records 0.031secs
FlushViewOfFile failed 33
Wed Apr 14 13:14:40 building new index on { _id: 1 } for mydb.things
Wed Apr 14 13:14:40 Buildindex mydb.things idxNo:0 { name: "_id_", ns: "mydb.t
ngs", key: { _id: 1 } }
Wed Apr 14 13:14:40 done for 0 records 0.046secs
FlushViewOfFile failed 33
Wed Apr 14 19:32:06 connection accepted from 172.17.17.31:3938 #2
Wed Apr 14 19:32:06 MessagingPort recv() errno:0 No error 172.17.17.31:3938
Wed Apr 14 19:32:06 end connection 172.17.17.31:3938
Wed Apr 14 19:36:26 connection accepted from 172.17.17.31:3951 #3
Wed Apr 14 19:36:26 connection accepted from 172.17.17.31:3952 #4
Wed Apr 14 19:36:26 MessagingPort recv() errno:0 No error 172.17.17.31:3952
Wed Apr 14 19:36:26 end connection 172.17.17.31:3952
Wed Apr 14 19:36:26 MessagingPort recv() errno:0 No error 172.17.17.31:3951
Wed Apr 14 19:36:26 end connection 172.17.17.31:3951
--------------------------------------------------------------------------------------------------------

打开另一个cmd
cd到bin下执行 mongo的命令

后输入help 命令

--------------------------------------------------------------------------------------------------------
C:\>cd mongodb-win32-i386-1.4.0

C:\mongodb-win32-i386-1.4.0>cd bin

C:\mongodb-win32-i386-1.4.0\bin>mongo
MongoDB shell version: 1.4.0
url: test
connecting to: test
type "exit" to exit
type "help" for help
> help
HELP
show dbs                     show database names
show collections             show collections in current database
show users                   show users in current database
show profile                 show most recent system.profile entries wit
h time >= 1ms
use <db name>                set curent database to <db name>
db.help()                    help on DB methods
db.foo.help()                help on collection methods
db.foo.find()                list objects in collection foo
db.foo.find( { a : 1 } )     list objects in foo where a == 1
it                           result of the last line evaluated; use to f
urther iterate
---------------------------------------------------------------------------------------------


这样便可以开始操作了

http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo

这里有关于启动和关闭数据库的命令

http://www.mongodb.org/display/DOCS/Tutorial

这里有详细而简单的教程

http://www.mongodb.org/display/DOCS/Manual

这里是命令手册



如何 用Java 访问mongdb数据库

http://www.mongodb.org/display/DOCS/Java+Tutorial
这里有详细的讲解

记得引入mongo-1.4.jar就行了 

这是我的一段Test代码

-----------------------------------------------------------------------------------------
package com.platform.mongodb.test;

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.Mongo;
import com.mongodb.MongoException;

public class TestMongodb {

//get DB
public static DB getDB() throws UnknownHostException, MongoException{
Mongo m = new Mongo( "localhost" , 27017 );
return m.getDB("mydb");       
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//get collection
Set<String> colls = getDB().getCollectionNames();
//遍历 collection
for (String s : colls) {
System.out.println(s);
}

//get collection collection'name is things
DBCollection coll = getDB().getCollection("things");

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);
//insert data
coll.insert(doc);
//query data
DBCursor dbc=coll.find();
while(dbc.hasNext()){
System.out.println(dbc.next());
}

}

}
-----------------------------------------------------------------------------------------



结果
----------------------------------------------------------------------------------------
system.indexes
things
{ "_id" : "4bc54f40851b00000000464e" , "name" : "hurry"}
{ "_id" : "4bc54f45851b00000000464f" , "x" : 3.0}
{ "_id" : "4bc5507f851b000000004650" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004651" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004652" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004653" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004654" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004655" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004656" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004657" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5507f851b000000004658" , "x" : 4.0 , "j" : 1.0}
{ "_id" : "4bc5590d851b000000004659" , "x" : 4.0}
{ "_id" : "4bc5a8baffe574e4aa9cbabc" , "name" : "MongoDB" , "type" : "database" , "count" : 1 , "info" : { "x" : 203 , "y" : 102}}
{ "_id" : "4bc6f4ff17b774e427b39fb3" , "name" : "MongoDB" , "type" : "database" , "count" : 1 , "info" : { "x" : 203 , "y" : 102}}
-----------------------------------------------------------------------------------------


很明显这种数据库 抛弃了SQL语句 直接用相应的编程语言完成对数据库的操作

我对它的发展充满了期待 ,希望有朝一日它可以代替关系数据库




6月8日 补充 (其实你不想装也行) 下面的博文提供了几种


mongodb的管理工具(很不错 可以试试)
http://blog.nosqlfan.com/html/213.html


  评论这张
 
阅读(406)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017