美文网首页mongoDB+ES
mongodb插入字段,更新数据,查询限制(python)

mongodb插入字段,更新数据,查询限制(python)

作者: 布口袋_天晴了 | 来源:发表于2019-11-12 14:45 被阅读0次

插入字段--关键语句

db.your_table_name.update(
#更新条件
{"_id": ent['_id']}, 
#更新内容
{'$set': {'case_reason': final_key}}, 
multi=True)

查询限制--关键语句(根据_id字段,一个一个地更新新加入的case_reason字段的值)

db.chongqing.find({},{"content.title":1,}).limit(100)
db.your_table_name.find({},{"your_segement":1,}).limit(your_count)

具体代码

"""
date:2019-11-12
author:bukoudai_tianqingle
function:mongodb insert a "case_reason" segement
"""
import pymysql
import re
import pymongo

class init_MongoDB:
    def __init__(self, host=None, port=None, user=None, pwd=None):
        self.host = host
        self.port = port
        self.user = user
        self.pwd = pwd
        self.client = pymongo.MongoClient(self.host, self.port)

    def connection(self, database):
        db = self.client.admin
        if self.pwd is not None:
            db.authenticate(self.user, self.pwd, mechanism="SCRAM-SHA-1")
        my_db = self.client[database]

        return my_db


class CaseReason():
    def __init__(self):
        self.get_class()
        self.get_mongodb_data()

    def get_class(self):
        self.key_list = []
        with open('mongo_anyou_class.txt','r',encoding='utf8') as f:
            line = f.readline()
            while line:
                if(len(line)>1):
                    matchObj = re.match(r'(.*)(\d+、)(.*)', line)
                    if (matchObj):
                        key_word = matchObj.group(3)
                        # print(key_word)
                        self.key_list.append(key_word)
                    else:
                        matchObj2 = re.match(r'(.*)[(](\d+)[)](.*)',line)
                        if (matchObj2):
                            key_word = matchObj2.group(3)
                            # print('22222',key_word)
                            self.key_list.append(key_word)
                line = f.readline()

    def get_mongodb_data(self):
        mongodb = init_MongoDB(host='your_mongo_host', port=27017, user='admin')
        db = mongodb.connection("law")  # 数据库名
        all_count=db.chongqing.find().count()
        entities = db.chongqing.find({},{"content.title":1,})
        for pos, ent in enumerate(entities):
            content = ent['content']
            title = content['title']
            print(pos, all_count)
            f_k = ''
            for key in self.key_list:
                if(title.find(key)!=-1):
                    f_k = f_k + '+' + key
            if(len(f_k)>1):
                # print(f_k)
                tep = f_k.split('+')
                final_key = ''
                max_len = 0
                for t in tep:
                    if(len(t)>max_len):
                        max_len = len(t)
                        final_key = t
                # print(final_key)
                db.chongqing.update({"_id": ent['_id']}, {'$set': {'case_reason': final_key}}, multi=True)
            else:
                # print('no')
                db.chongqing.update({"_id": ent['_id']}, {'$set': {'case_reason': 'not find'}}, multi=True)

CaseReason()

相关文章

  • mongodb插入字段,更新数据,查询限制(python)

    插入字段--关键语句 查询限制--关键语句(根据_id字段,一个一个地更新新加入的case_reason字段的值)...

  • Node.js官方mongodb驱动

    准备 一些mongodb命令 连接mongodb 插入数据 更新数据 删除数据 查询数据 参考https://gi...

  • MySQL插入更新与删除

    [TOC] 插入数据 不指定具体字段名 需要全部的值 指定具体字段名 插入多条数据 将查询结果插入表中 更新数据 ...

  • day18(MongoDB)

    安装MongoDB 配置启动: 优化警告 插入数据 查询数据 更新数据 删除数据 mongostat mongot...

  • SQL语句整理

    显示 创建表和数据库 数据类型 插入行 查询 普通查询 条件查询 排序 限制条数 多表联合查询 更新(替换) 删除...

  • Tp5_查询构造器,实现增删改查操作.

    1.插入操作//单挑数据 //多条数据 2.更新操作 3.查询操作查询单条字段 4.删除操作

  • mongodb及express框架(0812)

    安装mongodb mongodb增删改查操作 插入数据 查询数据 插入多条数据 切换数据库并进入 test 修改...

  • SQL语句汇总(方便粘贴sqlite语句)

    DDL语句 删除表 创建表 DML语句 插入语句 更新数据 删除数据 DQL语句 基本查询 查询特殊的字段 通过条...

  • MongoDB 数据类型查询 — $type使用

    MongoDB 使用过程中经常需要根据字段的类型来查询数据, 而MongoDB中查询字段类型是通过$type操作符...

  • Mysql、MongoDB与Redis的特性

    MongoDB: 非关系型数据库、面向文档存储,能就地快速更新储存数据、支持字段索引,其优势在于查询功能比较强大...

网友评论

    本文标题:mongodb插入字段,更新数据,查询限制(python)

    本文链接:https://www.haomeiwen.com/subject/hsxqictx.html