Is there a dead easy way to rename a collection in mongo? Something like:
db.originalCollectionName.rename('newCollectionName');
And if not, what is the best way to go about effectively renaming one?
Close. Use db.originalCollectionName.renameCollection('newCollectionName')
See http://www.mongodb.org/display/DOCS/renameCollection+Command
Assume that the database name is "mytestdb" and collection name is "orders". collection name change to orders2015 The simplest way is,
> use mytestdb
> db.orders.renameCollection( "orders2015" )
Note : db.collection.renameCollection() is not supported on sharded collections.
For those who cannot rename, because the name causes an issue like: SyntaxError: Unexpected token ILLEGAL, it is because the name is illegal.
You can work around this by calling with brackets notation: db["oldCollectionILLEGALName"].renameCollection("someBetterName")
In case you are using Node.js MongoDB driver:
mongoClient.db(dbName).collection('oldName').rename("newName");
https://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#rename
my case was using mongoose:
await mongoose.connection.collection("oldName").rename("newName");
Rename a collection in cmd:
cd C:\Program Files\MongoDB\Server\4.2\bin
mongo
use yourdb
db.yourcollection.renameCollection("someBetterName")
This example is made for MongoDB 4.2
You can use the following syntax to rename an existing collection in MongoDB.
db.originalCollectionName.renameCollection('newCollectionName')
For instance, if your existing collection name is 'demo' and want to rename to 'demo_updated' then, the query would be as follows:-
db.demo.renameCollection('demo_updated')
Thanks!
Related
As I was importing a new json file to my mongodb collection I've accidentally use just one '-' instead of 2. Eg.:
mongoimport --host=127.0.0.1 --db=dataBaseName -collection=people --file=importFile.json
I believe that due to the lack of the second '-', now I'm stuck with the following results when I type show collections:
people
ollection=people
I can't access, drop or interact with the second one. Apart from droping the database and starting over, is there a way around this issue?
You can rename the collection like:
> use YourDatabase
// Might wanna drop people collection first
> db.getCollection("ollection=people").renameCollection("people")
Hope This helps!
when I attempt to insert new document manually in system.indexes collection in mongodb,new collection created.here goes the code
{
"v" : 1,
"key" : {
"code" : 1
},
"name" : "code_1",
"ns" : "mydb.collection"
}
where collection is my collection name which is not already present in database and mydb is my database name. Why new collection is getting created?
Is it possible to create collection by adding index manually in system.indexes.
Why are you asking us this? You already tried to add a new index to system.indexes. Has a new collection been created? If yes, then yes it is possible, if no, then not possible.
Is this a correct way?
How do you think? Have you read somewhere in documentation that in order to create a new collection you need to dance around and to create manually indexes in some system defined collection? Or may be it was written in documentation that db.createCollection(name, options) is what you should do or if you so desire you can just insert a document in a non existed collection and it will create it.
So why after all this one might think that the correct way is to do some manipulation with system.indexes?
As a complement to #Salvador Dali's answer strongly discouraging you to do modify system.index directly: if for some reason you really don't want/can't use createCollection, just remember this is a wrapper around the create command.
You can issue yourself such command to create a new collection:
db.runCommand( { create: "collection" } )
As about inserting an entry in system.indexes: from the doc:
Deprecated since version 3.0: Access this data using listIndexes.
The <database>.system.indexes collection lists all the indexes in the database.
By reading that it appears that system.indexes should be considered as read-only (its direct use is even deprecated since 3.0). The behavior you observed should be considered as unspecified. And so unreliable and subject to change without further notices.
If you really need to understand why it behave that way, maybe you should take a look at the source code or ask the question on the MongoDB developer mailing list. There you could have all the insights.
mongodb db.getCollectionNames()command will give you all collection names what are all there in the current db, in a list.
I want the same output using pymongo. I googled some time and couldn't find anything like that.
Is anything like that exists ?
collection_names()
Will show you the collections of the current database.
From documentation:
collection_names()
Get a list of all the collection names in this database.
[read more]
Since pymongo 3.7, collection_names() is deprecated and the correct way to get collection names is list_collection_names()
Changing the name of a collection in mongodb can be achieved by copy the documents in the collection to a new one and delete the original.
But is there a simpler way to change the name of a collection in mongodb?
db.oldname.renameCollection("newname")
Really? No google search?
http://www.mongodb.org/display/DOCS/renameCollection+Command
> db.oldname.renameCollection("newname")
db.oldCollectionName.renameCollection("NewCollectionName")
Check Manual here.
What I want:
I have a master collection of products, I then want to filter them and put them in a separate collection.
db.masterproducts.find({category:"scuba gear"}).copyTo(db.newcollection)
Of course, I realise the 'copyTo' does not exist.
I thought I could do it with MapReduce as results are created in a new collection using the new 'out' parameter in v1.8; however this new collection is not a subset of my original collection. Or can it be if I use MapReduce correctly?
To get around it I am currently doing this:
Step 1:
/usr/local/mongodb/bin/mongodump --db database --collection masterproducts -q '{category:"scuba gear"}'
Step 2:
/usr/local/mongodb/bin/mongorestore -d database -c newcollection --drop packages.bson
My 2 step method just seems rather inefficient!
Any help greatly appreciated.
Thanks
Bob
You can iterate through your query result and save each item like this:
db.oldCollection.find(query).forEach(function(x){db.newCollection.save(x);})
You can create small server side javascript (like this one, just add filtering you want) and execute it using eval
You can use dump/restore in the way you described above
Copy collection command shoud be in mongodb soon (will be done in votes order)! See jira feature.
You should be able to create a subset with mapreduce (using 'out'). The problem is mapreduce has a special output format so your documents are going to be transformed (there is a JIRA ticket to add support for another format, but I can not find it at the moment). It is also going to be very inefficent :/
Copying a cursor to a collection makes a lot of sense, I suggest creating a ticket for this.
there is also toArray() method which can be used:
//create new collection
db.creatCollection("resultCollection")
// now query for type="foo" and insert the results into new collection
db.resultCollection.insert( (db.orginialCollection.find({type:'foo'}).toArray())