[CSHARP-3075] Collection Aggregation Merge Default Value Throws an Exception Created: 26/Apr/20  Updated: 28/Oct/23  Resolved: 14/Oct/20

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.10.2
Fix Version/s: 2.12.0

Type: Bug Priority: Major - P3
Reporter: Christopher Lombardi Assignee: Mikalai Mazurenka (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, Microsoft .Net Framework 4.7.2



 Description   

This line tries to convert what could be a BsonString type parameter into a BsonDocument and fails. The default (by documentation) acceptable input for a $merge aggregation pipeline operation is the collection name only (a string), but could be a complex object with properties like: into.

Workaround: I could put the, "into" property and make a BsonDocument as a $merge operation's value, but I only knew to do this by reading the source code after receiving an error that the BsonString could not be converted to a BsonDocument.

https://github.com/mongodb/mongo-csharp-driver/blob/9d0f35043498af39041e77df2655ef1aad3b030e/src/MongoDB.Driver/MongoCollectionImpl.cs#L809



 Comments   
Comment by Githook User [ 14/Oct/20 ]

Author:

{'name': 'Mikalai Mazurenka', 'email': 'mikalai.mazurenka@mongodb.com', 'username': 'MikalaiMazurenka'}

Message: CSHARP-3075: Collection Aggregation Merge Default Value Throws an Exception
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/9ee2eef650dd7f0d3f6a5c020c8189b1786e79ef

Comment by Robert Stam [ 21/Sep/20 ]

Thank you for reporting this. I am able to reproduce this.

Reproduction:

using MongoDB.Bson;
using MongoDB.Driver;
using System.Linq;
 
namespace TestCSharp3075
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            var client = new MongoClient("mongodb://localhost");
            var database = client.GetDatabase("test");
            var collection = database.GetCollection<BsonDocument>("test");
 
            var pipeline = new EmptyPipelineDefinition<BsonDocument>()
                .AppendStage<BsonDocument, BsonDocument, BsonDocument>(new BsonDocument("$merge""output"));
 
            var result = collection.Aggregate(pipeline).ToList();
        }
    }
}

 

Comment by Christopher Lombardi [ 26/Apr/20 ]

Problem:
new BsonDocument("$merge", "collectionName")
Resolution:
new BsonDocument("$merge", new BsonDocument()
.Add("into", "collectionName")
.Add("on", "_id")
.Add("whenMatched", "replace")
.Add("whenNotMatched", "insert"))

Generated at Wed Feb 07 21:44:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.