I am building nodejs application and I am using MongoDB official driver(not mongosse)
And I want to write a command to MongoDB.
I have 100 documents in a collection. (All id’s are unique )
I want keep the first 50 documents and delete all other documents in the collection
That means skip the first 50 documents and delete all other documents
What command should I write.
2
Answers
I had 100 documents.
I manage to skip the first 50 documents and return other 50 documents ids with this command in nodejs
The result was
(Not all 50 documents ids are here. But it will return all 50 ids)
Then I deleted the 50 documents with this command
I hope this is clear
On the mongo-shell you can try this if you want to order and delete by specific field:
If you simply want to delete and keep them, according to the insertion order, remove the sort command: