进入mongo
/usr/local/mongodb/bin/mongo --host 10.1.1.111:27017
查看所有db
show dbs
查看当前进入的db
db
查看当前db的所有collection
show collections
创建collection(table)
db.createCollection("myindex", {size:200,capped:true,max:1000})
删除collection
db.mytable.drop();
新增一条记录
db.myindex.insert({'uid':1,'username':'admin'})
查看
http://docs.mongodb.org/manual/reference/method/db.collection.find/
http://docs.mongodb.org/manual/tutorial/query-documents/
http://www.cnblogs.com/TankMa/archive/2011/06/08/2074947.html
http://codefather.pl/post/1369797147/nosql-no-problem-norm-and-mongodb-tutorial
原型find($where, $field)
$where表示条件
$field表示选择哪些字段
返回值是cursor,中文翻译为游标
查看所有的条目
db.myindex.find()
根据条件查询:
db.storage_soft.find({'soft_id':2});
根据条件查询只选择某些字段:
db.storage_soft.find({'soft_id':2}, {'soft_download_times':1});
skip && limit
查询第1条记录:
db.storage_soft.find().limit(1);
查询前N条记录:
db.storage_soft.find().limit(N);
skip:查询soft_id大于2的情况下,跳过前10个,取第10+1,到10+N个,相当于limit 10,1
> db.storage_soft.find({'soft_id':{'$gt':2}},{'soft_id':1}).skip(10).limit(1);
{ "_id" : ObjectId("534f674d0114e67f4d000322"), "soft_id" : 20 }
sort函数
降序:
db.storage_soft.find({},{'soft_id':1}).sort({soft_id:-1}).limit(1);
升序:
db.storage_soft.find({},{'soft_id':1}).sort({soft_id:1}).limit(1);
distinct
> db.storage_soft.runCommand({'distinct':'storage_soft', 'key':'soft_status',query:{'soft_id':{'$gt':0,'$lt':10}}});
{
"values" : [
1
],
"stats" : {
"n" : 7,
"nscanned" : 25297,
"nscannedObjects" : 25297,
"timems" : 123,
"cursor" : "BasicCursor"
},
"ok" : 1
}
http://docs.mongodb.org/manual/reference/command/distinct/
关于find的where条件
相等:db.storage_soft.find({'soft_id':1},{'soft_id':1});
大于:{'soft_id':{'$gt':1}}
db.storage_soft.findOne({'soft_id':{'$gt':1}},{'soft_id':1});
小于:{'soft_id':{'$lt':10}}
db.storage_soft.findOne({'soft_id':{'$lt':10}},{'soft_id':1});
大于等于:{'soft_id':{'$gte':1}}
小于等于:{'soft_id':{'$lte':10}}
$ne:不等
{'soft_id':{'$ne':1}
$eq:没有这个操作
> db.storage_soft.find({'soft_id':{'$eq':1}}).limit(1);
error: { "$err" : "invalid operator: $eq", "code" : 10068 }
$in: 软件id为in列表中的其中一个值
{'soft_id':{'$in':[1,2,3]}}
错误:{'soft_id':{'$in':1}}
$nin: 软件id均不等于in列表中的任何一个值
{'soft_id':{'$in':[1,2,3]}}
$all: 几个条件必须同时满足
db.storage_soft.find({'soft_prev_baoku_subcate_id':{'$all':['173','172']}},{'soft_id':1}).count();
http://docs.mongodb.org/manual/reference/operator/query/all/
$exists: 某个字段是存在的
如果某条记录上的soft_prev_baoku_subcate_id的值是null,那么用$exists:true查询,这条记录是符合条件的
某条记录上的soft_prev_baoku_subcate_id这个字段不存在,用$exists:true查询,这条记录不满足条件,也不会被查到
db.storage_soft.find({'soft_prev_baoku_subcate_id':{'$exists':true}},{'soft_id':1}).count();
http://docs.mongodb.org/manual/reference/operator/query/exists/
多个查询条件
或:软件id或者为1,或者为10
{'$or':[{'soft_id':1},{'soft_id':10}]}
且:软件id为1,并且为10
{'$and':[{'soft_id':1},{'soft_id':10}]}
且:软件id大于1,并且小于5
> db.storage_soft.find({'$and':[{'soft_id':{'$gt':1}},{'soft_id':{'$lt':5}}]},{'soft_id':1});
{ "_id" : ObjectId("534f674b0114e67f4d000121"), "soft_id" : 2 }
{ "_id" : ObjectId("534f674c0114e67f4d00015b"), "soft_id" : 3 }
{ "_id" : ObjectId("534f674c0114e67f4d000166"), "soft_id" : 4 }
且,且:软件id大于1,并且软件id小于5,并且软件状态为1
> db.storage_soft.find({'$and':[{'soft_id':{'$gt':1}},{'soft_id':{'$lt':5}},{'soft_status':1}]},{'soft_id':1,'soft_status':1});
{ "_id" : ObjectId("534f674b0114e67f4d000121"), "soft_id" : 2, "soft_status" : 1 }
{ "_id" : ObjectId("534f674c0114e67f4d00015b"), "soft_id" : 3, "soft_status" : 1 }
{ "_id" : ObjectId("534f674c0114e67f4d000166"), "soft_id" : 4, "soft_status" : 1 }
$regex 正则匹配:软件名中带有“abc”的软件
db.storage_soft.find({'soft_name':{'$regex':'test'}},{'soft_id':1}).count();
正则匹配:软件名中带有“abc”,但不区分大小写
db.storage_soft.find({'soft_name':/test/i},{'soft_id':1}).count();
i模式表示大小写不敏感。
正则匹配:软件名中带有“abc”,但需区分大小写
db.storage_soft.find({'soft_name':/test/},{'soft_id':1}).count();
http://docs.mongodb.org/manual/reference/operator/query/regex/
子字段subfield:edition_plugin字段的flag字段为'2'
db.storage_soft_edition.findOne({'edition_plugin.flag':'2'});
某个字段中可以存多个值,其中一个值命中了,就被选出来
db.storage_soft.find({'soft_prev_baoku_subcate_id':{'$in':['172']}},{'soft_id':1}).count();
> db.storage_soft.find({'soft_prev_baoku_subcate_id':{'$in':['172']}},{'soft_id':1,'soft_prev_baoku_subcate_id':1});
{ "_id" : ObjectId("534f674a0114e67f4d000000"), "soft_id" : NumberLong(1), "soft_prev_baoku_subcate_id" : [ "172" ] }
{ "_id" : ObjectId("534f674b0114e67f4d000121"), "soft_id" : 2, "soft_prev_baoku_subcate_id" : [ "172", "173" ] }
某个字段只有一个值,但我给出多个值,只要这个字段和其中的一个值相等,就被选出来
> db.storage_soft.find({'soft_id':{'$in':[1,2,3]}},{'soft_id':1});
{ "_id" : ObjectId("534f674a0114e67f4d000000"), "soft_id" : NumberLong(1) }
{ "_id" : ObjectId("534f674b0114e67f4d000121"), "soft_id" : 2 }
{ "_id" : ObjectId("534f674c0114e67f4d00015b"), "soft_id" : 3 }
更新记录
http://docs.mongodb.org/manual/reference/method/db.collection.update/
db.myindex.update({'username':'admin'},{'$set':{'uid':2}},false)
db.collection.update( <query>, <update>, { upsert: <boolean> = false, multi: <boolean> = false, writeConcern: <document> } )
db.storage_soft_cate.update({cate_id:191}, {$set:{cate_id:167}})
db.storage_soft_cate.find({'cate_id':167});
db.storage_soft_cate.find({"_id":"534f69260114e67f4d01f456"})
db.storage_soft.update({'_id':ObjectId('534f674b0114e67f4d000121')}, {'$set':{'soft_download_times':1}}, false);
db.storage_soft.find({'_id':ObjectId('534f674a0114e67f4d000000')});
删除记录
db.myindex.remove({'uid':2},true)
db.collection.remove( <query>, <justOne> )
can't remove from a capped collection
查看当前table的collection条目数
db.myindex.count()
创建索引
> db.myindex.ensureIndex({'uid':1})
查看索引
> db.myindex.getIndexes(); [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test_2.myindex", "name" : "_id_" }, { "v" : 1, "key" : { "uid" : 1 }, "ns" : "test_2.myindex", "name" : "uid_1" }, { "v" : 1, "key" : { "uid" : -1 }, "ns" : "test_2.myindex", "name" : "uid_-1" }
走索引
> db.myindex.find({'uid':1}).explain() { "cursor" : "BtreeCursor uid_1", "isMultiKey" : false, "n" : 0, "nscannedObjects" : 0, "nscanned" : 0, "nscannedObjectsAllPlans" : 0, "nscannedAllPlans" : 0, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "uid" : [ [ 1, 1 ] ] }, "server" : "corp:2" }
不走索引
> db.myindex.find({'username':1}).explain() { "cursor" : "ForwardCappedCursor", "isMultiKey" : false, "n" : 0, "nscannedObjects" : 4, "nscanned" : 4, "nscannedObjectsAllPlans" : 4, "nscannedAllPlans" : 4, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { }, "server" : "fet:2" }
按条件查询操作
相等
> db.myindex.find({'uid':2}) { "_id" : ObjectId("525fc1d78a7af6409b333a4a"), "uid" : 2, "username" : "admin" } { "_id" : ObjectId("525fc1ec8a7af6409b333a4b"), "uid" : 2, "username" : "admin" } { "_id" : ObjectId("525fcab28a7af6409b333a4c"), "uid" : 2, "username" : "admin2" }
大于
> db.myindex.find({'uid': {$gt: 2}} ) { "_id" : ObjectId("525fcac28a7af6409b333a4d"), "uid" : 3, "username" : "admin3" }
多个条件与
> db.myindex.find({'uid': {$gt: 2}, 'username':'admin3'} ) { "_id" : ObjectId("525fcac28a7af6409b333a4d"), "uid" : 3, "username" : "admin3" }
查看所有元素
> db.myindex.find() { "_id" : ObjectId("525fc1d78a7af6409b333a4a"), "uid" : 2, "username" : "admin" } { "_id" : ObjectId("525fc1ec8a7af6409b333a4b"), "uid" : 2, "username" : "admin" } { "_id" : ObjectId("525fcab28a7af6409b333a4c"), "uid" : 2, "username" : "admin2" } { "_id" : ObjectId("525fcac28a7af6409b333a4d"), "uid" : 3, "username" : "admin3" }
in操作
> db.myindex.find({'uid':{$in:[2,3]}}) { "_id" : ObjectId("525fc1d78a7af6409b333a4a"), "uid" : 2, "username" : "admin" } { "_id" : ObjectId("525fc1ec8a7af6409b333a4b"), "uid" : 2, "username" : "admin" } { "_id" : ObjectId("525fcab28a7af6409b333a4c"), "uid" : 2, "username" : "admin2" } { "_id" : ObjectId("525fcac28a7af6409b333a4d"), "uid" : 3, "username" : "admin3" }
range操作
> db.myindex.find({'uid':{$gt:2,$lt:4}}) { "_id" : ObjectId("525fcac28a7af6409b333a4d"), "uid" : 3, "username" : "admin3" }
or操作
> db.myindex.find({'$or':[{'username':'admin2'},{'username':'admin3'}]}) { "_id" : ObjectId("525fcab28a7af6409b333a4c"), "uid" : 2, "username" : "admin2" } { "_id" : ObjectId("525fcac28a7af6409b333a4d"), "uid" : 3, "username" : "admin3" }
删除操作 remove
db.storage_soft.remove({soft_id:{"$gt":12000}})