Confusion About Deleting Documents in Capped Collections

I’m hoping for sort out why I’m not seeing what I’d expect in a capped collection. According to the docs:

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

However, I am able to delete documents from my Capped Collection, from what it seems?

test> db.createCollection("cappyMcCapCap", { "capped": true, "size": 100000, "max": 100 } )
{ ok: 1 }

test> db.cappyMcCapCap.isCapped()
true

So far so good, now I’ll insert two documents:

test> db.cappyMcCapCap.insertOne({ "hat": "bowler" });
{
  acknowledged: true,
  insertedId: ObjectId("63a4c2dffe0ea68c13a8c265")
}

test> db.cappyMcCapCap.insertOne({ "hat": "newsboy" });
{
  acknowledged: true,
  insertedId: ObjectId("63a4c2e9fe0ea68c13a8c266")
}

And then delete the newer of the two documents:

test> db.cappyMcCapCap.deleteOne({ "hat": "newsboy" });
{ acknowledged: true, deletedCount: 1 }

Which is successful?

test> db.cappyMcCapCap.find();
[ { _id: ObjectId("63a4c2dffe0ea68c13a8c265"), hat: 'bowler' } ]

I seem to remember maybe getting an error in the past, but I have not tried manually deleting documents from a Capped Collection in a while.

So is that documentation correct, or am I misunderstanding something?

Thanks!

Hello @Justin_Jenkins

Could you please let me know which version of MongoDB are you using? In the meantime, could you try this syntax to create a capped collection and try to remove an inserted document?

db.createCollection("cappyMcCapCap2", { capped : true, size : 5242880, max : 5000 } )

Looking forward to hearing from you

Please let me know if you have any additional questions or concerns regarding the details above.

Kind Regards,
Josman

Thanks @Josman_Perez_Exposit I tired using that syntax, with the same results.

I’m using 6.0.1 (via Docker).

Out of curiosity, I also spun up a 5.0.14 container to test, and it worked the same.

I could delete documents in capped collections.

The process in 6.0.1:

However!

Then, I spun up a container with 4.2.23 and I did get the kind of error I recall getting in the past.

Is this just not a the same anymore in new versions but it isn’t documented? Or what exactly is going on?

The error, in 4.2.23:

> db.cappyMcCapCap2.deleteOne({ "hat": "newsboy" })

2022-12-23T20:20:39.563+0000 E  QUERY    [js] WriteError({
"index" : 0,
"code" : 20,
"errmsg" : "cannot remove from a capped collection: test.cappyMcCapCap2",
"op" : {
"q" : {
"hat" : "newsboy"
},
"limit" : 1
}
}) :
WriteError({
"index" : 0,
"code" : 20,
"errmsg" : "cannot remove from a capped collection: test.cappyMcCapCap2",
"op" : {
"q" : {
"hat" : "newsboy"
},
"limit" : 1
}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.deleteOne@src/mongo/shell/crud_api.js:375:17
@(shell):1:1

Any thoughts @Josman_Perez_Exposit … is this ability to delete something newish with capped collections or ???

Hi @Justin_Jenkins, sorry for the delay. I have indeed reproduced and scaled it internally. I want to inform you that MongoDB > 5.0 removed the restriction on delete operations in the apply operations command.

However, we don’t seem to have added this to the documentation yet. I’ve filed an internal ticket to update the documentation to reflect this to avoid confusion.

Please let me know if you have any additional questions or concerns regarding the above details.

Best regards,
Josman

5 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.