Created
August 10, 2016 16:34
-
-
Save yeahmx91/81bcd6e361eb4234e54e7e923096716f to your computer and use it in GitHub Desktop.
LocomotiveCMS copy slug to new locale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MongoDB.Bson; | |
using MongoDB.Driver; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace LocomotiveParser | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connectionString = "mongodb://192.168.247.146"; | |
var mongoClient = new MongoClient(connectionString); | |
var database = mongoClient.GetDatabase("locomotive_dev"); | |
var collection = database.GetCollection<BsonDocument>("locomotive_content_entries"); | |
var filter = Builders<BsonDocument>.Filter.Exists("_slug.es", false); | |
var cursor = collection.Find(filter); | |
var totalElements = cursor.ToEnumerable().Count(); | |
Console.WriteLine("Completing spanish slug for {0} elements", totalElements); | |
var updateCommands = new List<UpdateOneModel<BsonDocument>>(); | |
if (totalElements > 0) | |
{ | |
foreach (var entry in cursor.ToEnumerable()) | |
{ | |
BsonValue entry_id = null; | |
BsonValue entry_slug = null; | |
if (entry.TryGetValue("_id", out entry_id) && entry.TryGetValue("_slug", out entry_slug)) | |
{ | |
BsonElement slug_value; | |
if (entry_slug.AsBsonDocument.TryGetElement("en", out slug_value)) | |
{ | |
string slug_en = slug_value.Value.ToString(); | |
string id = entry_id.ToString().Replace("{", "").Replace("}", ""); | |
var new_slugs = new BsonDocument { { "en", slug_en }, { "es", slug_en } }; | |
var builder = Builders<BsonDocument>.Update; | |
var update = builder.Set("_slug", new_slugs); | |
var entryUpdate = new UpdateOneModel<BsonDocument>(new BsonDocument("_id", entry_id), update); | |
updateCommands.Add(entryUpdate); | |
} | |
} | |
} | |
} | |
try | |
{ | |
collection.BulkWrite(updateCommands.AsEnumerable()); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Error"); | |
} | |
cursor = collection.Find(filter); | |
Console.WriteLine("{0} elements without change", cursor.ToEnumerable().Count()); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment