如何为 Couchbase Lite 创建复杂的连接查询
How to create complex Join Query for Couchbase Lite
我正在尝试想出一个合适的连接查询来将以下文档连接在一起(人为但有用),但我什至不知道从哪里开始。
查看 official documentation 是短的,因为它似乎已经过时(IFunction
甚至不存在)。
沙发床 mobile documentation is a little more current, but I'm still struggling at making heads or tails of it. It doesn't translate 1 to 1 with the c# api.
我尝试加入的文档与此类似
/*school.json*/
{
"Name": "Harvard",
"Teachers": [
{
"HireDate": "1-1-2020",
"SchoolId": "qwerty",
"TeacherDocumentId": "Teacher::1234"
},
{
"HireDate": "1-1-2019",
"SchoolId": "zxcvb",
"TeacherDocumentId": "Teacher::1235"
}
]
}
/*teacher.json*/
{
"Id":"Teacher::1234",
"Name": "Peggy Sue",
"TeacherDetailDocumentId":"TeacherDetail::9876"
}
/*teacher.json*/
{
"Id":"Teacher::1235",
"Name": "William Jackson",
"TeacherDetailDocumentId":"TeacherDetail::9875"
}
/*teacherdetail.json*/
{
"Id":"TeacherDetail::9876",
"CourseLoad":1
}
/*teacherdetail.json*/
{
"Id":"TeacherDetail::9875",
"CourseLoad":3
}
目标是投射一个复杂的 C# 对象
public class School
{
public string Name {get;set;}
public IEnumerable<SchoolTeacher> Teachers {get;set;}
}
public class SchoolTeacher
{
public DateTime HireDate { get; set;}
public string SchoolId { get; set;}
public string TeacherDocumentId { get; set; }
public Teacher Teacher { get; set; }
}
public class Teacher
{
public string Id { get; set; }
public string Name { get; set; }
public string TeacherDetailDocumentId { get; set; }
public TeacherDetail TeacherDetail { get; set; }
}
public class TeacherDetail
{
public string Id { get; set; }
public int CourseLoad { get; set; }
}
这类似于以下复合体 json
{
"Name": "Harvard",
"Teachers": [
{
"HireDate": "1-1-2020",
"SchoolId": "qwerty",
"TeacherDocumentId": "Teacher::1234",
"Teacher": {
"Id": "Teacher::1234",
"Name": "Peggy Sue",
"TeacherDetailDocumentId": "TeacherDetail::9876",
"TeacherDetail": {
"Id": "TeacherDetail::9876",
"CourseLoad": 1
}
}
},
{
"HireDate": "1-1-2019",
"SchoolId": "zxcvb",
"TeacherDocumentId": "Teacher::1235",
"Teacher": {
"Id": "Teacher::1235",
"Name": "William Jackson",
"TeacherDetailDocumentId": "TeacherDetail::9875",
"TeacherDetail": {
"Id": "TeacherDetail::9875",
"CourseLoad": 3
}
}
}
]
}
与当前的 Couchbase Lite 查询 API 一样,您将需要多个查询才能获取学校教师数组中每位教师的信息。如果您不需要一次构建整个 JSON,这可能会导致性能和内存问题,具体取决于教师的数量,您可以将查询分解为多个级别,并在需要特定数据时根据需要进行查询正在访问。
比如访问School.Teachers属性时可以先查询一个没有明细的SchoolTeacher列表。当SchoolTeacher.teacher被访问时,可以再次查询得到老师的详细信息
我正在尝试想出一个合适的连接查询来将以下文档连接在一起(人为但有用),但我什至不知道从哪里开始。
查看 official documentation 是短的,因为它似乎已经过时(IFunction
甚至不存在)。
沙发床 mobile documentation is a little more current, but I'm still struggling at making heads or tails of it. It doesn't translate 1 to 1 with the c# api.
我尝试加入的文档与此类似
/*school.json*/
{
"Name": "Harvard",
"Teachers": [
{
"HireDate": "1-1-2020",
"SchoolId": "qwerty",
"TeacherDocumentId": "Teacher::1234"
},
{
"HireDate": "1-1-2019",
"SchoolId": "zxcvb",
"TeacherDocumentId": "Teacher::1235"
}
]
}
/*teacher.json*/
{
"Id":"Teacher::1234",
"Name": "Peggy Sue",
"TeacherDetailDocumentId":"TeacherDetail::9876"
}
/*teacher.json*/
{
"Id":"Teacher::1235",
"Name": "William Jackson",
"TeacherDetailDocumentId":"TeacherDetail::9875"
}
/*teacherdetail.json*/
{
"Id":"TeacherDetail::9876",
"CourseLoad":1
}
/*teacherdetail.json*/
{
"Id":"TeacherDetail::9875",
"CourseLoad":3
}
目标是投射一个复杂的 C# 对象
public class School
{
public string Name {get;set;}
public IEnumerable<SchoolTeacher> Teachers {get;set;}
}
public class SchoolTeacher
{
public DateTime HireDate { get; set;}
public string SchoolId { get; set;}
public string TeacherDocumentId { get; set; }
public Teacher Teacher { get; set; }
}
public class Teacher
{
public string Id { get; set; }
public string Name { get; set; }
public string TeacherDetailDocumentId { get; set; }
public TeacherDetail TeacherDetail { get; set; }
}
public class TeacherDetail
{
public string Id { get; set; }
public int CourseLoad { get; set; }
}
这类似于以下复合体 json
{
"Name": "Harvard",
"Teachers": [
{
"HireDate": "1-1-2020",
"SchoolId": "qwerty",
"TeacherDocumentId": "Teacher::1234",
"Teacher": {
"Id": "Teacher::1234",
"Name": "Peggy Sue",
"TeacherDetailDocumentId": "TeacherDetail::9876",
"TeacherDetail": {
"Id": "TeacherDetail::9876",
"CourseLoad": 1
}
}
},
{
"HireDate": "1-1-2019",
"SchoolId": "zxcvb",
"TeacherDocumentId": "Teacher::1235",
"Teacher": {
"Id": "Teacher::1235",
"Name": "William Jackson",
"TeacherDetailDocumentId": "TeacherDetail::9875",
"TeacherDetail": {
"Id": "TeacherDetail::9875",
"CourseLoad": 3
}
}
}
]
}
与当前的 Couchbase Lite 查询 API 一样,您将需要多个查询才能获取学校教师数组中每位教师的信息。如果您不需要一次构建整个 JSON,这可能会导致性能和内存问题,具体取决于教师的数量,您可以将查询分解为多个级别,并在需要特定数据时根据需要进行查询正在访问。
比如访问School.Teachers属性时可以先查询一个没有明细的SchoolTeacher列表。当SchoolTeacher.teacher被访问时,可以再次查询得到老师的详细信息