Deleting a document from a mongodb collection

后端 未结 1 556
失恋的感觉
失恋的感觉 2021-01-25 16:55

I have a collection called customers and it contains documents. I have written a function to delete a document using the field and its value which takes as arguments. But it doe

相关标签:
1条回答
  • 2021-01-25 17:22

    Rewrite your function so that the arguments become part of the query object. You can do this using either computed property or bracket notation.

    Using computed property:

    function removeDocument(fieldName, value) {
        db.customers.remove({[fieldName]: value});
    }
    

    Using bracket notation

    function removeDocument(fieldName, value) {
        var query = {};
        query[fieldName] = value;
        db.customers.remove(query);
    }
    
    0 讨论(0)
提交回复
热议问题