记录一下最近用到的命令或方法
一。查询重复值
#可以显示重复字段名和重复几次
db.集合名.aggregate({
$group:{
'_id':{'查询重复字段名','$查询重复字段名'},
'uniquedIds':{$addToSet:'$_id'},
'count':{$sum:1}
},{$math:{
'count':{$gt:1}}
})
二。python根据excel更新mongodb的值(需要用admin账号不然会出现权限问题,需要离线导入pyhon包点击一下连接)
import pymongo
import xlrd
myclient = pymongo.MongoClient("mongodb://admin:password@ip:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["PointDevice"]
# for x in mycol.find():
# print(x)
#读取excel
book = xlrd.open_workbook('严管.xlsx')
sheet1 = book.sheets()[0]
nrows = sheet1.nrows
print('表格总行数',nrows)
# ncols = sheet1.ncols
# print('表格总列数',ncols)
#
#row3_values = sheet1.row_values(2)
#print('第3行值',row3_values)
col3_values = sheet1.col_values(2)
print('第3列值',col3_values)
# 遍历
for i in range(nrows-1):
i = i + 1
#print(sheet1.row_values(i))
#点位名
cell_i_1 = sheet1.cell(i, 1).value
#路口编号
cell_i_3 = sheet1.cell(i, 3).value
#道路编号
cell_i_4 = sheet1.cell(i, 4).value
#l路段 编号
cell_i_5 = sheet1.cell(i, 5).value
#点位名
myquery = {"channelName": cell_i_1}
#查询路口编号是否为空
cross_t = mycol.find_one({"cross": cell_i_3})
if cross_t is None:
#修改为严管和其他编号
newvalues= {"$set": {"isControl": "1", "cross": cell_i_3, "road": cell_i_4, "block": cell_i_5}}
# 更新
mycol.update_one(myquery, newvalues)
print("修改点位:"+cell_i_1+":改为严管和补充编号")
else:
#修改为严管
newvalues = {"$set": {"isControl": "1"}}
# 更新
mycol.update_one(myquery, newvalues)
print("修改点位:"+cell_i_1+":改为为严管无需补编号")




