python 其中指定了集合名称

xvw2m8pv  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(140)
app=Flask(__name__)
CORS(app)
app.config["MONGODB_SETTINGS"] = [
    {
        "db": "UPTeam",
        "host": "10.64.127.94",
        "port": 27017,
        "alias": "default",
    }
]
db=MongoEngine()
db.init_app(app)

class PerfResult(db.Document):
    release = db.StringField()
    cycle = db.StringField()
    device = db.StringField()
    results = db.DictField()

    def to_jason(self):
        return {
            "release": self.release,
            "cycle": self.cycle,
            "device": self.device,
            "results": self.results
        }

@app.route("/api/update", methods = ["POST"])
def db_update():
    try:
        content = request.json        
        print("ARGS")
        print("{}".format("/api/update"))
        print("*****************")
        print("{}".format(content))
        print("*****************")
        the_release = str(content["release"]).upper()
        the_cycle = str(content["cycle"]).upper()
        the_device = str(content["device"]).upper()
        the_profile = str(content["profile"]).upper()
        the_label = str(content["label"]).upper()
        the_packet_size = str(content["packet_size"]).upper()
        the_throughput = int(content["throughput"])
        the_rate = float(content["rate"])
        the_kpps = int(content["kpps"])
        the_qfp = int(content["qfp"])

        result_obj = PerfResult.objects(release=the_release, cycle=the_cycle, device=the_device).first()
        if result_obj:
            new_result = {"throughput": the_throughput, "kpps": the_kpps, "rate": the_rate, "qfp": the_qfp, "label": the_label}
            existing_results = result_obj["results"]
            existing_results[the_profile + "-" + the_packet_size] = new_result
            result_obj.update(results=existing_results)
            response = make_response("Cycle result updated", 200)
            print("RESPONSE")
            print("*****************")
            print("{}".format(str(response)))
            print("*****************")
            return response
        else:
            new_result = {"throughput": the_throughput, "kpps": the_kpps, "rate": the_rate, "qfp": the_qfp, "label": the_label}
            result_obj = PerfResult(
                release=the_release,
                cycle=the_cycle,
                device=the_device,
                results= {the_profile + "-" + the_packet_size: new_result})
            result_obj.save()
            response = make_response("Cycle created and result added", 200)
            print("RESPONSE")
            print("*****************")
            print("{}".format(str(response)))
            print("*****************")
            return response
    except:
        print("EXCEPTION")
        print("*****************")
        print("{}".format(traceback.format_exc()))
        print("*****************")
        return make_response(traceback.format_exc(), 201)

在上面的代码中,我没有在任何地方指定集合名称,但它仍然以某种方式将集合名称作为perf_results
如何指定集合的名称?

jgovgodb

jgovgodb1#

如果要指定自定义集合名称,可以通过在PerfResult类中添加 meta类并设置collection属性来实现。

class PerfResult(db.Document):
    # ...

    meta = {
        'collection': 'custom_collection_name'  # Collectio name
    }

相关问题