[英]How to join MongoDB collection with string[] on collection with string using Linq
I am trying to join two collections.我正在尝试加入两个集合。 I managed to join the two properly where the joining parts are equivalent in type - but that's not the goal.
我设法正确地将两者连接起来,其中连接部分的类型相同 - 但这不是目标。 The difficulty occurs since i have a list of strings (id's) in one of the collections, and i want to replace each string in that list with an object of the other collection which is a collection of users defined as:
出现困难是因为我在其中一个集合中有一个字符串列表(id),并且我想用另一个集合的对象替换该列表中的每个字符串,该对象是一个用户集合,定义为:
public string name { get; set; }
public string email { get; set; }
public string _id { get; set; }
public bool IsDeleted { get; set; }
I have two types of meetings where i want to insert the users to the one replacing the strings hence it's a DTO.我有两种类型的会议,我想将用户插入到替换字符串的会议中,因此它是 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; }
}
As said, i want to join the collection og Meeting
with the collection of users
, resulting in a collection of MeetingDTO
's如上所述,我想将集合 og
Meeting
与users
集合一起加入,从而产生MeetingDTO
的集合
So far, my best attempt is this:到目前为止,我最好的尝试是这样的:
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;
}
However, this attempt gives the following run-time exception System.NotSupportedException: '$project or $group does not support {document}.'
但是,此尝试会产生以下运行时异常
System.NotSupportedException: '$project or $group does not support {document}.'
you can do it with the .Aggregate()
interface of the collection like so:您可以使用集合的
.Aggregate()
接口来做到这一点,如下.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();
test program:测试程序:
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(); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.