您的位置:首页 > 数据库 > Mongodb

MongoDB常用操作

2016-08-18 19:06 381 查看

批量更改字段名

db.getCollection('db_hosts').update({},
{$rename :
{"aggregate_top_5_avg_cpu_metric" : "aggregate_cpu_metric_top_5_avg"
}
}, {multi:true}
)


批量删除字段与值

db.getCollection('db_hosts').update({},
{$unset :
{"aggregate_cpu_metric_top_5_avg" : ""}
}
, {multi:true}
)


添加索引

db.getCollection('db_hosts').ensureIndex({'value': -1})


添加组合索引并去重

db.getCollection('db_memory_metric').ensureIndex(
{"host":1,"BusinessMinute":1},
{'unique': true, 'dropDups': true}
)


复杂查询

因为字典不支持两个
$or
所以以下写法是不支持的:

{
$or: [{a: 2}, {a: 3}],
$or: [{b: 5}, {b: 4}]
}


奇葩的是,以上写法是有结果的,但是只有最后一个
$or
结果有效,难以理解

所以mongo2.0引入
$and
↓↓↓↓

{
$and: [
{$or : [{'a':2},{'b':3}]},
{$or : [{'a':4},{'b':5}]}
]
}


所以如果fiter包含两个
$or
就必须用
$and
包住,具体如下:

db.getCollection('aggregate_day_host_metric2').find({"$and":[
{"BusinessDay":{"$gte":17043,"$lte":17043}},
{"$or":[
{"cpu_metric.top_5_avg":{"$gte":0,"$lte":20}},
{"memory_metric.top_5_avg":{"$gte":0,"$lte":19}}]},
{"$or":[
{"host.az_id":4001},
{"host.az_id":5001}]}
]
})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: