When deleting a document from a private collection in mongoDB, through an error?

I am studying mongoDB for my new project. I created a limited collection, but I get an error while I try to delete a document from a private collection in mongoDB, which is lower,

db.mycol.remove ({"_ identifier": ObjectId ("57bef716e5ff2cbb540e403b")})

WriteResult ({"nRemoved": 0, "writeError": {"code": 20, "errmsg": "cannot be removed from the closed collection: mytestdb.myc ol"}})

Please help me with this.

Thanks in advance.!

+2
source share
4 answers

Cropped collections do not support remove documents and have several other restrictions to make them executable in a limited usecase.

You cannot delete documents from a private collection. To remove all documents from the collection, use the drop () method to remove the Collection

+1
source

Take a look at the documentation:

"Deleting a document

You cannot delete documents from a private collection. To remove all documents from the collection, use the drop () method to remove the collection and recreate the closed collection. "

This means that you need to delete the entire collection and then recreate it. https://docs.mongodb.com/manual/core/capped-collections/

+1
source

Reason: your collection is closed, you cannot complete this action. Details: Limitations

Also, check that your collection is closed? on command:

 db.mycol.isCapped() 

check the version of MongoDB with the command:

 db.version() 

[Solve] Create a new collection without limits and copy all your documents.

Copy, replace the name of your collection and paste into your terminal.

 // replace <mycol> by <your collections name> db.createCollection( "mycol_temp") var cur = db.mycol.find() while (cur.hasNext()) { mycol = cur.next(); db.mycol_temp.insert(logItem); } db.mycol.drop() db.mycol_temp.renameCollection("mycol") 

Now the update () or remove () document is accepted through the reorganization of the collections.

0
source

You cannot delete documents from a private collection.

If your business logic needs to remove items from a limited collection, you can simply mark them as deleted (logical deletion with the addition of a logical field)

We implemented this for the queue years ago. Currently, we have switched to kafka.

0
source

All Articles