Created
January 23, 2024 15:09
-
-
Save leandromoh/1e743749eea27858797169733e87a57f to your computer and use it in GitHub Desktop.
This file contains 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
public class PipelineStageDefinitionBuilderSet<TDocument> | |
{ | |
private UpdateDefinition<TDocument> current; | |
public PipelineStageDefinitionBuilderSet() | |
{ | |
current = new BsonDocument(); | |
} | |
public IPipelineStageDefinition ToPipelineStageDefinition() | |
{ | |
var y = BsonDocument.Parse( | |
""" | |
{ $set : "FOO" } | |
"""); | |
y["$set"] = current.RenderToBsonDocument(); | |
var pipe = new BsonDocumentPipelineStageDefinition<TDocument, TDocument>(y); | |
return pipe; | |
} | |
public PipelineStageDefinitionBuilderSet<TDocument> Set<TField>(Expression<Func<TDocument, TField>> field, TField value) | |
{ | |
var xs = Builders<TDocument>.Update.Set(field, value); | |
var y = xs.RenderToBsonDocument(); | |
var temp = y["$set"].AsBsonDocument; | |
current = Builders<TDocument>.Update.Combine(current, temp).RenderToBsonDocument(); | |
return this; | |
} | |
public PipelineStageDefinitionBuilderSet<TDocument> Push<TField>(Expression<Func<TDocument, IEnumerable<TField>>> field, TField value) | |
{ | |
var xs = Builders<TDocument>.Update.Push(field, value); | |
var y = xs.RenderToBsonDocument(); | |
var temp = y["$push"].AsBsonDocument.ElementAt(0); | |
var d = BsonDocument.Parse( | |
$$""" | |
{ | |
"{{temp.Name}}": { $concatArrays: [ "${{temp.Name}}", [ "FOO" ] ] } | |
} | |
"""); | |
d[temp.Name]["$concatArrays"][1][0] = temp.Value; | |
current = Builders<TDocument>.Update.Combine(current, d).RenderToBsonDocument(); | |
return this; | |
} | |
public PipelineStageDefinitionBuilderSet<TDocument> Inc<TField>(Expression<Func<TDocument, TField>> field, TField value) | |
{ | |
var xs = Builders<TDocument>.Update.Inc(field, value); | |
var y = xs.RenderToBsonDocument(); | |
var temp = y["$inc"].AsBsonDocument.ElementAt(0); | |
var d = BsonDocument.Parse( | |
$$""" | |
{ | |
"{{temp.Name}}": { $sum: [ "${{temp.Name}}", "FOO" ] } | |
} | |
"""); | |
d[temp.Name]["$sum"][1] = temp.Value; | |
current = Builders<TDocument>.Update.Combine(current, d).RenderToBsonDocument(); | |
return this; | |
} | |
} |
Author
leandromoh
commented
Jan 23, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment