Skip to content

Instantly share code, notes, and snippets.

@yeahmx91
Created August 10, 2016 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeahmx91/81bcd6e361eb4234e54e7e923096716f to your computer and use it in GitHub Desktop.
Save yeahmx91/81bcd6e361eb4234e54e7e923096716f to your computer and use it in GitHub Desktop.
LocomotiveCMS copy slug to new locale
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