Created
March 28, 2019 21:18
-
-
Save Vannevelj/f36f3e5291f878bad12cda810afe0f60 to your computer and use it in GitHub Desktop.
Mongo filtering
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.1</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Mongo2Go" Version="2.2.9" /> | |
<PackageReference Include="MongoDB.Driver" Version="2.8.0" /> | |
</ItemGroup> | |
</Project> |
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
using Mongo2Go; | |
using MongoDB.Bson.Serialization; | |
using MongoDB.Bson.Serialization.Attributes; | |
using MongoDB.Bson.Serialization.Serializers; | |
using MongoDB.Driver; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace MongoFiltering | |
{ | |
public class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
var runner = MongoDbRunner.Start(singleNodeReplSet: true); | |
var client = new MongoClient(runner.ConnectionString); | |
var database = client.GetDatabase("testdb"); | |
var collection = database.GetCollection<Outer>(Guid.NewGuid().ToString()); | |
var elementId = Guid.NewGuid().ToString(); | |
var entry = new Outer | |
{ | |
Id = Guid.NewGuid().ToString(), | |
Value = 1, | |
Elements = new List<Element> | |
{ | |
new Element { Id = elementId, Value = 1 } | |
} | |
}; | |
Console.WriteLine($"Entering data"); | |
await collection.InsertOneAsync(entry); | |
Console.WriteLine($"Discovering data"); | |
await collection.FindAsync(x => x.Id == entry.Id); | |
Console.WriteLine($"Updating outer data"); | |
await collection.FindOneAndUpdateAsync(x => x.Id == entry.Id, Builders<Outer>.Update.Set(x => x.Value, 5)); | |
Console.WriteLine($"Updating inner data"); | |
await collection.FindOneAndUpdateAsync(Builders<Outer>.Filter.Where(x => x.Elements.Any(e => e.Id == elementId)), Builders<Outer>.Update.Set(x => x.Elements[-1].Value, 5)); | |
Console.Read(); | |
} | |
} | |
public class Outer | |
{ | |
public string Id { get; set; } | |
public int Value { get; set; } | |
[BsonSerializer(typeof(ElementsSerializer))] | |
public List<Element> Elements { get; set; } | |
} | |
public class Element | |
{ | |
public string Id { get; set; } | |
public int Value { get; set; } | |
} | |
public class ElementsSerializer : EnumerableSerializerBase<List<Element>> | |
{ | |
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, List<Element> value) => base.Serialize(context, args, value); | |
public override List<Element> Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) => base.Deserialize(context, args); | |
protected override void AddItem(object accumulator, object item) => ((List<Element>)accumulator).Add((Element)item); | |
protected override object CreateAccumulator() => new List<Element>(); | |
protected override IEnumerable EnumerateItemsInSerializationOrder(List<Element> value) => value; | |
protected override List<Element> FinalizeResult(object accumulator) => (List<Element>)accumulator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment