db.collection.find({ "field" : { $gt: value } } ); // greater than : field > value
db.collection.find({ "field" : { $lt: value } } ); // less than : field < value
db.collection.find({ "field" : { $gte: value } } ); // greater than or equal to : field >= value
db.collection.find({ "field" : { $lte: value } } ); // less than or equal to : field <= value
濡傛煡璇澶т簬3,灝忎簬4:
db.things.find({j : {$lt: 3}});
db.things.find({j : {$gte: 4}});
涔熷彲浠ュ悎騫跺湪涓鏉¤鍙ュ唴:
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); // value1 < field < value
2) 涓嶇瓑浜?$ne
渚嬪瓙錛?/p>
db.things.find( { x : { $ne : 3 } } );
db.collection.find( { "field" : { $in : array } } );
渚嬪瓙錛?/p>
db.things.find({j:{$in: [2,4,6]}});
db.things.find({j:{$nin: [2,4,6]}});
db.things.find( "this.a % 10 == 1")
鍙敤$mod浠f浛錛?/p>
db.things.find( { a : { $mod : [ 10 , 1 ] } } )
{ a: [ 1, 2, 3 ] }
涓嬮潰榪欎釜鏉′歡鏄彲浠ュ尮閰嶇殑錛?/p>
db.things.find( { a: { $all: [ 2, 3 ] } } );
浣嗘槸涓嬮潰榪欎釜鏉′歡灝變笉琛屼簡錛?/p>
db.things.find( { a: { $all: [ 2, 3, 4 ] } } );
db.things.find( { a : { $size: 1 } } );
瀹樼綉涓婅涓嶈兘鐢ㄦ潵鍖歸厤涓涓寖鍥村唴鐨勫厓绱狅紝濡傛灉鎯蟲壘$size<5涔嬬被鐨勶紝浠栦滑寤鴻鍒涘緩涓涓瓧孌墊潵淇濆瓨鍏冪礌鐨勬暟閲忋?/p>
You cannot use $size to find a range of sizes (for example: arrays with more than 1 element). If you need to query for a range, create an extra size field that you increment when you add elements.
7錛?exists
$exists鐢ㄦ潵鍒ゆ柇涓涓厓绱犳槸鍚﹀瓨鍦細(xì)
濡傦細(xì)
db.things.find( { a : { $exists : true } } ); // 濡傛灉瀛樺湪鍏冪礌a,灝辮繑鍥?/span>
db.things.find( { a : { $exists : false } } ); // 濡傛灉涓嶅瓨鍦ㄥ厓绱燼錛屽氨榪斿洖
$type 鍩轟簬 bson type鏉ュ尮閰嶄竴涓厓绱犵殑綾誨瀷錛屽儚鏄寜鐓х被鍨婭D鏉ュ尮閰嶏紝涓嶈繃鎴戞病鎵懼埌bson綾誨瀷鍜宨d瀵圭収琛ㄣ?/p>
db.things.find( { a : { $type : 2 } } ); // matches if a is a string
db.things.find( { a : { $type : 16 } } ); // matches if a is an int
db.customers.find( { name : /acme.*corp/i } ); // 鍚庨潰鐨刬鐨勬剰鎬濇槸鍖哄垎澶у皬鍐?/pre>
db.things.find( { colors : "red" } );
> t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } )
{ "_id" : ObjectId("4b5783300334000000000aa9"),
"x" : [ { "a" : 1, "b" : 3 }, 7, { "b" : 99 }, { "a" : 11 } ]
}
$elemMatch : { a : 1, b : { $gt : 1 } } 鎵鏈夌殑鏉′歡閮借鍖歸厤涓婃墠琛屻?/pre>娉ㄦ剰錛屼笂闈㈢殑璇彞鍜屼笅闈㈡槸涓嶄竴鏍風(fēng)殑銆?/p>
> t.find( { "x.a" : 1, "x.b" : { $gt : 1 } } )
$elemMatch鏄尮閰峽 "a" : 1, "b" : 3 }錛岃屽悗闈竴鍙ユ槸鍖歸厤{ "b" : 99 }, { "a" : 11 }
12) 鏌ヨ宓屽叆瀵硅薄鐨勫?br />
db.postings.find( { "author.name" : "joe" } );娉ㄦ剰鐢ㄦ硶鏄?span class="code-quote">author.name錛岀敤涓涓偣灝辮浜嗐傛洿璇︾粏鐨勫彲浠ョ湅榪欎釜閾炬帴錛?dot notation
涓句釜渚嬪瓙錛?/p>
> db.blog.save({ title : "My First Post", author: {name : "Jane", id : 1}})濡傛灉鎴戜滑瑕佹煡璇?authors name 鏄疛ane鐨? 鎴戜滑鍙互榪欐牱錛?/p>
> db.blog.findOne({"author.name" : "Jane"})濡傛灉涓嶇敤鐐癸紝閭e氨闇瑕佺敤涓嬮潰榪欏彞鎵嶈兘鍖歸厤錛?/p>
db.blog.findOne({"author" : {"name" : "Jane", "id" : 1}})涓嬮潰榪欏彞錛?/p>
db.blog.findOne({"author" : {"name" : "Jane"}})鏄笉鑳藉尮閰嶇殑錛屽洜涓簃ongodb瀵逛簬瀛愬璞★紝浠栨槸綺劇‘鍖歸厤銆?/p>
13) 鍏冩搷浣滅 $not 鍙栧弽
濡傦細(xì)
db.customers.find( { name : { $not : /acme.*corp/i } } );db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );mongodb榪樻湁寰堝鍑芥暟鍙互鐢紝濡傛帓搴忥紝緇熻絳夛紝璇峰弬鑰冨師鏂囥?br />
mongodb鐩墠娌℃湁鎴?or)鎿嶄綔絎︼紝鍙兘鐢ㄥ彉閫氱殑鍔炴硶浠f浛錛屽彲浠ュ弬鑰冧笅闈㈢殑閾炬帴錛?br />
http://www.mongodb.org/display/DOCS/OR+operations+in+query+expressions
]]>
#coding=utf-8
from pymongo import Connection
conn = Connection("localhost")
db = conn.foo
test = {"id":1, "novle":"嫻嬭瘯涓涓?/span>"}
db.foo.save(test)
cursor = db.foo.find()
for i in cursor:
print i
铏界劧鎶ラ敊錛屼笉褰卞搷鎵ц
out
{u'_id': ObjectId('4bc193c11724bc0cc4000000'), u'id': 1, u'novle': u'\ufffd\ufffd\ufffd\ufffd'}
OK 嫻嬭瘯鎴愬姛
涓嬫 灝辨槸璁?鎬庝箞鍦―JANGO閲岄潰 浣跨敤mongodb浜?/p>
MongoDB鎶婃暟鎹瓨鍌ㄥ湪鏂囦歡涓紙榛樿璺緞涓猴細(xì)/data/db錛夛紝涓烘彁楂樻晥鐜囦嬌鐢ㄥ唴瀛樻槧灝勬枃浠惰繘琛岀鐞嗐?/p>
瀹夎錛?br />
Linux/OS X涓?
1 寤虹珛鏁版嵁鐩綍
mkdir -p /data/db
2 涓嬭澆鍘嬬緝鍖?br />
curl -O http://downloads.mongodb.org/linux/mongodb-linux-i686-latest.tgz
3 瑙e帇緙╂枃浠?br />
tar xzf mongodb-linux-i386-latest.tgz
4 鍚姩鏈嶅姟
bin/mongod run &
5 浣跨敤鑷甫瀹㈡埛绔繛鎺?br />
/bin/mongo
6 嫻嬭瘯
db.foo.save( { a : 1 } )
db.foo.findOne()
windows涓嬶細(xì)
1 寤虹珛鏁版嵁鐩綍c:\data\db
2 涓嬭澆鍘嬬緝鍖咃紝瑙e帇鏂囦歡
3 鍚姩鏈嶅姟
bin\mongod.exe run
4 鑷甫瀹㈡埛绔?br />
bin\mongon.exe
鍦↙INUX鍜學(xué)INDOWS緋葷粺涓嬬殑浣跨敤澶у悓灝忓紓錛屼笉鍚岀殑鍦版柟涓昏鏄粯璁ょ殑鏁版嵁瀛樺偍鐩綍銆侺INUX綾葷郴緇熶笅瀛樻斁鍦?data/db涓嬶紝鑰學(xué)INDOWS
浼?xì)瀛樻攽譁–:\data\db涓嬨傚彲浠ュ湪鍚姩鏃朵嬌鐢?-dbpath鍙傛暟鎸囧畾瀛樺偍鐩綍騫跺惎鍔ㄣ傚錛歜in\mongod.exe --dbpath d:\data\mongo
甯哥敤鍚姩鍙傛暟錛?br />
run 鐩存帴鍚姩銆備緥錛?/mongod run
--dbpath 鎸囧畾鐗瑰畾瀛樺偍鐩綍鍚姩錛岃嫢鐩綍涓嶅瓨鍦ㄥ垯鍒涘緩銆備緥錛?/mongod --dbpath /var/data/mongo
--port 鎸囧畾绔彛鍚姩銆備緥錛?/mongod --port 12345
鍋滄MONGO鏈嶅姟錛?br />
鏂規(guī)硶1錛氭湇鍔$鍋滄錛屽彲浣跨敤Ctrl+C
鏂規(guī)硶2錛氬湪瀹㈡埛绔仠姝紝鍙厛榪炴帴瀹㈡埛绔?br />
./mongo
騫朵嬌鐢ㄥ懡浠?br />
db.shutdownerver()
鐒跺悗閫鍑哄鎴風(fēng)
exit
浣跨敤JAVA璇█鎿嶄綔MONGODB闈炲父綆鍗曪紝鍙灝嗛┍鍔ㄦ枃浠跺姞鍏ュ埌CLASSPATH涓氨鍙互浣跨敤銆?/p>
1 寤虹珛榪炴帴
瑕佸緩绔婱ongoDB鐨勮繛鎺ワ紝浣犲彧瑕佹寚瀹氳榪炴帴鍒扮殑鏁版嵁搴撳氨鍙互銆傝繖涓暟鎹簱涓嶄竴瀹氬瓨鍦紝濡傛灉涓嶅瓨鍦紝MongoDB浼?xì)鍏堜皋Z綘寤虹珛榪欎釜
搴撱傚悓鏃訛紝鍦ㄨ繛鎺ユ椂浣犱篃鍙互鍏蜂綋鎸囧畾瑕佽繛鎺ュ埌鐨勭綉緇滃湴鍧鍜岀鍙c備笅闈㈢殑鏄繛鎺ユ湰鏈烘暟鎹簱鐨勪竴浜涗緥瀛愶細(xì)
import com.mongodb.Mongo;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoAdmin;
Mongo db = new Mongo("mydb");
Mongo db = new Mongo("localhost", "mydb");
Mongo db = new Mongo("localhost", 27017, "mydb");
2 瀹夊叏楠岃瘉錛堥潪蹇呴夛級(jí)
MongoDB鏈嶅姟鍙互鍦ㄥ畨鍏ㄦā寮忚繍琛岋紝姝ゆ椂浠諱綍瀹㈡埛绔榪炴帴鏁版嵁搴撴椂闇浣跨敤鐢ㄦ埛鍚嶅拰瀵嗙爜銆傚湪JAVA涓彲浣跨敤濡備笅鏂規(guī)硶榪炴帴錛?/p>
boolean auth = db.authenticate(userName, password);
濡傛灉鐢ㄦ埛鍚嶅瘑鐮侀獙璇侀氳繃錛岃繑鍥炲間負(fù)true錛屽惁鍒欎負(fù)false
3 鑾峰彇闆嗗悎鍒楄〃
姣忎釜鏁版嵁搴撻兘瀛樺湪闆朵釜鎴栧涓泦鍚堬紝闇瑕佹椂浣犲彲浠ヨ幏寰椾粬浠殑鍒楄〃錛?/p>
Set<String> colls = db.getCollectionNames();
for(String s : colls){
System.out.println(s);
}
4 鑾峰緱涓涓泦鍚?br /> 瑕佽幏寰楁煇涓壒瀹氶泦鍚堬紝浣犲彲浠ユ寚瀹氶泦鍚堢殑鍚嶅瓧錛屽茍浣跨敤getCollection()鏂規(guī)硶錛?/p>
DBCollection coll = db.getCollection("testCollection");
褰撲綘鑾峰彇浜嗚繖涓泦鍚堝璞★紝浣犲氨鍙互瀵規(guī)暟鎹繘琛屽鍒犳煡鏀逛箣綾葷殑鎿嶄綔銆?/p>
5 鎻掑叆鏂囨。
褰撲綘鑾峰緱浜嗕竴涓泦鍚堝璞★紝浣犲氨鍙互鎶婃枃妗f彃鍏ュ埌榪欎釜瀵硅薄涓備緥濡傦紝瀛樺湪涓涓狫SON寮忕殑灝忔枃妗o細(xì)
{
"name" : "MongoDB",
"type" : "database",
"count" : 1,
"info" : {
x : 203,
y : 102
}
}
璇鋒敞鎰忥紝榪欎釜鏂囨。鍖呭惈涓涓唴閮ㄦ枃妗c傛垜浠彲浠ヤ嬌鐢˙asicDBObject綾繪潵鍒涘緩榪欎釜鏂囨。錛屽茍涓斾嬌鐢╥nsert()鏂規(guī)硶鏂逛究鍦板皢瀹冩彃鍏ュ埌闆?/p>
鍚堜腑銆?/p>
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);
coll.insert(doc);
6 浣跨敤findOne()鏌ユ壘闆嗗悎涓涓涓枃妗?br /> 瑕佹煡鎵炬垜浠笂涓姝ユ彃鍏ョ殑閭d釜鏂囨。錛屽彲浠ョ畝鍗曞湴浣跨敤findOne()鎿嶄綔鏉ヨ幏鍙栭泦鍚堜腑絎竴涓枃妗c傝繖涓柟娉曡繑鍥炰竴涓崟涓鏂囨。錛堣繖鏄浉瀵逛簬浣跨敤DBCursor鐨刦ind()鎿嶄綔鐨勮繑鍥烇級(jí)錛岃繖瀵逛簬鍙湁涓涓枃妗f垨鎴戜滑鍒氭彃鍏ョ涓涓枃妗f椂寰堟湁鐢紝鍥犱負(fù)姝ゆ椂騫朵笉闇瑕佷嬌鐢ㄥ厜鏍囥?/p>
DBObject myDoc = coll.findOne();
System.out.println(myDoc);
榪斿洖綾諱技錛?br />
{
"_id" : "ac907a1f5b9d5e4a233ed300" ,
"name" : "MongoDB" ,
"type" : 1 ,
"info" : {
"x" : 203 ,
"y" : 102} ,
"_ns" : "testCollection"
}
娉ㄦ剰_id鍜宊ns鍏冪礌鏄敱MongoDB鑷姩鍔犲叆浣犵殑鏂囨。銆傝浣忥細(xì)MongoDB鍐呴儴瀛樺偍浣跨敤鐨勫厓绱犲悕鏄互“_”鍋氫負(fù)寮濮嬨?/p>
7 鍔犲叆澶氱鏂囨。
涓轟簡鍋氭洿澶氭湁瓚g殑鏌ヨ璇曢獙錛岃鎴戜滑鍚戦泦鍚堜腑鍔犲叆澶氱鏂囨。綾誨瀷錛岃薄錛?br />
{
"i" : value
}
鍙互閫氳繃寰幆鏉ュ疄鐜?/p>
for(int i = 0; i < 100; i++){
coll.insert(new BasicDBObject().append("i", i));
}
娉ㄦ剰鎴戜滑鍙互鍦ㄤ竴涓泦鍚堜腑鎻掑叆涓嶅悓綾誨瀷鐨勬枃妗o紝榪欏氨鏄垜浠墍璇寸殑“妯″紡鑷敱”錛坰chema-free錛夈?/p>
8 緇熻鏂囨。鏁伴噺
浣跨敤getCount()鏂規(guī)硶
System.out.println(coll.getCount());
9 浣跨敤鍏夋爣錛坈ursor錛夋潵鑾峰彇鍏ㄩ儴鏂囨。
涓轟簡鑾峰彇闆嗗悎涓殑鎵鏈夋枃妗o紝鎴戜滑鍙互浣跨敤find()鏂規(guī)硶銆傝繖涓柟娉曡繑鍥炰竴涓奃BCursor瀵硅薄錛屾潵鍏佽鎴戜滑灝嗙鍚堟煡璇㈡潯浠剁殑鏂囨。榪唬
鍑烘潵銆?/p>
DBCursor cur = coll.find();
while(cur.hasNext()){
System.out.println(cur.next());
}
10 鍦ㄦ煡璇腑鑾峰彇鍗曚竴鏂囨。
鎴戜滑鍙互鍒涘緩涓涓煡璇紝騫朵紶閫掔粰find()鏂規(guī)硶鏉ヨ幏鍙栭泦鍚堜腑鎵鏈夋枃妗g殑涓涓瓙闆嗐備緥濡傦紝鎴戜滑鎯寵鏌ヨ鍩熷悕涓?i"錛屽茍涓斿間負(fù)71鐨勬枃妗o細(xì)
BasicDBObject query = new BasicDBObject();
query.put("i", 71);
cur = coll.find(query);
while(cur.hasNext()){
System.out.println(cur.next());
}
11 浣跨敤鏉′歡鏌ヨ鑾峰彇闆嗗悎
渚嬪錛屾垜浠兂瑕佹煡璇㈡墍鏈塱>50鐨勬枃妗o細(xì)
BasicDBObject query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 50));
cur = coll.find(query);
while(cur.hasNext()){
System.out.println(cur.next());
}
褰撶劧錛屾垜浠篃鍙互鍋?0 < i <= 30鐨勬煡璇?/p>
BasicDBObject query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30));
cur = coll.find(query);
while(cur.hasNext()){
System.out.println(cur.next());
}
12 鍒涘緩绱㈠紩
MongoDB鏀寔绱㈠紩錛岃屼笖寰堝鏄撳湪闆嗗悎涓婂鍔犵儲(chǔ)寮曘傝鍒涘緩绱㈠紩錛屽彧闇瑕佹寚瀹氳鍔犵儲(chǔ)寮曠殑灞炴э紝騫朵笖鎸囧畾鍗囧簭錛?錛夋垨闄嶅簭鍗沖彲錛?1錛夈?/p>
coll.createIndex(new BasicDBObject("i", 1));
13 鑾峰彇绱㈠紩鍒楄〃
List<DBObject> list = coll.getIndexInfo();
for(DBObject o : list){
System.out.println(o);
}
14 MongoDB綆$悊鍑芥暟
綆$悊鍑芥暟鍦╟om.mongodb.MongoAdmin綾諱腑瀹氫箟銆?br />
渚婣錛氳幏鍙栨暟鎹簱鍒楄〃
MongoAdmin admin = new MongoAdmin();
for(String s : admin.getDatabaseNames()){
System.out.println(s);
}
渚婤錛氳幏鍙栨暟鎹簱瀵硅薄
Mongo m = admin.getDB("mydb");
渚婥錛氬垹闄ゆ暟鎹簱
admin.dropDatabase("mydb");
15 鐢―BObject瀛樺偍JAVA瀵硅薄
MongoDB for JAVA椹卞姩涓彁渚涗簡鐢ㄤ簬鍚戞暟鎹簱涓瓨鍌ㄦ櫘閫氬璞$殑鎺ュ彛DBObject
渚嬪錛屽瓨鍦ㄤ竴涓渶瑕佸瓨鍌ㄧ殑瀵硅薄綾籘weet
public class Tweet implements DBObject{
/*...*/
}
鍙互浣跨敤濡備笅浠g爜錛?/p>
Tweet myTweet = new Tweet();
myTweet.put("user", userId);
myTweet.put("message", message);
myTweet.put("date", new Date());
collection.insert(myTweet);
褰撲竴涓枃妗d粠MongoDB涓彇鍑烘椂錛屽畠浼?xì)鑷姩鎶婃枃妗h浆鎹㈡垚DBObject鎺ュ彛綾誨瀷錛岃灝嗗畠瀹炰緥鍖栦負(fù)浣犵殑瀵硅薄錛岄渶浣跨敤
DBCollection.setObjectClass()銆?br />
collection.setObjectClass(Tweet);
Tweet myTweet = (Tweet)collection.findOne();
16 JAVA椹卞姩鐨勫茍鍙戞?br /> JAVA鐨凪ongoDB椹卞姩鏄嚎紼嬪畨鍏ㄧ殑銆傚鏋滀綘灝嗗畠鐢ㄥ湪WEB鏈嶅姟涓紝鍙互鍒涘緩瀹冪殑涓涓崟渚嬶紝騫跺湪鎵鏈夎姹備腑浣跨敤瀹冦?/p>
鐒惰岋紝濡傛灉浣犻渶瑕佸湪涓涓細(xì)璇濓紙渚嬪HTTP璇鋒眰錛変腑淇濊瘉浜嬪姟涓鑷存э紝涔熻浣犱細(xì)甯屾湜鍦ㄨ繖涓細(xì)璇濅腑瀵歸┍鍔ㄤ嬌鐢ㄥ悓涓涓鍙c傝繖浠呬粎鍦?/p>
璇鋒眰閲忛潪甯稿ぇ鐨勭幆澧冧腑錛屼緥濡備綘緇忓父浼?xì)璇诲彇鍒氬啓鍏ョ殑鏁版嵁銆?br />
涓轟簡榪欎竴鐐癸紝浣犻渶瑕佷嬌鐢ㄥ涓嬩唬鐮侊細(xì)
Mongo m;
m.restartStart();
// code.........
m.requestDone();
浠ヤ笂浠嬬粛浜嗙畝鍗曠殑mongoDB浣跨敤錛屾洿澶氫俊鎭鏌ラ槄MongoDB API for Java銆?/p>
CODE:
> show dbs
CODE:
> db
Admin
鎯沖垏鎹㈠埌test鏁版嵁搴撲笅闈?
CODE:
> use test
switched to db test
> db
Test
鎯蟲煡鐪媡est涓嬫湁鍝簺琛ㄦ垨鑰呭彨collection錛屽彲浠ヨ緭鍏?
CODE:
> show collections
system.indexes
user
鎯崇煡閬搈ongodb鏀寔鍝簺鍛戒護(hù)錛屽彲浠ョ洿鎺ヨ緭鍏elp
CODE:
> 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 with 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 further iterate
濡傛灉鎯崇煡閬撳綋鍓嶆暟鎹簱鏀寔鍝簺鏂規(guī)硶錛?
CODE:
> db.help();
DB methods:
db.addUser(username, password) 娣誨姞鏁版嵁搴撴巿鏉冪敤鎴?br />
db.auth(username, password) 璁塊棶璁よ瘉
db.cloneDatabase(fromhost) 鍏嬮殕鏁版嵁搴?br />
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) 澶嶅埗鏁版嵁搴?br />
db.createCollection(name, { size : ..., capped : ..., max : ... } ) 鍒涘緩琛?br />
db.currentOp() displays the current operation in the db
db.dropDatabase() 鍒犻櫎褰撳墠鏁版嵁搴?br />
db.eval_r(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames() 鑾峰彇褰撳墠鏁版嵁搴撶殑琛ㄥ悕
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
db.getName()
db.getPrevError()
db.getProfilingLevel()
db.getReplicationInfo()
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() kills the current operation in the db
db.printCollectionStats() 鎵撳嵃鍚勮〃鐨勭姸鎬佷俊鎭?br />
db.printReplicationInfo() 鎵撳嵃涓繪暟鎹簱鐨勫鍒剁姸鎬佷俊鎭?br />
db.printSlaveReplicationInfo() 鎵撳嵃浠庢暟鎹簱鐨勫鍒剁姸鎬佷俊鎭?br />
db.printShardingStatus() 鎵撳嵃鍒嗙墖鐘舵佷俊鎭?br />
db.removeUser(username) 鍒犻櫎鏁版嵁搴撶敤鎴?br />
db.repairDatabase() 淇鏁版嵁搴?br />
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }
db.setProfilingLevel(level) 0=off 1=slow 2=all
db.shutdownServer()
db.version() current version of the server
濡傛灉鎯崇煡閬撳綋鍓嶆暟鎹簱涓嬬殑琛ㄦ垨鑰呰〃collection鏀寔鍝簺鏂規(guī)硶錛屽彲浠ヤ嬌鐢ㄤ竴涓嬪懡浠ゅ錛?
CODE:
> db.user.help(); user涓鴻〃鍚?br />
DBCollection help
db.foo.count() 緇熻琛ㄧ殑琛屾暟
db.foo.dataSize() 緇熻琛ㄦ暟鎹殑澶у皬
db.foo.distinct( key ) - eg. db.foo.distinct( 'x' ) 鎸夌収緇欏畾鐨勬潯浠墮櫎閲?br />
db.foo.drop() drop the collection 鍒犻櫎琛?br />
db.foo.dropIndex(name) 鍒犻櫎鎸囧畾绱㈠紩
db.foo.dropIndexes() 鍒犻櫎鎵鏈夌儲(chǔ)寮?br />
db.foo.ensureIndex(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups 澧炲姞绱㈠紩
db.foo.find( [query] , [fields]) -
first parameter is an optional query filter. second parameter is
optional set of fields to return. 鏍規(guī)嵁鏉′歡鏌ユ壘鏁版嵁
-----------------------
閫氳繃鏉′歡鏌ヨ錛?db.foo.find( { x : 77 } , { name : 1 , x : 1 } )
-----------------------------
db.foo.find(...).count()
db.foo.find(...).limit(n) 鏍規(guī)嵁鏉′歡鏌ユ壘鏁版嵁騫惰繑鍥炴寚瀹氳褰曟暟
db.foo.find(...).skip(n)
db.foo.find(...).sort(...) 鏌ユ壘鎺掑簭
db.foo.findOne([query]) 鏍規(guī)嵁鏉′歡鏌ヨ鍙煡璇竴鏉℃暟鎹?br />
db.foo.getDB() get DB object associated with collection 榪斿洖琛ㄦ墍灞炵殑搴?br />
db.foo.getIndexes() 鏄劇ず琛ㄧ殑鎵鏈夌儲(chǔ)寮?br />
db.foo.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } ) 鏍規(guī)嵁鏉′歡鍒嗙粍
db.foo.mapReduce( mapFunction , reduceFunction , <optional params> )
db.foo.remove(query) 鏍規(guī)嵁鏉′歡鍒犻櫎鏁版嵁
db.foo.renameCollection( newName ) renames the collection 閲嶅懡鍚嶈〃
db.foo.save(obj) 淇濆瓨鏁版嵁
db.foo.stats() 鏌ョ湅琛ㄧ殑鐘舵?br />
db.foo.storageSize() - includes free space allocated to this collection 鏌ヨ鍒嗛厤鍒拌〃絀洪棿澶у皬
db.foo.totalIndexSize() - size in bytes of all the indexes 鏌ヨ鎵鏈夌儲(chǔ)寮曠殑澶у皬
db.foo.totalSize() - storage allocated for all data and indexes 鏌ヨ琛ㄧ殑鎬誨ぇ灝?br />
db.foo.update(query, object[, upsert_bool]) 鏍規(guī)嵁鏉′歡鏇存柊鏁版嵁
db.foo.validate() - SLOW 楠岃瘉琛ㄧ殑璇︾粏淇℃伅
db.foo.getShardVersion() - only for use with sharding
Mongodb鐨勫浠藉伐鍏穖ongodumpCODE:
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongodump --help
options:
--help produce help message
-h [ --host ] arg mongo host to connect to
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod data files in this path,
instead of connecting to a mongod instance
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
-o [ --out ] arg (=dump) output directory
[falcon@www.fwphp.cn ~/mongodb/bin]$ [color=Blue]./mongodump -d test -o test/[/color]
connected to: 127.0.0.1
DATABASE: test to test/test
test.user to test/test/user.bson
100000 objects
test.system.indexes to test/test/system.indexes.bson
1 objects
[falcon@www.fwphp.cn ~/mongodb/bin]$ ls
2 mongo mongodump mongofiles mongorestore mongosniff
dump mongod mongoexport mongoimport mongos test
MongoDB鐨勬暟鎹仮澶嶅伐鍏穖ongorestoreCODE:
> show collections
system.indexes
User
鍒犻櫎user琛?
CODE:
> db.user.drop();
True
> show collections
System.indexes
鐜板湪鍒╃敤mongorestore琛ㄦ仮澶嶅垰鎵嶅埄鐢╩ongodump澶囦喚鐨勬暟鎹?
CODE:
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongorestore --help
usage: ./mongorestore [options] [directory or filename to restore from]
options:
--help produce help message
-h [ --host ] arg mongo host to connect to
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod data files in this path,
instead of connecting to a mongod instance
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
[falcon@www.fwphp.cn ~/mongodb/bin]$ ./mongorestore -d test -c user test/test/user.bson
connected to: 127.0.0.1
test/test/user.bson
going into namespace [test.user]
100000 objects
User琛ㄤ腑鐨?0w鏉¤褰曞凡緇忔仮澶?
CODE:
> show collections
system.indexes
user
> db.user.find();
{ "_id" : ObjectId("4b9c8db08ead0e3347000000"), "uid" : 1, "username" : "Falcon.C-1" }
{ "_id" : ObjectId("4b9c8db08ead0e3347010000"), "uid" : 2, "username" : "Falcon.C-2" }
{ "_id" : ObjectId("4b9c8db08ead0e3347020000"), "uid" : 3, "username" : "Falcon.C-3" }
{ "_id" : ObjectId("4b9c8db08ead0e3347030000"), "uid" : 4, "username" : "Falcon.C-4" }
{ "_id" : ObjectId("4b9c8db08ead0e3347040000"), "uid" : 5, "username" : "Falcon.C-5" }
.................
has more