MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

एक मोंगोडब डेटाबेस का नाम कैसे बदलें

मैं एक समाधान के साथ आया जो मेरे लिए काम करता है। दी, मैं छोटे डेटाबेस के साथ काम कर रहा हूँ लेकिन यह कुछ के लिए समस्या का समाधान कर सकता है।

/// <summary>
///  Renames old database to new database by copying all its collected contents
///  ToDO: Add checks and balances to ensure all items were transferred correctly over
///  Disclaimer: use at own risk and adjust to your needs
/// </summary>
/// <param name="currentName"></param>
/// <param name="newName"></param>
public void renameDatabase(string currentName, string newName)
{

    this.mongoClient.DropDatabase(newName); //we drop the new database in case it exists

    var newDb = mongoClient.GetDatabase(newName); //get an instance of the new db

    var CurrentDb = mongoClient.GetDatabase(currentName); //get an instance of the current db

    foreach (BsonDocument Col in CurrentDb.ListCollections().ToList()) //work thru all collections in the current db
    {
        string name = Col["name"].AsString; //getting collection name

        //collection of items to copy from source collection
        dynamic collectionItems = CurrentDb.GetCollection<dynamic>(name).Find(Builders<dynamic>.Filter.Empty).ToList();

        //getting instance of new collection to store the current db collection items
        dynamic destinationCollection = newDb.GetCollection<dynamic>(name);

        //insert the source items into the new destination database collection
        destinationCollection.InsertMany(collectionItems);
    }

    //removing the old datbase
    this.mongoClient.DropDatabase(currentName);
}


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. विंडोज़ पर बाहरी ड्राइव का उपयोग करने के लिए मैं डॉकर में चलाने के लिए मोंगो को कैसे कॉन्फ़िगर करूं?

  2. गंभीर त्रुटि:कक्षा 'मोंगोडीबी \ ड्राइवर \ प्रबंधक' नहीं मिला

  3. मोंगोडब में एक गहरी नेस्टेड वस्तु को कैसे हटाएं

  4. MongoDB C# एकत्रीकरण - आराम -> groupBy

  5. एक सरणी में $addToSet लेकिन यह मुझे शून्य देता है