Alex Sid
Alex Sid

Reputation: 31

Bulk remove from MongoDB collection via C++ driver

I made a simple replicator using MongoDB C++ driver (legacy). Now I'm trying to speed it up. Insert operation is much faster with vector insert (bulk inserts show about same time as single element inserts). But I can't figure what is best options for remove bunch of documents. I have documents I want to remove as a vector of mongo::BSONObj (with ObjectId's).

Upvotes: 2

Views: 318

Answers (1)

Alex Sid
Alex Sid

Reputation: 31

I made a query of ObjectId for a single remove per bunch of documents. Like this

            {"_id" : {$in : [ObjectId("..."), ...]}}

It shows better perfomance. Here is my sample code in C++

            std::vector<mongo::BSONObj> bulk_data;
            ...
            std::stringstream ss;
            ss << "{\"_id\" : {$in : [";
            for(size_t j = 0; j < bulk_data.size(); ++j)
            {
                if(j != 0)
                    ss << ",";
                ss << "ObjectId(\"" << bulk_data[j]["_id"].OID() << "\")";
            }
            ss << "]}}";
            conn_src->remove( "test.col1" , ss.str());

Upvotes: 1

Related Questions