好的,我正在使用Couchbase 2.0和最新的.NET客户端。 基本上我写的是一个跟踪目标的项目(一个美化的待办事项清单)......
我已经设法将目标对象存储为couchbase中的JSON文档,然后将其反序列化为POCO,但我的问题是如何自动查找链接的文档并填充子目标列表<目标>
不确定这种自动反序列化是否可行,如果没有一些逻辑可以在代码本身内处理它,但任何指针都很受欢迎,欢呼!
JSON
{
id: "goal_1",
name: "goal 1",
description: "think of some better projects",
subGoals: [goal_2, goal_3]
}
C#
var goal = client.GetJson<Goal>(id);
return goal;
这是POCO
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
namespace stuff.Models
{
public class Goal
{
protected DateTime _targetDate;
/// <summary>
/// Name of the goal
/// </summary>
[JsonProperty("name")]
public String Name { get; set; }
/// <summary>
/// Full description of the goal
/// </summary>
[JsonProperty("description")]
public String Description { get; set; }
/// <summary>
/// Target date for completing this goal
/// </summary>
[JsonProperty("targetDate")]
public DateTime? TargetDate
{
get
{
return _targetDate;
}
set
{
// target date must be later than any sub-goal target dates
}
}
/// <summary>
/// Any sub-goals
/// </summary>
[JsonProperty("subGoals")]
public List<Goal> SubGoals { get; set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="Name"></param>
/// <param name="Description"></param>
/// <param name="TargetDate"></param>
/// <param name="SubGoals"></param>
public Goal(String Name, String Description, DateTime? TargetDate = null, List<Goal> SubGoals = null)
{
this.Name = Name;
this.Description = Description;
this.TargetDate = TargetDate;
this.SubGoals = SubGoals;
}
}
}
答案 0 :(得分:2)
在一个“join”查询中没有自动获取相关文档的方法,但您可以使用整理视图。所以给出了一组如下目标(注意添加了一个类型属性):
{"name":"goal 1","description":"This is a parent goal","subgoals":["goal2","goal3"],"type":"goal"}
{"name":"goal 2","description":"This is a child goal","type":"goal"}
{"name":"goal 3","description":"This is another child goal","type":"goal"}
{"name":"goal 4","description":"This is another parent goal","subgoals":["goal5","goal6","goal7"],"type":"goal"}
{"name":"goal 5","description":"This is a child goal","type":"goal"}
{"name":"goal 6","description":"This is a child goal","type":"goal"}
{"name":"goal 7","description":"This is a child goal","type":"goal"}
然后,您将编写一个视图,输出每个父目标,后跟其子项:
function (doc, meta)
{
if (doc.type == "goal" && doc.subgoals)
{
emit([meta.id, 0], null);
for(var idx in doc.subgoals)
{
emit([doc.subgoals[idx], 1], null);
}
}
}
此视图的输出是一组行,其中每行包含一个由父ID和0组成的键,后跟其子项和1(0和1确保行正确排序):
{"id":"goal1","key":["goal1",0],"value":null},
{"id":"goal1","key":["goal2",1],"value":null},
{"id":"goal1","key":["goal3",1],"value":null},
{"id":"goal4","key":["goal4",0],"value":null},
{"id":"goal4","key":["goal5",1],"value":null},
{"id":"goal4","key":["goal6",1],"value":null}
在.NET SDK教程中,我将介绍如何查询整理视图(Brewery及其啤酒)。
http://www.couchbase.com/docs/couchbase-sdk-net-1.2/collatedviews.html