Querydsl Path Depth - mongodb

I have a entity Document with a list of DocumentValue
#QueryEntity
#Document
public class Document{
private List<DocumentValue> documentValues;
}
DocumentValue can has also a list of DocumentValue
#QueryEntity
public class DocumentValue {
String value;
String name;
String id;
List<DocumentValue> documentValues;
}
I am now trying to do something like
private QDocumentValue getDocumentValuePathByDepth(int depth){
ListPath path = QDocument.document.documentValues;
if (depth != null) {
for (int i = 0; i < depth; i++) {
path = path.documentValues.any();
}
}
}
Does anybody know if its possible to do an eleMatch in that depth?
Like
ListPath<QDocumentValue> query = getDocumentValuePathByDepth(5);
return query.fieldId.eq(documentFilter.getFieldId()).and(query.value.between(from, to));
One element of documentValues in that depth should fulfill both conditions
BR D.C.

elemMatch is supported in Querydsl Mongodb like this
QDocumentValue documentValue = QDocumentValue.documentValue;
query.anyEmbedded(document.documentValues, documentValue)
.on(documentValue.id.eq(documentFilter.getFieldId(),
documentValue.value.between(from, to));

Related

Get dependent ids when querying principal

I'm trying to get just the ids for dependents if a principal is queried, every time the principal is queried.
My initial thought is to add it somehow in the OnModelCreating definitions, however that appears to be limited to filtering down larger sets of data, unless I'm missing something.
Something like this:
builder.Entity<ListingModel>()
.AlsoDoThis(
x => x.MenuIds.AddRange(
Menus.Where(y => y.ListingId == x.Id).Select(y => y.Id).ToList()
)
);
There is a need to not do this in code for each individual place I have a Select, since that functionality is normalized in some base classes. The base classes have a <TModel> passed in and don't inherently know what properties need to be handled this way.
I do have a workaround where I'm grabbing everything with an AutoInclude(), then filtering it out in the model definition with customer getter/setter to return a list of ids. But rather than being more performant (grabbing related FK ids at the DB level) it's transferring all of that data to the server and then programmatically selecting a list of ids, as far as I understand it.
private List<int> _topicsIds = new();
[NotMapped]
public List<int> TopicsIds
{
get { return Topics.Count > 0 ? Topics.Select(x => x.Id).ToList() : _topicsIds; }
set { _topicsIds = value; }
}
public List<TopicModel> Topics { get; set; } = new();
"Extra SQL that gets called with every select in a context" is (to my limited knowledge) almost what HasQueryFilter does, with a just slightly broader operation. I think this is the approach I'm looking for, just selecting more stuff instead of filtering stuff out.
You can project everything via Select
var result = ctx.ListingModels
.Select(lm => new // or to DTO
{
Id = lm.Id,
OtherProperty = lm.OtherProperty,
Ids = x.MenuIds.Select(m => m.Id).ToList()
})
.ToList();
To make more general solution we can use annotations and define how to project such entities.
During Model defining:
builder.Entity<TopicModel>()
.WithProjection(
x => x.MenuIds,
x => x.Menus.Where(y => y.ListingId == x.Id).Select(y => y.Id).ToList()
);
Then usage in common code:
public virtual List<TModel> GetList(List<int> ids)
{
var list = _context.Set<TModel>().Where(x => ids.Any(id => id == x.Id))
.ApplyCustomProjection(_context)
.ToList();
return list;
}
ApplyCustomProjection(_context) will find previously defined annotation and will apply custom projection.
And extensions implementation:
public static class ProjectionExtensions
{
public const string CustomProjectionAnnotation = "custom:member_projection";
public class ProjectionInfo
{
public ProjectionInfo(MemberInfo member, LambdaExpression expression)
{
Member = member;
Expression = expression;
}
public MemberInfo Member { get; }
public LambdaExpression Expression { get; }
}
public static bool IsUnderDotnetTool { get; }
= Process.GetCurrentProcess().ProcessName == "dotnet";
public static EntityTypeBuilder<TEntity> WithProjection<TEntity, TValue>(
this EntityTypeBuilder<TEntity> entity,
Expression<Func<TEntity, TValue>> propExpression,
Expression<Func<TEntity, TValue>> assignmentExpression)
where TEntity : class
{
// avoid registering non serializable annotations during migrations update
if (IsUnderDotnetTool)
return entity;
var annotation = entity.Metadata.FindAnnotation(CustomProjectionAnnotation);
var projections = annotation?.Value as List<ProjectionInfo> ?? new List<ProjectionInfo>();
if (propExpression.Body is not MemberExpression memberExpression)
throw new InvalidOperationException($"'{propExpression.Body}' is not member expression");
if (memberExpression.Expression is not ParameterExpression)
throw new InvalidOperationException($"'{memberExpression.Expression}' is not parameter expression. Only single nesting is allowed");
// removing duplicate
projections.RemoveAll(p => p.Member == memberExpression.Member);
projections.Add(new ProjectionInfo(memberExpression.Member, assignmentExpression));
return entity.HasAnnotation(CustomProjectionAnnotation, projections);
}
public static IQueryable<TEntity> ApplyCustomProjection<TEntity>(this IQueryable<TEntity> query, DbContext context)
where TEntity : class
{
var et = context.Model.FindEntityType(typeof(TEntity));
var projections = et?.FindAnnotation(CustomProjectionAnnotation)?.Value as List<ProjectionInfo>;
// nothing to do
if (projections == null || et == null)
return query;
var propertiesForProjection = et.GetProperties().Where(p =>
p.PropertyInfo != null && projections.All(pr => pr.Member != p.PropertyInfo))
.ToList();
var entityParam = Expression.Parameter(typeof(TEntity), "e");
var memberBinding = new MemberBinding[propertiesForProjection.Count + projections.Count];
for (int i = 0; i < propertiesForProjection.Count; i++)
{
var propertyInfo = propertiesForProjection[i].PropertyInfo!;
memberBinding[i] = Expression.Bind(propertyInfo, Expression.MakeMemberAccess(entityParam, propertyInfo));
}
for (int i = 0; i < projections.Count; i++)
{
var projection = projections[i];
var expression = projection.Expression.Body;
var assignExpression = ReplacingExpressionVisitor.Replace(projection.Expression.Parameters[0], entityParam, expression);
memberBinding[propertiesForProjection.Count + i] = Expression.Bind(projection.Member, assignExpression);
}
var memberInit = Expression.MemberInit(Expression.New(typeof(TEntity)), memberBinding);
var selectLambda = Expression.Lambda<Func<TEntity, TEntity>>(memberInit, entityParam);
var newQuery = query.Select(selectLambda);
return newQuery;
}
}

MongoDB Aggregation in java: How to get total records count along with pagination result?

I have a search criteria in which I have to provide pagination on searched result along with total number of records in collection. Suppose in a collection of 10 records, I want to get only 5 records along with count of total records. The resulted data, I want to push them into separate object having count and searchResult properties. Total count of records will have to map to count and paginated records to searchResult. I have applied aggregation and it is working well except inclusion of CountOperation and ProjectOperation. When I add countOperation and ProjectOperation in aggregation it gives invalid reference "_id!" exception.
the expected query would be like this.
db.customer.aggregate([
{
$facet:{
searchResult:[{$match:{"name" : { "$regex" : "xyz", "$options" : "i" }}}],
count: [{ $count: 'count' }]
}
}
])
and the output would be like this.
[
{
"searchResult":[{...},{...},{...}, ...],
"count":[{"count":100}]
}
]
Search logic:
public List<SampleSearchResult> findListByRequest(ListRequest queryParams, Class<T> clazz) {
String collectionName = mongoTemplate.getCollectionName(clazz);
MatchOperation matchOperation = getMatchOperation(queryParams);
SortOperation sortOperation = getSortOperation(queryParams);
SkipOperation skipOperation = Aggregation.skip((long) queryParams.getPageNumber() * queryParams.getSize());
LimitOperation limitOperation = Aggregation.limit(queryParams.getSize());
CountOperation countOperation = Aggregation.count().as("count");
ProjectionOperation projectionOperation = getProjectionOperation();
AggregationResults<SampleSearchResult> results = mongoTemplate
.aggregate(Aggregation.newAggregation(matchOperation, sortOperation, skipOperation, limitOperation, countOperation, projectionOperation ), collectionName, SampleSearchResult.class);
return (List<SampleSearchResult>) results.getMappedResults();
}
Projection operation logic
private ProjectionOperation getProjectionOperation() {
return Aggregation.project("count").and("_id").previousOperation();
}
SortOperation logic:
private SortOperation getSortOperation(ListRequest listRequest) {
// setting defaults
if (StringUtils.isEmpty(listRequest.getSortBy())) {
listRequest.setSortBy("_id");
listRequest.setAsc(false);
}
Sort sort = listRequest.isAsc() ? new Sort(Direction.ASC, listRequest.getSortBy())
: new Sort(Direction.DESC, listRequest.getSortBy());
return Aggregation.sort(sort);
}
MatchOperation logic:
private MatchOperation getMatchOperation(ListRequest listRequest) {
Criteria criteria = new Criteria();
// build match operation logic with listRequest parameters
return Aggregation.match(criteria);
}
The resultant object which will hold aggregation result
public class SampleSearchResult {
private List<Object> searchResult;
private int count;
public List<Object> getSearchResult() {
return searchResult;
}
public void setSearchResult(List<Object> searchResult) {
this.searchResult = searchResult;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
I need to write CountOperation and ProjectionOperation properly to map the data to SampleSearchResult but I'm not that efficient to do since I'm new to MongoDB opreations.
Well this might be coming very late, but let me drop my answer here for who ever might be having this same trouble in future. The right way to go is truly using facet as described here.
I could use the facet in Java, but mapping the result of the query to a pojo class was also a problem for me. But here is how I was able to get past that challenge.
First looking at the expected output as seen in the question:
[
{
"searchResult":[{...},{...},{...}, ...],
"count":[{"count":100}]
}
]
A pojo to fit this output must be modelled like this:
#Getter //lombok stuff to create getters and setters
#Setter
public class CustomerSearchResult {
private List<CustomerData> searchResult;
private List<CountDto> count;
}
So here is CountDto.java
#Setter
#Getter
public class CountDto {
private Long count;
}
And here is CustomerData.java
#Getter
#Setter
public class CustomerData {
private Long dateCreated;
private String Id;
private String firstName;
private String lastName;
}
Next, MongoClient must be instantiated with a custom or default code registry like this:
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
public class MongoSource {
private MongoClient mongoClient;
public MongoClient createClient(){
String connectionString = "your.database.connection.string";
ConnectionString connection = new ConnectionString(connectionString);
CodecRegistry defaultCodec = MongoClientSettings.getDefaultCodecRegistry();
CodecRegistry fromProvider = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry pojoCodecRegistry = fromRegistries(defaultCodec, fromProvider);
MongoClientSettings.Builder builder = MongoClientSettings.builder();
builder.applyConnectionString(connection);
MongoClientSettings settings = builder.codecRegistry(pojoCodecRegistry).build();
mongoClient = MongoClients.create(settings);
return mongoClient;
}
public MongoClient getMongoClient(){
return mongoClient;
}
}
Using Mongo Java Driver 4.0.X
Not sure the version of the mongo driver or third-party API that was used in the question but I think the use of criteria has been deprecated or was never part of mongo API natively. But this is how I was able to achieve this using the Mongo Java Driver 4.0.x
To achieve a search with paginated result and the total count, using facet, it would be required to build two separate pipelines
One to do the search with sort, limit and skip
The other to do the actual count of the total result without limit
and skip
These two pipelines would finally be used to construct the facets to be used with the aggregate API.
Consider the pojo below used to define the respective query fields
import lombok.Getter;
import lombok.Setter;
#Getter
#Setter
public class SearchQuery {
String firstName;
String lastName;
int pageNumber;
int pageSize;
}
And here is the final snippet that performs the search and returns the CustomerSearchResult intsance that contains the count and the paginated result defined by the pageSize and pageNumber.
import java.util.*;
import static com.mongodb.client.model.Aggregates.*;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import org.apache.commons.lang3.StringUtils;
public CustomerSearchResult findListByRequest(SearchQuery queryParam){
MongoSource mongoSource = new MongoSource();
MongoDatabase database = mongoSource.createClient().getDatabase("customers_db");
MongoCollection<CustomerSearchResult> collection = database.getCollection("customer", CustomerSearchResult.class);
List<Bson> queryPipeline = new ArrayList<>();
List<Bson> countPipeline = new ArrayList<>();
List<Bson> andMatchFilter = new ArrayList<>();
//you might want to check for null before using the fields in the query param object
andMatchFilter.add(regex("firstName", Pattern.compile(queryParam.getFirstName(), Pattern.CASE_INSENSITIVE)));
andMatchFilter.add(regex("lastName", Pattern.compile(queryParam.getLastName(), Pattern.CASE_INSENSITIVE)));
if(queryParam.getPageNumber() == 0){
queryParam.setPageNumber(1);
}
if(queryParam.getPageSize() == 0){
queryParam.setPageSize(30);
}
queryPipeline.add(match(and(andMatchFilter)));
queryPipeline.add(sort(eq("dateCreated", -1)));
queryPipeline.add(skip(queryParam.getPageSize() * (queryParam.getPageNumber() - 1)));
queryPipeline.add(limit(queryParam.getPageSize()));
queryPipeline.add(project(fields(include("Id","firstName","lastName"))));
countPipeline.add(match(and(andMatchFilter)));
countPipeline.add(count());
Facet resultFacet = new Facet("searchResult", queryPipeline);
Facet totalDocFacet = new Facet("count", countPipeline);
return collection.aggregate(Collections.singletonList(facet(resultFacet, totalDocFacet))).first();
}
Note that the CustomerSearchResult fields are the same as the names given to the facets defined for each pipeline respectively. This will allow the defined coderegistry to map correctly the output document to your pojo class (CustomerSearchResult)
then with that you can do like so to access the count:
SearchQuery searchQuery = new SearchQuery();
searchQuery.setPageNumber(1);
searchQuery.setPageSize(15);
searchQuery.setFirstName("John");
searchQuery.setLastName("Doe");
CustomerSearchResult result = findListByRequest(query);
long count = result.getCount()!= null? result.getCount().get(0).getCount() : 0;
List<CustomerData> data = result.getSearchResult();

How do you create a dynamic QueryDSL operator based on a quoted String value

I have a pojo that contains a property name, logic operator as String and the value of property. What I want to accomplish is create a Predicate or Expression etc dynamically from the pojo data. Below are my code:
public class QueryParam {
private String property = "acctType"; //can be any property of classname
private String operator = "eqic" //can be any logic operator !=, >, <, >=, <= etc
private Object value; //will store the value of
// getters/setters here
}
public interface CustomerRepository extends JpaRepository<Customer, Long>, QueryDslPredicateExecutor<Customer>{
}
#Service("CustomerService")
class MyCustomerServiceImpl {
#Resource
private CustomerRepository custRpstry;
//if classname is Customer, property is "acctType", operator is "eqic", and value is "Corporate"
//I want my findAll below to retrieve all Customers having acctType = "Corporate"
List<Customer> findAll(List<QueryParam> qryParam) {
QCustomer qcust = QCustomer.customer;
BooleanBuilder where = new BooleanBuilder();
for(QueryParam param : qryParam) {
//within this block, i want a BooleanBuilder to resolve to:
where.and(qcust.acctType.equalsIgnoreCase("Corporate"));
something like:
where.and(param.getClassname().param.getProperty().param.getOperator().param.getValue())
}
return custRpstry.findAll(where.getValue()).getContent();
}
}
I can't figure out to formulate my BooleanBuilder especially the portion that will convert
getOperator() into .equalIgnoreCase().
Any help would be greatly appreciated.
Thanks in advance,
Mario
After combining several answers to some related questions here in so, I was able to formulate a solution that works for me.
BooleanBuilder where = new BooleanBuilder();
for(QueryParam param: qryParam) {
//create: Expressions.predicate(Operator<Boolean> opr, StringPath sp, filter value)
//create an Operator<Boolean>
Operator<Boolean> opr = OperationUtils.getOperator(param.getOperator().getValue());
//create a StringPath to a class' property
Path<User> entityPath = Expressions.path(Customer.class, "customer");
Path<String> propPath = Expressions.path(String.class, entityPath, param.getProperty());
//create Predicate expression
Predicate predicate = Expressions.predicate(opr, propPath, Expressions.constant(param.getValue()));
where.and(predicate);
}
list = repository.findAll(where.getValue(), pageReq).getContent();
My OperationUtils.java
public class OperationUtils {
public static com.mysema.query.types.Operator<Boolean> getOperator(String key) {
Map<String, com.mysema.query.types.Operator<Boolean>> operators = ImmutableMap.<String, com.mysema.query.types.Operator<Boolean>>builder()
.put(Operator.EQ.getValue() ,Ops.EQ)
.put(Operator.NE.getValue() ,Ops.NE)
.put(Operator.GT.getValue() ,Ops.GT)
.put(Operator.GTE.getValue() ,Ops.GOE)
.put(Operator.LT.getValue() ,Ops.LT)
.put(Operator.LTE.getValue() ,Ops.LOE)
.build();
return operators.get(key);
}
}

Rename MongoDB Child Collection Property [duplicate]

I'm doing refactoring on production database and need to make some renamings. Version of mongodb is 1.8.0. I use C# driver to do refactoring of database. Have faced with problem when I try to rename field of complex type that is located in array.
For example I have such document:
FoobarCollection:
{
Field1: "",
Field2: [
{ NestedField1: "", NestedField2: "" },
{ NestedField1: "", NestedField2: "" },
...
]
}
I Need to rename NestedField2 into NestedField3, for example.
MongoDB documentation says:
$rename
Version 1.7.2+ only.
{ $rename : { old_field_name : new_field_name } }
Renames the field with name 'old_field_name' to 'new_field_name'. Does not expand arrays to find a match for 'old_field_name'.
As I understand, simply using Update.Rename() wouldn't give result, because as documentation says "rename - doesn't expand arrays to find a match for old field name"
What C# code I should write to rename NestedField2 into NestedField3?
I have implemented special type to do renaming of arbitrary field in MongoDB. Here is it:
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
}
}
}
}
And helper type to keep path segments:
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]; }
}
}
}
To separate nest levels I use '$' sign - the only sign that is forbidden for collection names in mongo.
Usage can be something like this:
MongoDbRefactorer mongoDbRefactorer = new MongoDbRefactorer(Mongo.Database);
mongoDbRefactorer.RenameField("schools", "FoobarTypesCustom$FoobarDefaultName", "FoobarName");
This code will find in collection schools FoobarTypesCustom property. It can be as complex type so array. Then will find all FoobarDefaultName properties (if FoobarTypesCustom is array then it will iterate through it) and rename it to FoobarName. Nesting levels and number of nested arrays no matters.

