A
A
Andrey Smirnov2019-09-19 15:37:53
JavaScript
Andrey Smirnov, 2019-09-19 15:37:53

How to delete documents by field?

Hello, such a question arose, I began to familiarize myself with the node js express + mongodb technology stack (I exclude mongoose for now, I say right away to exclude questions about it).
There is a users database, it contains the user collection, it contains documents with the following fields:

{
                "_id" : ObjectId("5d8342f4b172fcb285fc597a"),
                "name" : "Andrey",
                "age" : 32,
                "sex" : {
                        "male" : true,
                        "female" : false
                }
        },
        {
                "_id" : ObjectId("5d834329b172fcb285fc597c"),
                "name" : "Anna",
                "age" : 48,
                "sex" : {
                        "male" : false,
                        "female" : true
                }
        },
        {
                "_id" : ObjectId("5d8369c1b172fcb285fc597e"),
                "name" : "Anna",
                "age" : 48,
                "postIndex" : 67890
        },
        {
                "_id" : ObjectId("5d8369d4b172fcb285fc597f"),
                "name" : "Andrey",
                "age" : 48,
                "postIndex" : 123456
        },
        {
                "_id" : ObjectId("5d836bb2b172fcb285fc5980"),
                "name" : "Anton",
                "age" : 48,
                "postIndex" : "123456"
        },
        {
                "_id" : ObjectId("5d836c52b172fcb285fc5981"),
                "name" : "Annete",
                "age" : 48,
                "postIndex" : -1975
        },
        {
                "_id" : ObjectId("5d836e91b172fcb285fc5982"),
                "name" : "Annete",
                "age" : 48,
                "postIndex" : 0.0016502834182392193
        },
        {
                "_id" : ObjectId("5d836ec6b172fcb285fc5983"),
                "name" : "Annete",
                "age" : 48,
                "postIndex" : ISODate("2019-09-19T12:04:22.380Z")
        }

I would like to remove all documents that have a postIndex field, no matter what value they have.
I understand that in order to delete all documents that have this field with INTEGRATE values, you need to write the following on the server:
db.user.remove($where:"/[0-9]+/.test(this.postIndex)")

I am interested in two questions, since I did not find answers to them (I rummaged through everything, including both Google and the doc):
1. How can I execute the same command on Node JS Express to delete with integer values?
2. How can I delete a document in the collection on the server and on Node JS Express by its presence of any field? (in this example, postIndex)
I would be grateful for any answers, comments, or even just tips on solving this problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question