0

I have this JSON above with _id, date, and two objects: sessionData and metaData. I want to delete from every document the object metaData where "userId":123456

{
    "_id": {
        "$oid": "60feedd4b3aefff2629b93b7"
    },
    "insertionDate": {
        "$date": "2021-07-26T18:15:57.564Z"
    },
    "sessionData": {
        "time": [1364, 1374, 1384],
        "yaw": [0.15, 0.3, 0.45],
        "pitch": [0.36, 0.76, 1.08],
        "roll": [-0.13, -0.25, -0.35],
        "heading": [-3.24, -3.25, -3.17],
        "ax": [-0.42, -0.41, -0.41],
        "ay": [-0.15, -0.13, -0.1],
        "az": [0.9, 0.91, 1],
        "gx": [0, 0, 0],
        "gy": [-0.01, 0, -0.01],
        "gz": [0.02, 0.02, 0.02],
        "mx": [0.26, 0.26, 0.26],
        "my": [0.01, 0.01, 0.01],
        "mz": [-0.04, -0.04, -0.07]
    },
    "metaData": {
        "userId": 123456,
        "gender": "M",
        "ageGroup": "SENIOR",
        "weightKg": 70,
        "heightCm": 175,
        "poolSizeM": 50
    }
}

The code I have:

@app.route('/data/anonymousData/<int:l>', methods = ['POST'])
def makeanonymous(l):    
    result = collection.update_many({}, {"$pull":{ "metaData": {"$in": {"userId": l }}}}, multi=True )    
    t=result.deleted_count
    return f'Deleted {t} documents.' 

1 Answer 1

1

You can use $unset to remove the object like this:

collection.update_many({
  "metaData.userId": 123456
},
{
  "$unset": {
    "metaData": ""
  }
})

Check this example.

Sign up to request clarification or add additional context in comments.

Comments

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.