Hi,
I am using JOIN statement on IMongoQueryable to join two collections data but getting error if I use join statement. I am using mongodb driver 2.25.0.
Both the collection having same Id , need to combine some fields
var mainCollection = _mainCollection.AsQueryable();
var otherCollection = _otherCollection.AsQueryable();
var sharedQuery = mainCollection
.Join(mainCollection,
main => main.Id,
other => other.Id,
(main, other) => new Entity
{
Id = main.Id,
Model = main.Model,
Body = main.Body,
Status = main.Status,
Version = main.Version,
WorkFlowInfo = other.Body
});
Body is a bsonDocument and group is a field inside this. When I was using 2.18 version of mongodb driver it was working fine. but After upgrade I started getting this error. Need your help here
Hi, @Divya_Vyas,
Welcome to the MongoDB Community Forums. I tried your query with the latest driver and it produced reasonable MQL. I had to make some assumptions about your POCOs and updated your Join
to be between main and other rather than main and main. Here is the repro that I ran:
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
var client = new MongoClient();
var db = client.GetDatabase("test");
var main = db.GetCollection<Entity>("main");
var other = db.GetCollection<Entity>("other");
var mainCollection = main.AsQueryable();
var otherCollection = other.AsQueryable();
var sharedQuery = mainCollection
.Join(otherCollection,
main => main.Id,
other => other.Id,
(main, other) => new Entity
{
Id = main.Id,
Model = main.Model,
Body = main.Body,
Status = main.Status,
Version = main.Version,
WorkFlowInfo = other.Body
});
Console.WriteLine(sharedQuery);
public class Entity
{
public ObjectId Id { get; set; }
public string Model { get; set; }
public string Body { get; set; }
public string Status { get; set; }
public string Version { get; set; }
public string WorkFlowInfo { get; set; }
}
And the MQL output that it produced:
test.main.Aggregate([{ "$project" : { "_outer" : "$$ROOT", "_id" : 0 } }, { "$lookup" : { "from" : "other", "localField" : "_outer._id", "foreignField" : "_id", "as" : "_inner" } }, { "$unwind" : "$_inner" }, { "$project" : { "_id" : "$_outer._id", "Model" : "$_outer.Model", "Body" : "$_outer.Body", "Status" : "$_outer.Status", "Version" : "$_outer.Version", "WorkFlowInfo" : "$_inner.Body" } }])
Please file a CSHARP ticket with a self-contained repro and stack trace and we will be happy to investigate further with you.
Sincerely,
James
@James_Kovacs Thank you for trying the query. There is a slight change in Entity, Body and WorkFlowInfo is a bsondocument.
My aggregate query is
db.collection.Aggregate([{ “$match” : { “IsDeleted” : { “$ne” : true } } },
{“$match” : { “$expr” : { “$eq” : [{ “$toString” : “$Body.group” }, “group name”] }}}])
and after this I am using join statement
.Join(otherCollection,
main => main.Id,
other => other.Id,
(main, other) => new Entity
{
Id = main.Id,
Model = main.Model,
Body = main.Body,
Status = main.Status,
Version = main.Version,
WorkFlowInfo = other.Body
});