How to remove a field completely from a MongoDB document ?



  • 3 Answer(s)

    just try to do this below code:

    db.example.update({}, {$unset: {words:1}}, false, true);
    

    further information http://www.mongodb.org/display/DOCS/Updating#Updating-%24unset

    UPDATING:

    For the link https://docs.mongodb.com/manual/reference/operator/update/unset/no longer covers ‘$unset’ing. Be sure to add {multi: true} if you want to delete this field from entire of the documents in the collection; or else, it will only delete it from the first document it search that matches. See this for updated documentation:

    Specimen:

    db.example.update({}, {$unset: {words:1}} , {multi: true});
    
    Answered on December 27, 2018.
    Add Comment

    To delete field in MongoDB:

    The lonely record:

    db.getCollection('userData').update({}, {$unset: {pi: 1}})
    
    

    The many record:

    db.getCollection('userData').update({}, {$unset: {pi: 1}}, {multi: true})
    
    
    Answered on December 27, 2018.
    Add Comment

    you can also just try to do this one:

    db.example.updateMany({},{"$unset":{"tags.words":1}})
    
    
    Answered on December 27, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.