un-cap a collection

62 views
Skip to first unread message

Mick

unread,
Sep 23, 2015, 3:16:29 PM9/23/15
to mongodb-user
Hi all,

I want to un-cap a collection in Mongo using the C++ driver.  I read some posts that if I want to do that, I'd have to copy the existing collection to a new one, drop the capped collection and rename the new collection to the original name.

I tried runCommand(db.collection, BSON("copyTo" << "temp") and

runCommand(db.collection, BSON("cloneCollection" << "from" << "localhost" << "temp"))

"and they both come up with bad or malformed command request?"

Could somebody please point me in the right direction on how to do this?  Or any other way to do this?

Thank you in advance.

Andrew Morrow

unread,
Sep 23, 2015, 3:24:16 PM9/23/15
to mongod...@googlegroups.com

Hi -

What version of the C++ driver are you using?

Thanks,
Andrew


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/5f547dcd-9cd7-439d-b612-1a02ebf73662%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mick

unread,
Sep 23, 2015, 3:38:02 PM9/23/15
to mongodb-user
2.4.3

Andrew Morrow

unread,
Sep 23, 2015, 3:43:58 PM9/23/15
to mongod...@googlegroups.com

That is an extremely old and outdated version of the C++ driver. How did you obtain it and how did you build it? Also, what version of the MongoDB server are you running?

Thanks,
Andrew


Mick

unread,
Sep 23, 2015, 3:59:51 PM9/23/15
to mongodb-user
MongodB server version is 2.4.4.  We downloaded the source code off your web site and built it with scons. 
I don't have the authority to change the driver, so I have to make do with what I have, is there any way around that?

Andrew Morrow

unread,
Sep 23, 2015, 4:22:52 PM9/23/15
to mongod...@googlegroups.com

Well, if you really can't upgrade the driver (more on that below), did you try using the DBClientInterface::copyDatabase helper rather than using runCommand directly? It looks to me like it is present in the server 2.4 sources and probably does what you want. Looking at the implementation in the v2.4 mongodb tree, I think the issue is that your command is not being run against the correct database: the copyDatabase helper runs it against the 'admin' database.

In any case, I'd very strongly recommend that you upgrade to a newer version of the C++ driver. The 2.4 vintage C++ driver is deprecated, and will be end-of-life soon, and the new drivers are significantly improved.


If at all possible you should at least upgrade to the 26compat release stream which is mostly a drop in replacement for the driver that you are using. Better still would be to upgrade to legacy-1.0.5, which is a significantly improved C++ driver, but is not 100% source compatible with the 2.4 or 2.6 era drivers, though the API changes are mostly small. Longer term, we are writing an entirely new C++ driver, though that will require C++11.

Thanks,
Andrew


Mick

unread,
Sep 23, 2015, 4:36:11 PM9/23/15
to mongodb-user
Thank you, Andrew,
Yes, the copyDatabase works, but the newly created database still has the capped = True.
What I'm trying to accomplish here is un-cap a collection by copying it into a temp (and that's the part that I'm having difficulty with), drop the capped collection, then rename the temp collection.
What is the syntax for the "copyTo" command in the runCommand?
Thanks again.

Andrew Morrow

unread,
Sep 23, 2015, 4:59:42 PM9/23/15
to mongod...@googlegroups.com

Here is the documentation for the copydb and clone commands:


I'm somewhat puzzled about why you need to do this programmatically though. Are you creating lots of capped collections, and then later uncapping them? If this is only a one time operation, perhaps it would be better handled from the shell than the C++ driver?

In any event, it doesn't sound like your question is particularly about the C++ driver itself  - it seems you are actually interested in the details of how to use these commands to accomplish un-capping a collection.



Mick

unread,
Sep 23, 2015, 5:30:42 PM9/23/15
to mongodb-user
Thank you, Andrew.
You are correct, it is a one time operation to uncap the collections, as it was decided it wasn't necessary. Unfortunately the product is installed at many customer sites, and we don't have access to update the database from the shell, so the next software (C++) patch will have to handle that. 
Thanks again, really appreciate your help.

Wan Bachtiar

unread,
Sep 24, 2015, 4:04:17 AM9/24/15
to mongodb-user

Hi Mick,

You can un-cap a capped collection with the following steps:

  1. Copy the capped collection to a temporary collection.
  2. Drop the capped collection.
  3. Rename the temporary collection to the original name of the capped collection.

the copyDatabase works, but the newly created database still has the capped = True.

The capped status is part of the collection options. Therefore, copying the whole database will not remove the capped option of a collection.

Below is a C++ code example using the current MongoDB C++ driver that executes the steps listed above:

#include "mongo/client/dbclient.h"

int main() {

  /* Initialize Driver */
  mongo::Status status = mongo::client::initialize();
  if (!status.isOK()){
    std::cout<< "Failed to initialize driver." << std::endl;
    return  EXIT_FAILURE;
  }

  /* Establish db connection */
  mongo::DBClientConnection c;
  try {
    c.connect("localhost");
  } catch( const mongo::DBException &e ) {
    std::cout << "caught " << e.what() << std::endl;
  }

  /* The database name is 'capped' and the capped collection name is 'test',
     using a temporary collection in the same database with a name 'temp'.
  */
  const std::string CAPPED_COLL = "capped.test";
  const std::string UNCAPPED_COLL = "capped.temp";

  /* Copy collection from capped to uncapped collection */
  std::auto_ptr<mongo::DBClientCursor> cursor = c.query(CAPPED_COLL, mongo::BSONObj());
  while(cursor->more()){
    mongo::BSONObj p = cursor->next();
    c.insert(UNCAPPED_COLL, p);
  }

  /* Drop capped collection, and rename uncapped to capped collection */
  mongo::BSONObj result;
  c.runCommand("admin",
               BSON("renameCollection"<<UNCAPPED_COLL
                    <<"to"<<CAPPED_COLL
                    <<"dropTarget"<<true), 
                result);

  std::cout<< "Completed" <<std::endl;
  return EXIT_SUCCESS;
}

Kind Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages