Change collection name in mongodb - mongodb

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.

Related

Why does my update $set code remove my entire document?

Help!
I don't what am doing wrong, when I try to update an existing field using the $set method the entire document gets removed.
Can you kindly point out what I am doing wrong in my code:
recipientsDetails.update({_id: "GCYmFqZbaaYD7DvMZ"}, {$set: {paymentStatus: "Approved"}});
Thanks for your help!
The code is correct. It's likely that your publish function for recipientsDetails contains recipientsDetails.find({paymentStatus: "Not Approved"}). Naturally, once you update the document, the document will no longer satisfy that filtering query and the document vanish from the client.
Your code is correct.Check you mongoDB using Robomongo tool.connect your local project with robomongo and update a document then check whether it is updated or not? If the record updated there is an issue with the publish or subscriptions

How can I populate a list in a MongoDB document?

I want to populate within a loop a MongoDB document's list, without overwriting the existing entries of the list. Something like python's append. Is there a command that does this?
You can use $Push operator.
If you need advance check Modifiers also.

last update in mongoengine

Is there any way to find last update Document in Collection? in other way sort collection by update
somethings like this
people = Person.objects.order_by_update()
or i must add update time for each doc?
I use mongodb, mongoengine, flask
You must add a field such as last_updated_time if you want to be able to sort in this way. Also, since you're sorting on it, you should probably index it.
The only thing that mongodb stores by default is _id, which can be used roughly as a created_time timestamp.

How can I rename a collection in MongoDB?

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!

Undo convertToCapped to a collection

In MongoDB you can convert a collection into a capped collection with the command convertToCapped, but is there a way to revert this change so a capped collection goes back to normal?
It's seems there is only one way to convert from capped collection to normal - just simple copy objects to normal collection and remove original capped collection.
db.createCollection("norm_coll");
var cur = db.cap_col.find()
while (cur.hasNext()) {obj = cur.next(); db.norm_coll.insert(obj);}
same as above without using script.
db.collection.copyTo("collection_temp")
db.collection.drop()
db.collection_temp.renameCollection("collection")
I think there is a way! I'm not sure if this is bullet-proof, but I tried:
db.num_coll.convertToCapped(new_size)
and since then it is working.