【问题标题】:How to join MongoDB collection with string[] on collection with string using Linq如何使用 Linq 在带有字符串的集合上加入带有字符串 [] 的 MongoDB 集合
【发布时间】:2020-06-16 19:49:16
【问题描述】:

我正在尝试加入两个集合。我设法将两者正确连接,其中连接部分的类型相同 - 但这不是目标。出现困难是因为我在其中一个集合中有一个字符串(id)列表,并且我想用另一个集合的对象替换该列表中的每个字符串,该对象是定义为的用户集合:

public string name { get; set; }
public string email { get; set; }
public string _id { get; set; }
public bool IsDeleted { get; set; }

我有两种类型的会议,我想将用户插入替换字符串的会议,因此它是 DTO。

public class MeetingBase : ModelBase
    {
        public string createdBy { get; set; }
        public string facilitator { get; set; }
        public string topic { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public int RoomNumber { get; set; }
        public DateTime meetingDate { get; set; }

    }

public class Meeting : MeetingBase
    {
        public List<string> attendees { get; set; }
    }

public class MeetingDTO : MeetingBase
    {
        public List<User> attendees { get; set; }
    }

如前所述,我想将Meeting 的集合与users 的集合一起加入,从而得到MeetingDTO 的集合

到目前为止,我最好的尝试是这样的:

public async Task<List<MeetingDTO>> getMeetingDtos(string userid)
        {
            var userCollection = _userDataManager.GetDataContext().MongoCollection;
            var meetingCollection = _meetingDataManager.GetDataContext().MongoCollection;

            var query = from m in meetingCollection.AsQueryable()
                where (m.createdBy == userid)
                from attendee in m.attendees
                join u in userCollection.AsQueryable() on attendee equals u._id into meetingusers
                select new MeetingDTO()
                {
                    _id = m._id,
                    IsDeleted = m.IsDeleted,
                    attendees = meetingusers.ToList(),
                    address = m.address,
                    city = m.city,
                    country = m.country,
                    createdBy = m.createdBy,
                    facilitator = m.facilitator,
                    meetingDate = m.meetingDate,
                    RoomNumber = m.RoomNumber,
                    topic = m.topic

                };

            var result = query.ToList();
            return result;
        }

但是,这种尝试会产生以下运行时异常System.NotSupportedException: '$project or $group does not support {document}.'

【问题讨论】:

    标签: c# mongodb linq .net-core


    【解决方案1】:

    您可以使用集合的.Aggregate() 接口来完成,如下所示:

        var meetingDTOs = meetingCollection.Aggregate()
                            .Match(m => m.createdBy == userid)
                            .Lookup<Meeting, User, MeetingDTO>(
                                userCollection,
                                m => m.attendees,
                                u => u.Id,
                                dto => dto.attendees)
                            .ToList();
    

    测试程序:

    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    using MongoDB.Driver;
    using MongoDB.Entities;
    using MongoDB.Entities.Core;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace StackOverFlow
    {
        public class User : Entity
        {
            public string name { get; set; }
        }
    
        public class MeetingBase : Entity
        {
            public string createdBy { get; set; }
            public string topic { get; set; }
        }
    
        public class Meeting : MeetingBase
        {
            [BsonRepresentation(BsonType.ObjectId)]
            public List<string> attendees { get; set; }
        }
    
        public class MeetingDTO : MeetingBase
        {
            public List<User> attendees { get; set; }
        }
    
        public static class Program
        {
            private static void Main()
            {
                new DB("test");
    
                var user1 = new User { name = "user one" };
                var user2 = new User { name = "user two" };
                new[] { user1, user2 }.Save();
    
                var meeting = new Meeting
                {
                    attendees = new List<string> { user1.ID, user2.ID },
                    createdBy = "god",
                    topic = "corona beer"
                };
                meeting.Save();
    
                var meetingDTOs = DB.Fluent<Meeting>()
                                    .Match(m => m.createdBy == "god")
                                    .Lookup<Meeting, User, MeetingDTO>(
                                        DB.Collection<User>(),
                                        m => m.attendees,
                                        u => u.ID,
                                        dto => dto.attendees)
                                    .ToList();
            }
        }
    }

    【讨论】:

    • @AndersMøllerHansen 我很高兴。希望 mongodb c# 团队在文档方面做得更好;-)
    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 2016-02-06
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2011-02-19
    • 2013-06-07
    • 2016-05-10
    相关资源
    最近更新 更多