Skip to content

Instantly share code, notes, and snippets.

@aclisp
Created December 12, 2019 09:32
Show Gist options
  • Save aclisp/f7db2307b2c3980afb9f9732958e8d2e to your computer and use it in GitHub Desktop.
Save aclisp/f7db2307b2c3980afb9f9732958e8d2e to your computer and use it in GitHub Desktop.
MongoDB Shell Scripts
var now = Math.floor(Date.now() / 1000)
var db = connect("10.70.0.140:10002/admin", "ikdx_admin", "vws6XGl78Q4D")
db = db.getSiblingDB("channel_biz")
var table = db.getCollection('fixed_pos_channels')
var cursor = table.aggregate([
{"$group": {_id:"$cid", count:{$sum:1}}}
])
var dup_cid = []
// 找出所有的重复cid
while ( cursor.hasNext() ) {
row = cursor.next()
if (row.count > 1) {
dup_cid.push(row._id)
}
}
var delete_id = []
// 只保留最新时间段的cid
function keep_latest(cid) {
var cursor = table.find({"cid": cid})
while ( cursor.hasNext() ) {
row = cursor.next()
remove = " "
if (row.end_time < now) {
remove = "!"
delete_id.push(row._id)
}
print(remove, row.short_cid, row.cid, row.start_time, row.end_time)
}
}
for (var i=0; i<dup_cid.length; i++) {
cid = dup_cid[i]
keep_latest(cid)
}
// 删除之前备份表
for (var i=0; i<delete_id.length; i++) {
id = delete_id[i]
table.remove({_id: id})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment