where is the collection name specified

48 views Asked by At
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)

In the above code, I have not specified the collection name anywhere, still somehow it takes the collection name as perf_results.

How can i specify the name of the collection?

1

There are 1 answers

1
biscuit_delicious On

If you want to specify a custom collection name, you can do so by adding a meta class within the PerfResult class and setting the collection attribute.

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

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