Rename field of complex type that is located in array

I'm doing refactoring on production database and need to make some renamings. Version of mongodb is 1.8.0. I use C# driver to do refactoring of database. Have faced with problem when I try to rename field of complex type that is located in array.
For example I have such document:
FoobarCollection:
{
Field1: "",
Field2: [
{ NestedField1: "", NestedField2: "" },
{ NestedField1: "", NestedField2: "" },
...
]
}
I Need to rename NestedField2 into NestedField3, for example.
MongoDB documentation says:
$rename
Version 1.7.2+ only.
{ $rename : { old_field_name : new_field_name } }
Renames the field with name 'old_field_name' to 'new_field_name'. Does not expand arrays to find a match for 'old_field_name'.
As I understand, simply using Update.Rename() wouldn't give result, because as documentation says "rename - doesn't expand arrays to find a match for old field name"
What C# code I should write to rename NestedField2 into NestedField3?
I have implemented special type to do renaming of arbitrary field in MongoDB. Here is it:
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
}
}
}
}
And helper type to keep path segments:
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]; }
}
}
}
To separate nest levels I use '$' sign - the only sign that is forbidden for collection names in mongo.
Usage can be something like this:
MongoDbRefactorer mongoDbRefactorer = new MongoDbRefactorer(Mongo.Database);
mongoDbRefactorer.RenameField("schools", "FoobarTypesCustom$FoobarDefaultName", "FoobarName");
This code will find in collection schools FoobarTypesCustom property. It can be as complex type so array. Then will find all FoobarDefaultName properties (if FoobarTypesCustom is array then it will iterate through it) and rename it to FoobarName. Nesting levels and number of nested arrays no matters.