[CSHARP-1627] Join operation not working mongo c# driver linq Created: 17/Apr/16  Updated: 19/Apr/16  Resolved: 19/Apr/16

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.2.3
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Khushal Patel Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

.net 4.5,


Issue Links:
Duplicate
duplicates CSHARP-1573 $Lookup uses the localField as the va... Closed

 Description   

Please see http://stackoverflow.com/questions/35698842/join-operation-not-working-mongo-c-sharp-driver-linq#comment60869673_35698842

When I try to perform join operation I am getting joint document collection count as zero. It happens when we link (objectId) _id fields both the side, its works when you link up some other datatype likes strings, it could be bug in the driver which is not accepting objectId.



 Comments   
Comment by Craig Wilson [ 19/Apr/16 ]

Hi Khushal,

My apologies. I was testing against our master branch. We have fixed a bug that will be released with 2.2.4. There isn't an issue with ObjectIds. There is an issue with the foreign field name not being set properly as you noted above. This turns out to be a duplicate of CSHARP-1573.

I'm going to close this ticket as a duplicate. Feel free to re-open if you believe something more to be going on here. We'll be releasing 2.2.4 in the next week or two.

Craig

Comment by Khushal Patel [ 19/Apr/16 ]

Hello Craig, the issue is its not failed the build or raise an exception but it wont bring the data i tried your code also but its not working

Here are some points
1) the query fetches the data for collection1 but not for collection2
2) the query printed on the console is "aggregate([{ "$lookup" :

{ "from" : "temp2", "localField" : "ClassTwoId", "foreignField" : "ClassTwoId", "as" : "j" }

}])" which seems weird because ForeignField should be mapped to "_id" as per my understanding, please correct me if i am wrong .

3) When loop through "query" object and trying to get the count of "j" always gives zero.

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 
namespace MongoDBJoinTester
{
    public class Program
    {
        public static void Main(string[] args)
        {
 
            var client = new MongoClient();
            var db = client.GetDatabase("test");
            var collection1 = db.GetCollection<Class>("temp1");
            var collection2 = db.GetCollection<Class2>("temp2");
 
            var query = from c in collection1.AsQueryable()
                        join m in collection2.AsQueryable() on
                 c.ClassTwoId equals m.Id into j
                        select new { c, j };
 
            Console.WriteLine(query);
            var r = query.ToList();
 
            Console.ReadLine();
 
            foreach(var item in r)
            {
 
                Console.WriteLine("Class1.Id = " + item.c.Id);
                Console.WriteLine("Class1.Text = " + item.c.SomeText);
                Console.WriteLine("Class2.Count = " + item.j.Count());
 
                foreach (var obj in item.j)
                {
                    //if runs then its Working
                    Console.WriteLine("Class2.Id = " + obj.Id);
 
                }
 
            }
 
            Console.ReadLine();
 
 
        }
 
 
        public class Class
        {
            public ObjectId Id { get; set; }
            public string SomeText { get; set; }
            public ObjectId ClassTwoId { get; set; }
            public ObjectId ClassThrId { get; set; }
            public DateTime CreatedDate { get; set; }
            [BsonIgnore]
            public Class2 ClassTwoDeatils { get; set; }
        }
 
        public class Class2
        {
            public ObjectId Id { get; set; }
        }
 
    }
}

Comment by Craig Wilson [ 18/Apr/16 ]

Hi Khushal,

I've tried to replicate this both in an automated test as well as a console application and cannot get it to fail. Below is the program I'm using. If you still believe this to be a bug, could you provide a Program.cs that replicates the issue and attach it?

internal class Program
    {
        private static void Main(string[] args)
        {
            var client = new MongoClient();
            var db = client.GetDatabase("test");
            var collection1 = db.GetCollection<Class>("temp1");
            var collection2 = db.GetCollection<Class2>("temp2");
 
            var query = from c in collection1.AsQueryable()
                        join m in collection2.AsQueryable() on
                 c.ClassTwoId equals m.Id into j
                        select new { c, j };
 
            Console.WriteLine(query);
            var r = query.ToList();
        }
 
        public class Class
        {
            public ObjectId Id { get; set; }
            public string SomeText { get; set; }
            public ObjectId ClassTwoId { get; set; }
            public ObjectId ClassThrId { get; set; }
            public DateTime CreatedDate { get; set; }
            [BsonIgnore]
            public Class2 ClassTwoDeatils { get; set; }
        }
 
        public class Class2
        {
            public ObjectId Id { get; set; }
        }
    }

Craig

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