1

how do i insert data to a specific user in python flask mongodb

{
    "_id" : ObjectId("5e208aa1f86973bbd7db6e8a"),
    "worker_name" : "user1",
    "location" : "location1",
    "detection" : [ ]
},
{
    "_id" : ObjectId("5e208aa1f86973bbd7db6e8b"),
    "worker_name" : "user2",
    "location" : "location2",
    "detection" : [ ]
}

above are my users, I want to insert some data in user1 detection list, below are my code i had tried

def face_detection():
    face_module = mongo.db.face_modules
    user = mongo.db.users
    stream_link = request.form['stream_link']
    location = request.form['location']
    camera = request.form['camera']
    result = {
            "location": location,
            "stream_url": stream_link,
            "worker_name": "user1",
            "date": "1/1/2020",
            "hour": "9",
            "minute": "10",
            "second": "25"
        }
    if user.find({"worker_name": result['worker_name']}).count() > 0:
        update_user = user.detection.insert(result)
        output = "user updated"
        return jsonify({'result': output})
2
  • so what error are you getting? did you mean to use insert_one instead of insert? Commented Jan 16, 2020 at 16:21
  • @GonzaloHernandez i dont know I'm new to flask python , i want that result dictionary should insert to a user1 detection list Commented Jan 16, 2020 at 16:31

1 Answer 1

1

Since the document already exists you need to update it rather than insert, Also as you wanted to insert something into an array it should be through $push, Plus instead of making two DB calls you can use find_one_and_update which will return updated document with this option :: return_document = ReturnDocument.AFTER or will return none incase of no matching document found. Based on that you can return the response. In general you would use insert or insert_one for inserting a new document to colleciton. I'm a bit new to pymongo, Please add the code to check error scenario from DB, Plus test this code & you're feel free to update this answer with any findings..

Try this :

def face_detection():
face_module = mongo.db.face_modules
user = mongo.db.users
stream_link = request.form['stream_link']
location = request.form['location']
camera = request.form['camera']
result = {
        "location": location,
        "stream_url": stream_link,
        "worker_name": "user1",
        "date": "1/1/2020",
        "hour": "9",
        "minute": "10",
        "second": "25"
    }
resp = user.find_one_and_update(
    {"worker_name": result['worker_name']},
    { '$push': {'detection' : result} },
    return_document = ReturnDocument.AFTER)


if resp :
    return jsonify({'result': resp})
else :
    return jsonify({'result': 'No document found'})
Sign up to request clarification or add additional context in comments.

6 Comments

its working but its not generating id's of result , there are 2 collections one of user which have list of detection and other collection name detection i want the same detection with its id should add to the user
@chiragprajapati : MongoDB is NoSQL there is no relation between two collections, if you need that relation either you need to add detection documents as sub-documents to relative document of user or you need to pass ids of relative detection documents when inserting/updating documents in user. You can read two collections with one query, but you cannot write to two collections, there are no relations..
yes, i want detection document as sub-documents to relative document of user , how can i do that?
@chiragprajapati : what do you mean by that ? When inserting data in detection collection, you need to insert respective ids for detection documents in user collection, You've to do that through code/manually - MongoDB can't do it.
how can i do that ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.