简体   繁体   中英

C# Mongo Driver error: Cannot deserialize a 'Int32' from BsonType 'Document'

Following MongoDB query run fine on Robo3T 1.2 and return correct 'count'.

db.runCommand( {
aggregate: "User",
pipeline: [
{$unwind: '$UserSubscriptions'},
{$group: {
    _id: '$_id',
    codes: {$addToSet: '$UserSubscriptions.Subscription.Publication'}
}},
{$unwind: '$codes'},
{$group: {
    _id: '$codes',
    count: {$sum: 1}
}}
]
} )

I m trying to convert above query to C# MongoDriver query like this

 var unwind = new BsonDocument { { "$unwind", "$UserSubscriptions" } };
        var group1 = new BsonDocument
            {
                { "$group",
                    new BsonDocument
                        {
                            { "_id", new BsonDocument
                                         {
                                             {
                                                 "Id","$_id"
                                             }
                                         }
                            },
                            {
                                "codes", new BsonDocument
                                             {
                                                 {
                                                     "$addToSet","$UserSubscriptions.Subscription.Publication.Code"
                                                 }
                                             }
                            }
                        }
              }
            };
        var unwindCode = new BsonDocument { { "$unwind", "$codes" } };
        var group2 = new BsonDocument
            {
                { "$group",
                    new BsonDocument
                        {
                            { "_id", new BsonDocument
                                         {
                                             {
                                                 "Id","$codes"
                                             }
                                         }
                            },
                            {
                                "codes", new BsonDocument
                                             {
                                                 {
                                                     "$sum",1
                                                 }
                                             }
                            }
                        }
              }
            };
        var pipeline = new[] { unwind, group1 , unwindCode , group2 };
    var result = coll.Aggregate<T>(pipeline);

But getting error Cannot deserialize a 'Int32' from BsonType 'Document'.

Id is a primary key in collection

public int Id { get; set; }

在此处输入图片说明

Please advise how to achieve this.

(Robo 3T 1.2.1) (MongoDB.Driver 2.4.4)

You are trying to deserialize a BsonDocument to 'Int32' by creating a BsonDocument for "_id". You just need to do this:

var group1 = new BsonDocument
{
    { 
        "$group", new BsonDocument
        {
            { 
                "_id", "$_id"
            },
            {
                "codes", new BsonDocument
                {
                     {
                        "$addToSet", "$UserSubscriptions.Subscription.Publication.Code"
                     }
                }
            }
        }
    }
};

var group2 = new BsonDocument
{
    { 
        "$group", new BsonDocument
        {
            { 
                "_id", "$codes"
            },
            {
                "count", new BsonDocument
                {
                     {
                        "$sum", 1
                     }
                }
            }
        }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM