??xml version="1.0" encoding="utf-8" standalone="yes"?>
2.现在完成后解压目录,攑ֈC盘下Q然后按照如下选择“我的电脑->属?>高->环境变量”Q将“C:\mongodb\bin”q个路径攑ֈ环境变量内(如果d环境变量可以ȝ上找Q?
3.选择"开?>q行"Q输?cmd"Q在DOSH口内输?#8220;mongod -port 27017 -dbpath D:/deployment/mongoDb/data/db -logpath D:/deployment/mongoDb/data/log/logs.log ”Q其中dbpath 为数据文件存储\径,logpath 为日志文件存储\径?/p>
4.不要关闭上面的窗口,新打开一个DOSQ输?#8220;mongo->use admin->db.AddUser(username,password)”,username:数据库数据集的用户名Qpassword数据库数据集操作的密码?/p>
5.在两个DOSH口内分别按“ctrl+c”Q在W一个DOSH口内输?#8220;mongod -port 27017 -dbpath D:/deployment/mongoDb/data/db -logpath D:/deployment/mongoDb/data/log/logs.log -logappend -auth”的启动命令,auth是要求操作数据集是需要验证?/p>
6.在第二个DOSH口内,输入“mongo->use admin->db.auth(username,password)”Q然后就可以Ҏ据集q行操作Q增、删、改Q?/p>
来自:http://www.hitb.com.cn/web/guest/bbs/-/message_boards/message/26944
--help | 昄命o行参?/td> |
--nodb | 不连接数据库方式启动Q稍后可以?nbsp;new Mongo() ?nbsp;connect() 来徏立连?/td> |
--shell | 从命令行q行完一?.js 文g后,停留在shell中,而不是结?/td> |
非JavaScript的辅助指令:
help | 昄帮助 |
db.help() | 昄 db Ҏ帮助 |
db.myColl .help() | 昄聚集的方法帮?/td> |
show dbs | 打印服务器上所有数据库的列?/td> |
use dbname | 讄db变量来指明用服务器上的 dbname 数据?/td> |
show collections | 打印当前数据库的所有聚?/td> |
show users | 打印当前数据库的用户 |
show profile | 打印最q耗时大于1ms的profiling操作 |
db | 指向当前数据库对象和q接的变量,已经在你的实例里定义好?/td> |
db.auth(user,pass) | 数据库认证(如果q行安全模式的话Q?/td> |
coll = db.collection | 讉K数据库里特定?nbsp;collection |
cursor = coll.find() | 查找聚集里所有的对象。参?nbsp;[查询] ?/td> |
coll.remove(objpattern ) | 从聚集里删除匚w的对象?nbsp; objpattern 是一个指定匹配的域的对象Q例如:coll.remove( { name: "Joe" } ); |
coll.save(object ) | 在聚集中保存对象Q如果已l存在的话则更新它?nbsp; 如果对象?presave ҎQ则会在保存到数据库之前Q插入和更新之前Q调用该Ҏ?/td> |
coll.insert(object) | 向聚集中插入对象。不会检查该对象是否已经存在聚集中(卻I不是 upsertQ?/td> |
coll.update(...) | 在聚集中更新对象。update() 有许多参敎ͼh?nbsp;更新 文档?/td> |
coll.ensureIndex( { name : 1 } ) | ?nbsp;name 建烦引。如果烦引存在则不做M事?/td> |
coll.drop() | 删除 coll 聚集 |
db.getSisterDB(name) | q回当前q接的另一个数据库。它允许跨数据库查询Q例如:db.getSisterDB('production').getCollectionNames() |
coll.find() | 查询所有文?/td> |
it | 循环上次 find() 调用q回的游?/td> |
coll.find( criteria ); | 查询聚集中匹?nbsp;criteria 的对象。例如:coll.find( { name: "Joe" } ); |
coll.findOne( criteria); | 查询q返回一个对象。如果没有找到则q回 null。如果你只需要返回一个对象,q个Ҏ?nbsp;find() as limit(1) 效率更高。如果元素类型是字符Ԍ数字或时_你还可以使用正则表达式:coll.find( { name: /joe/i } ); |
coll.find( criteria, fields ); | 查询对象里特定的域。例如:coll.find( {}, {name:true} ); |
coll.find().sort( {field :1[, field :1] }); | 对返回结果进行排序(field ASCQ。?-1 表示 DESC?/td> |
coll.find( criteria ).sort( { field : 1 } ) | 查找匚w criteria 的对象,q对 field q行排序?/td> |
coll.find( ... ).limit(n ) | 限制l果q回 n 行。如果你只需要某几行数据Q推荐这样做来获得最优性能?/td> |
coll.find( ... ).skip(n) | 跌 n 行结果?/td> |
coll.count() | q回聚集里对象的L?/td> |
coll.find( ... ).count() | q回匚w该查询的对象L。注意,该返回会忽略 limit ?skip。比如有100行记录匹配该查询Q但是limit?0Q?tt>count() 仍会q回100。这比你自己循环更快Q但仍然需要消耗些旉?/td> |
更多信息请参?nbsp;[查询] ?/p>
[{{db.getLastError()}}] | q回上次操作的错?/td> |
db.getPrevError() | q回之前操作的错?/td> |
db.resetError() | 清除错误记录 |
db.cloneDatabase(fromhost) | 从另外指定的L拯当前数据数据库。fromhost必须为noauth模式?/td> |
db.copyDatabase(fromdb, todb, fromhost) | 拯fromhost的fromdb数据库到当前服务器的todb数据库。fromhost必须为noauth模式?/td> |
db.repairDatabase() | 修复当前数据库。如果数据库很大则该操作会非常慢?/td> |
db.addUser(user,pwd) | l当前数据库d用户?/td> |
db.getCollectionNames() | 获得所有聚集的列表?/td> |
db.dropDatabase() | 删除当前数据库?/td> |
db = connect("<host>:<port>/<dbname>") | 打开一个新的数据库q接。一个shell可能有多个连接,但是shell自动的getLastError只用?'db' 变量?/td> |
conn = new Mongo("hostname") | 打开一个新的服务器q接。然后可以?nbsp;getDB() 来选择一个数据库?/td> |
db = conn.getDB("dbname") | 对一个连接选择一个特定的数据库?/td> |
Object.bsonsize(db.foo.findOne()) | 打印一个数据库对象的bson大小Qmongo 版本1.3及以上) |
db.foo.findOne().bsonsize() | 打印一个数据库对象的bson大小 (mongo 版本1.3之前) |
条g操作W?br style="line-height: normal; " />
$gt : >
$lt : <
$gte: >=
$lte: <=
$ne : !=?lt;>
$in : in
$nin: not in
$all: all
$not: 反匹?1.3.3及以上版?
查询 name <> "bruce" and age >= 18 的数?br style="line-height: normal; " /> db.users.find({name: {$ne: "bruce"}, age: {$gte: 18}});
查询 creation_date > '2010-01-01' and creation_date <= '2010-12-31' 的数?br style="line-height: normal; " /> db.users.find({creation_date:{$gt:new Date(2010,0,1), $lte:new Date(2010,11,31)});
查询 age in (20,22,24,26) 的数?br style="line-height: normal; " /> db.users.find({age: {$in: [20,22,24,26]}});
查询 age取模10{于0 的数?br style="line-height: normal; " />
db.users.find('this.age % 10 == 0');
或?br style="line-height: normal; " />
db.users.find({age : {$mod : [10, 0]}});
匚w所?br style="line-height: normal; " />
db.users.find({favorite_number : {$all : [6, 8]}});
可以查询出{name: 'David', age: 26, favorite_number: [ 6, 8, 9 ] }
可以不查询出{name: 'David', age: 26, favorite_number: [ 6, 7, 9 ] }
查询不匹配name=B*带头的记?br style="line-height: normal; " />
db.users.find({name: {$not: /^B.*/}});
查询 age取模10不等? 的数?br style="line-height: normal; " />
db.users.find({age : {$not: {$mod : [10, 0]}}});
#q回部分字段
选择q回age和_id字段(_id字段L会被q回)
db.users.find({}, {age:1});
db.users.find({}, {age:3});
db.users.find({}, {age:true});
db.users.find({ name : "bruce" }, {age:1});
0为false, ?为true
选择q回age、address和_id字段
db.users.find({ name : "bruce" }, {age:1, address:1});
排除q回age、address和_id字段
db.users.find({}, {age:0, address:false});
db.users.find({ name : "bruce" }, {age:0, address:false});
数组元素个数判断
对于{name: 'David', age: 26, favorite_number: [ 6, 7, 9 ] }记录
匚wdb.users.find({favorite_number: {$size: 3}});
不匹配db.users.find({favorite_number: {$size: 2}});
$exists判断字段是否存在
查询所有存在name字段的记?br style="line-height: normal; " />
db.users.find({name: {$exists: true}});
查询所有不存在phone字段的记?br style="line-height: normal; " />
db.users.find({phone: {$exists: false}});
$type判断字段cd
查询所有name字段是字W类型的
db.users.find({name: {$type: 2}});
查询所有age字段是整型的
db.users.find({age: {$type: 16}});
对于字符字段Q可以用正则表辑ּ
查询以字母b或者B带头的所有记?br style="line-height: normal; " />
db.users.find({name: /^b.*/i});
$elemMatch(1.3.1及以上版?
为数l的字段中匹配其中某个元?/p>
Javascript查询?where查询
查询 age > 18 的记录,以下查询都一?br style="line-height: normal; " />
db.users.find({age: {$gt: 18}});
db.users.find({$where: "this.age > 18"});
db.users.find("this.age > 18");
f = function() {return this.age > 18} db.users.find(f);
排序sort()
以年龄升序asc
db.users.find().sort({age: 1});
以年龄降序desc
db.users.find().sort({age: -1});
限制q回记录数量limit()
q回5条记?br style="line-height: normal; " />
db.users.find().limit(5);
q回3条记录ƈ打印信息
db.users.find().limit(3).forEach(function(user) {print('my age is ' + user.age)});
l果
my age is 18
my age is 19
my age is 20
限制q回记录的开始点skip()
从第3条记录开始,q回5条记?limit 3, 5)
db.users.find().skip(3).limit(5);
查询记录条数count()
db.users.find().count();
db.users.find({age:18}).count();
以下q回的不?Q而是user表中所有的记录数量
db.users.find().skip(10).limit(5).count();
如果要返回限制之后的记录数量Q要使用count(true)或者count(?)
db.users.find().skip(10).limit(5).count(true);
分组group()
假设test表只有以下一条数?br style="line-height: normal; " />
{ domain: "www.mongodb.org"
, invoked_at: {d:"2009-11-03", t:"17:14:05"}
, response_time: 0.05
, http_action: "GET /display/DOCS/Aggregation"
}
使用groupl计test?1月䆾的数据count:count(*)、total_time:sum(response_time)?avg_time:total_time/count;
db.test.group(
{ cond: {"invoked_at.d": {$gt: "2009-11", $lt: "2009-12"}}
, key: {http_action: true}
, initial: {count: 0, total_time:0}
, reduce: function(doc, out){ out.count++; out.total_time+=doc.response_time }
, finalize: function(out){ out.avg_time = out.total_time / out.count }
} );
[
{
"http_action" : "GET /display/DOCS/Aggregation",
"count" : 1,
"total_time" : 0.05,
"avg_time" : 0.05
}
]
来自: http://hi.baidu.com/asminfo/blog/item/20301e22dcfcce50ac34de7a.html
原先的grails?.2.0Q从q个版本之后Q我试过1.2.1Q?.2.2Q?.3.0Q?.3.1Q但都因为程序在q行时会变成qQ而一直用着1.2.0?/p>
我们的TOMCAT使用的是GBK~码QGRAILS文本都是使用UTF-8~码Q所以,q是产生q的根本原因?/p>
最q又有grails1.3.6Q更新得很快Q网上找了找Q有解决Ҏ了?a target="_blank" style="line-height: normal; ">http://www.groovyq.net/content/grails13%E5%8F%91%E5%B8%83
在服务器上一试,果然能行。现记录如下Q?/p>
重新讑֮System.out的编码,下面语句加入到_GrailsInit.groovy中,或者加入工E的BootStrap.groovy的initD即?br style="line-height: normal; " /> System.out = new PrintStream(System.out, true,"GB2312");
我用的是第二种ҎQ在BootStrap.groovy的initD中加入以上语句?/p>
以前Q有位高手也提到q,但他的原文是Q?/p>
“如果是println打印中文q的话Q那么就在初始化文g中增?br style="line-height: normal; " />
System.out = new PrintStream(System.out, true, "GBK")
应该了Q你试一下吧Q?”
所以我一直以为是解决打印q的?/p>
在此一q谢q?/p>
来自: http://hi.baidu.com/caihexi/blog/item/76d093a4cce0c6e59152ee15.html