1
2
3
4
5
|
cat /etc/yum.repos.d/
10
.repo
[10gen]
name=10gen Repository
baseurl=http:
//downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=
0
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from pymongo
import
* # 导包
con = Connection(...) # 链接
db = con.database # 链接数据库
db.authenticate(
'username'
,
'password'
) # 登录
db.drop_collection(
'users'
) #删除表
db.logout() # 退出
db.collection_names() # 查看所有表
db.users.count() # 查询数量
db.users.find_one({
'name'
:
'xiaoming'
}) # 单个对象
db.users.find({
'age'
:
18
}) # 所有对象
db.users.find({
'id'
:
64
}, {
'age'
:
1
,
'_id'
:
0
}) # 返回一些字段 默认_id总是返回的
0
不返回
1
返回
db.users.find({}).sort({
'age'
:
1
}) # 排序
db.users.find({}).skip(
2
).limit(
5
) # 切片
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#!/usr/bin/env python
from pymongo
import
Connection
import
time,datetime
import
os,sys
connection = Connection(
'127.0.0.1'
,
27017
)
db = connection[
'xiaorui'
]
def func_time(func):
def _wrapper(*args,**kwargs):
start = time.time()
func(*args,**kwargs)
print func.__name__,
'run:'
,time.time()-start
return
_wrapper
@func_time
def ainsert(num):
posts = db.userinfo
for
x
in
range(num):
post = {
"_id"
: str(x),
"author"
: str(x)+
"Mike"
,
"text"
:
"My first blog post!"
,
"tags"
: [
"xiaorui"
,
"xiaorui.cc"
,
"rfyiamcool.51cto"
],
"date"
: datetime.datetime.utcnow()}
posts.insert(post)
if
__name__ ==
"__main__"
:
num = sys.argv[
1
]
ainsert(
int
(num))
|
1
2
3
4
5
6
7
8
9
10
11
12
|
def goodinsert(a):
posts.insert(a)
def ainsert(num):
for
x
in
range(num):
post = {
"_id"
: str(x),
"author"
: str(x)+
"Mike"
,
"text"
:
"My first blog post!"
,
"tags"
: [
"mongodb"
,
"python"
,
"pymongo"
],
"date"
: datetime.datetime.utcnow()}
# goodinsert(post)
a=threading.Thread(target=goodinsert,args=(post,))
a.start()
|
1
2
3
|
cat /etc/security/limits.conf
* soft nofile
102400
* hard nofile
102400
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
cat /etc/sysctl.conf
net.ipv4.tcp_syncookies =
1
net.ipv4.tcp_tw_reuse =
1
net.ipv4.tcp_tw_recycle =
1
net.ipv4.tcp_timestsmps =
0
net.ipv4.tcp_synack_retries =
2
net.ipv4.tcp_syn_retries =
2
net.ipv4.tcp_wmem =
8192
436600
873200
net.ipv4.tcp_rmem =
32768
436600
873200
net.ipv4.tcp_mem =
94500000
91500000
92700000
net.ipv4.tcp_max_orphans =
3276800
net.ipv4.tcp_fin_timeout =
30
#直接生效
/sbin/sysctl -p
|
1
|
多核问题可以在启动时加入启动参数: numactl --interleave=all
|
1
2
3
|
posts = db.userinfo
for
i
in
posts.find({
"author"
:re.compile(
'^2.Mike'
)}):
print i
|