Paul Davis
Paul Davis

Reputation: 525

MongoDB - "added on" timestamp but keep that timestamp when edited

I have a database with a timestamp ("added_on") when the post was added but I am allowing an edit of the post too, so I create a new edition of an edited timestamp ("edited_on"), but the original added on time stamp disappears when the user saves.

The edit function is:

@app.route("/edit_question/<question_id>", methods=["GET", "POST"])
def edit_question(question_id):
    if request.method == "POST":
        is_friends = "on" if request.form.get("is_friends") else "off"
        submit = {
            "question_title": request.form.get("question_title"),
            "question_text": request.form.get("question_text"),
            "is_friends": is_friends,
            "created_by": session["user"],
            "added_on": request.form.get("added_on"),
            "edited_on": datetime.now().strftime("%d %b %Y %H:%M")
        }

        mongo.db.questions.update({"_id": ObjectId(question_id)}, submit)
        flash("Question Successfully Edited")

    question = mongo.db.questions.find_one({"_id": ObjectId(question_id)})
    return render_template("edit_question.html", question=question)

But when I save the edited post, I lose the "added_on" timestamp. It becomes null.

So how do I keep both the original timestamp and the new edited_on time stamp on the same DB collection?

UPDATE

Using the code update_one instead, I now have this:

@app.route("/edit_question/<question_id>", methods=["GET", "POST"])
def edit_question(question_id):
    if request.method == "POST":
        is_friends = "on" if request.form.get("is_friends") else "off"
        submit = {
            "$set": {
                "question_title": request.form.get("question_title"),
                "question_text": request.form.get("question_text"),
                "is_friends": is_friends,
                "edited_on": datetime.now().strftime("%d %b %Y %H:%M")
                }
        }    

        mongo.db.questions.update_one({"_id": ObjectId(question_id)}, submit)
        flash("Question Successfully Edited")

    question = mongo.db.questions.find_one({"_id": ObjectId(question_id)})
    return render_template("edit_question.html", question=question)

But it throws a syntax error.

Upvotes: 0

Views: 83

Answers (1)

zhulien
zhulien

Reputation: 5695

[collection].update is a deprecated method. Migrate your code to using update_one:

updateQuery = {
    "$set": {
        "question_title": request.form.get("question_title"),
        "question_text": request.form.get("question_text"),
        "is_friends": is_friends,
        "edited_on": datetime.now().strftime("%d %b %Y %H:%M")
    }    
}

mongo.db.questions.update_one({"_id": ObjectId(question_id)}, updateQuery)

As you see, you don't update added_on and created_by on edit, as they were already created on insertion.

You can also do some checks to see which properties have changed and supply only the relevant updates in the updateQuery

Upvotes: 2

Related Questions