sql >> Database teknologi >  >> NoSQL >> MongoDB

Omdøb felt af kompleks type, der er placeret i array

Jeg har implementeret en speciel type til at omdøbe vilkårligt felt i MongoDB. Her er det:

using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;

namespace DatabaseManagementTools
{
    public class MongoDbRefactorer
    {
        protected MongoDatabase MongoDatabase { get; set; }

        public MongoDbRefactorer(MongoDatabase mongoDatabase)
        {
            MongoDatabase = mongoDatabase;
        }

        /// <summary>
        /// Renames field
        /// </summary>
        /// <param name="collectionName"></param>
        /// <param name="oldFieldNamePath">Supports nested types, even in array. Separate nest level with '$': "FooField1$FooFieldNested$FooFieldNestedNested"</param>
        /// <param name="newFieldName">Specify only field name without path to it: "NewFieldName", but not "FooField1$NewFieldName"</param>
        public void RenameField(string collectionName, string oldFieldNamePath, string newFieldName)
        {
            MongoCollection<BsonDocument> mongoCollection = MongoDatabase.GetCollection(collectionName);
            MongoCursor<BsonDocument> collectionCursor = mongoCollection.FindAll();

            PathSegments pathSegments = new PathSegments(oldFieldNamePath);

            // Rename field in each document of collection
            foreach (BsonDocument document in collectionCursor)
            {
                int currentSegmentIndex = 0;
                RenameField(document, pathSegments, currentSegmentIndex, newFieldName);

                // Now document is modified in memory - replace old document with new in mongo:
                mongoCollection.Save(document);
            }
        }

        private void RenameField(BsonValue bsonValue, PathSegments pathSegments, int currentSegmentIndex, string newFieldName)
        {
            string currentSegmentName = pathSegments[currentSegmentIndex];

            if (bsonValue.IsBsonArray)
            {
                var array = bsonValue.AsBsonArray;
                foreach (var arrayElement in array)
                {
                    RenameField(arrayElement.AsBsonDocument, pathSegments, currentSegmentIndex, newFieldName);
                }
                return;
            }

            bool isLastNameSegment = pathSegments.Count() == currentSegmentIndex + 1;
            if (isLastNameSegment)
            {
                RenameDirect(bsonValue, currentSegmentName, newFieldName);
                return;
            }

            var innerDocument = bsonValue.AsBsonDocument[currentSegmentName];
            RenameField(innerDocument, pathSegments, currentSegmentIndex + 1, newFieldName);
        }

        private void RenameDirect(BsonValue document, string from, string to)
        {
            BsonElement bsonValue;
            bool elementFound = document.AsBsonDocument.TryGetElement(from, out bsonValue);
            if (elementFound)
            {
                document.AsBsonDocument.Add(to, bsonValue.Value);
                document.AsBsonDocument.Remove(from);
            }
            else
            {
                // todo: log missing elements
            }
        }
    }
}

Og hjælpertype for at beholde stisegmenter:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace DatabaseManagementTools
{
    public class PathSegments : IEnumerable<string>
    {
        private List<string> Segments { get; set; }

        /// <summary>
        /// Split segment levels with '$'. For example: "School$CustomCodes"
        /// </summary>
        /// <param name="pathToParse"></param>
        public PathSegments(string pathToParse)
        {
            Segments = ParseSegments(pathToParse);
        }

        private static List<string> ParseSegments(string oldFieldNamePath)
        {
            string[] pathSegments = oldFieldNamePath.Trim(new []{'$', ' '})
                .Split(new [] {'$'}, StringSplitOptions.RemoveEmptyEntries);

            return pathSegments.ToList();
        }

        public IEnumerator<string> GetEnumerator()
        {
            return Segments.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public string this[int index]
        {
            get { return Segments[index]; }
        }
    }
}

For at adskille rede-niveauer bruger jeg '$'-tegnet - det eneste tegn, der er forbudt for samlingsnavne i mongo. Brugen kan være sådan her:

MongoDbRefactorer mongoDbRefactorer = new MongoDbRefactorer(Mongo.Database);
mongoDbRefactorer.RenameField("schools", "FoobarTypesCustom$FoobarDefaultName", "FoobarName");

Denne kode findes i samlingen schools FoobarTypesCustom ejendom. Det kan være så kompleks type, så array. Derefter vil finde alle FoobarDefaultName egenskaber (hvis FoobarTypesCustom er array, så vil den iterere gennem den) og omdøbe den til FoobarName . Indlejringsniveauer og antallet af indlejrede arrays er ligegyldigt.




  1. Få alle nøgler i Redis-databasen med python

  2. Hvordan får man Max og Min Value i MongoDB baseret på specifik nøgle?

  3. Python-databaseprogrammering med MongoDB for begyndere

  4. Graphql skabe relationer mellem to forespørgsler. Fejl kan ikke få adgang før initialisering