Reputation: 146
I'm working on couchbase lite wpf project. I want to get appointments by date. So I created a view with below code.
appointments.AddRange(App.StorageRepository.Query<AppointmentModel>(map, limit, offset, TodayWhere));
bool TodayWhere(IDictionary<string, object> accessDoc, IDictionary<string, object> parameters)
{
var tableName = parameters["table"].ToString();
if (!accessDoc["_id"].ToString().Contains(tableName))
{
return false;
}
var startDay = Convert.ToDateTime(accessDoc["startDate"].ToString());
var today = Convert.ToDateTime(parameters["today"].ToString());
if (startDay.Date != today.Date)
{
return false;
}
return true;
}
public List<T> Query<T>(IDictionary<string, object> map, int limit, int offset, Func<IDictionary<string, object>, IDictionary<string, object>, bool> monitoring)
{
List<T> list = new List<T>();
var viewName = map["view"].ToString();
var view = Manager.SharedInstance.GetDatabase(DatabaseName).GetView(viewName);
view.SetMap((doc, emit) =>
{
if (!monitoring(doc, map))
{
return;
}
emit(doc, null);
}, "1.0");
var query = view.CreateQuery();
query.Limit = limit;
query.Descending = true;
var queryResult =
query
.Run()
.ToList();
foreach (var item in queryResult)
{
var doc = GetObject<T>(item.Document.UserProperties);
list.Add(doc);
}
return list;
}
So this code is working, but there is a problem. For example. I created the view in 2016.05.06. The view returns appointments of the day correctly. But It returns same data in today too. I'm not sure what is wrong. I'm confused about View and query. I can't use the view like SQL view table?
Upvotes: 0
Views: 353
Reputation: 2276
Views build a static index that is persistent. The map function for a View must be a pure function, meaning it can't depend on anything but the document passed in. If your map function relies on anything (like the current date), you'll get inconsistent results. It looks like this may be what's happening in your case.
Rules for Couchbase Lite View map functions:
See the documentation on Couchbase Lite Views.
Upvotes: 1