Skip to content

Commit 887aac5

Browse files
rstamdnickless
authored andcommitted
CSHARP-2195: Support LINQ queries against RawBsonDocument.
1 parent 4057647 commit 887aac5

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/MongoDB.Bson/Serialization/Serializers/RawBsonDocumentSerializer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace MongoDB.Bson.Serialization.Serializers
1919
/// <summary>
2020
/// Represents a serializer for RawBsonDocuments.
2121
/// </summary>
22-
public class RawBsonDocumentSerializer : BsonValueSerializerBase<RawBsonDocument>
22+
public class RawBsonDocumentSerializer : BsonValueSerializerBase<RawBsonDocument>, IBsonDocumentSerializer
2323
{
2424
// private static fields
2525
private static readonly RawBsonDocumentSerializer __instance = new RawBsonDocumentSerializer();
@@ -70,5 +70,15 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
7070
var bsonWriter = context.Writer;
7171
bsonWriter.WriteRawBsonDocument(value.Slice);
7272
}
73+
74+
/// <inheritdoc/>
75+
public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
76+
{
77+
serializationInfo = new BsonSerializationInfo(
78+
memberName,
79+
BsonValueSerializer.Instance,
80+
typeof(BsonValue));
81+
return true;
82+
}
7383
}
7484
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq;
17+
using FluentAssertions;
18+
using MongoDB.Bson;
19+
using MongoDB.Bson.Serialization;
20+
using MongoDB.Driver.Linq;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
24+
{
25+
public class CSharp2195Tests : Linq3IntegrationTest
26+
{
27+
[Fact]
28+
public void Filter_should_work()
29+
{
30+
var collection = CreateCollection();
31+
var builder = Builders<RawBsonDocument>.Filter;
32+
var filter = builder.Eq(x => x["life"], 42);
33+
34+
var serializerRegistry = BsonSerializer.SerializerRegistry;
35+
var documentSerializer = serializerRegistry.GetSerializer<RawBsonDocument>();
36+
var renderedFilter = filter.Render(documentSerializer, serializerRegistry);
37+
renderedFilter.Should().Be("{ life : 42 }");
38+
39+
var results = collection.FindSync(filter).ToList();
40+
results.Select(x => x["_id"].AsInt32).Should().Equal(2);
41+
}
42+
43+
[Fact]
44+
public void Where_should_work()
45+
{
46+
var collection = CreateCollection();
47+
48+
var queryable = collection
49+
.AsQueryable()
50+
.Where(x => x["life"] == 42);
51+
52+
var stages = Translate(collection, queryable);
53+
AssertStages(
54+
stages,
55+
"{ $match : { life : 42 } }");
56+
57+
var results = queryable.ToList();
58+
results.Select(x => x["_id"].AsInt32).Should().Equal(2);
59+
}
60+
61+
private IMongoCollection<RawBsonDocument> CreateCollection()
62+
{
63+
var collection = GetCollection<BsonDocument>();
64+
65+
CreateCollection(
66+
collection,
67+
new BsonDocument { { "_id", 1 }, { "life", 41 } },
68+
new BsonDocument { { "_id", 2 }, { "life", 42 } });
69+
70+
return GetCollection<RawBsonDocument>();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)