MongoDB常用操作

批量更改字段名

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}]}
]
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值