What method does mongodb use to return the id of the data just inserted

  mongodb, question

In mysql, you can uselast_insert_id()Method to obtain the id of the recently inserted data, but in mongodb, no such method was found, how to implement it?

补充代码

def add_post():
    post = db.Post()
    post.title = request.form['title']
    post.text = request.form['text']
    post.save()
    return <刚刚插入数据的 _id>

This should be more clear

See LZ should be PyMongo … then read directlypost._idJust do it

It will update itself after insertion. …

def add_post():
    post = db.Post()
    post.title = request.form['title']
    post.text = request.form['text']
    post.save()
    return post._id

Reference:

[1]Collection – PyMongo Reference