[SERVER-5997] renameCollection creates an empty collection when run from a script Created: 04/Jun/12  Updated: 15/Aug/12  Resolved: 04/Jun/12

Status: Closed
Project: Core Server
Component/s: JavaScript, Shell
Affects Version/s: 2.0.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: bishwa shrestha Assignee: Unassigned
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File server5997.js     File test.js    
Operating System: ALL
Participants:

 Description   

renameCollection works from the shell:

> db.oldName.find();
{ "_id" : ObjectId("4df78a754a235ac761000000"), "name" : "user1" }
> db.oldName.renameCollection("newName");
{ "ok" : 1 }
> db.newName.find();
{ "_id" : ObjectId("4df78a754a235ac761000000"), "name" : "user1" }

I created a script "upgrade.js":

if(db["oldName"].findOne())
{
    db["oldName"].renameCollection("newName");
}

and running it creates an empty "newName" collection:

> db.oldName.findOne();
{ "_id" : ObjectId("4df78a754a235ac761000000"), "name" : "user1" }
> db.newName.findOne();
null

I can workaround this issue by inserting all the records from the old to the new collection and then dropping the old collection, but its not very clean.



 Comments   
Comment by bishwa shrestha [ 22/Jun/12 ]

Cool! Thanks

Comment by auto [ 22/Jun/12 ]

Author:

{u'date': u'2012-06-22T06:50:05-07:00', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: SERVER-5997 adding note about existing data to rename collection.

also redirecting the wiki page to the manual to reduce confusion
Branch: master
https://github.com/mongodb/docs/commit/c44eecc09812c0524103e60de3b92cd004b1edf7

Comment by bishwa shrestha [ 04/Jun/12 ]

hmm.. this could be tricky indeed. I think the documentation needs to be updated with this though.

Comment by Eliot Horowitz (Inactive) [ 04/Jun/12 ]

People generally very much want to create indexes before inserting any data, so not sure if there is anything that can be done.

Comment by bishwa shrestha [ 04/Jun/12 ]

Ahh... I found the problem.

An index was created on "newName" collection before the renameCollection command was invoked. This created the newName collection and caused the script to fail. Please see attachment(test.js).

I moved the index creation at the end (where it belongs ). However, it would be nice if the index couldn't be created on a non-existing collection at the first place.

Comment by bishwa shrestha [ 04/Jun/12 ]

Failure condition: create index before renaming

Comment by Eliot Horowitz (Inactive) [ 04/Jun/12 ]

See test script attached.
If that fails for you or you can modify such that it fails please let us know.

Generated at Thu Feb 08 03:10:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.