mongodb remove all from collection
To remove all documents from a MongoDB collection in C, you can use the mongo_collection_remove
function with an empty query document. Here's an example:
#include <mongo.h>
int main() {
mongo conn;
mongo_init(&conn);
mongo_connect(&conn, "localhost", 27017);
mongo_remove(&conn, "database.collection", NULL, MONGO_SINGLE_REMOVE);
mongo_destroy(&conn);
return 0;
}
This code snippet connects to a MongoDB server running on localhost
at port 27017
. It then removes all documents from the collection
in the database
. The NULL
parameter passed as the query document matches all documents in the collection. The MONGO_SINGLE_REMOVE
flag specifies that only one document should be removed at a time.
Remember to replace "database.collection"
with the actual name of your database and collection. You should also handle any errors that may occur during the connection and removal process.