How to make query

I have appointment data of each days in couchbase lite.
I want to get some data by date.
So I made a view with below code.
view.SetMap((doc, emit) =>
if (doc[“createdDate”] != today)
emit(doc, null);
}, “1.1”);
But the view doesn’t work my own way.
For example I made the view in yesterday and today, I want to get data from the view.
But the view returns yesterday data to me.
How do I make query for this logic?

I’m going to quote directly from the documentation here because I think this should cover your question, but feel free to ask if it doesn’t.

Remember: a view is not a query, it’s an index. Views are persistent, and need to be updated (incrementally) whenever documents change, so having large numbers of them can be expensive. Instead, it’s better to have a smaller number of views that can be queried in interesting ways.


Don’t do anything that depends on the current date and time – that breaks the first rule, since your function’s output can change depending on the date/time it’s called. Common mistakes include emitting a timestamp, emitting a person’s age, or emitting only documents that have been modified in the past week.

So what you probably want to do in this case is emit the date regardless of what it is, and then filter as part of the query.

Hope that helps!

Hi thanks for your reply, but there are many document in the view about 10000

For example I got 20000 document of appointment. I’m going get customer data with customer ID from appointment document. we can use join query in SQL but how can I make this query in couchbase lite?

The map block would just look like emit(doc["createdDate", null]). This builds an index where the key is the createdDate property. Then you can query it with the key property set to whatever date you’re looking for.

Your map function must only use the doc passed in as an argument. A document only gets processed once when creating an index.

In other words, the check “if (doc[“createdDate”] != today)” will only get processed once for each revision of a document. This is why you see yesterday’s data when using the view again today. You must not do this.

The document ID is automatically emitted as part of the index. So, to get what you want, your map function should look like (as Jens said)

view.SetMap((doc, emit) =>
emit(doc[“createdDate”], null);
}, “1.1”);

Couchbase Lite can easily index 200000 documents like this.

Then, use a query with a key filter with the date set to “today”. Use the document ID in the results to retrieve the appointment document. That document can either embed the customer information, or you can put the customer ID in the document and retrieve it.

Another way to do it would be to emit the customer ID as the value in your index. For example

view.SetMap((doc, emit) =>
emit(doc[“createdDate”], doc[“customerID”]);
}, “1.1”);

If the customer ID is tied to the customer information document ID (they could be the same), then you can retrieve the customer information directly. There’s no need for a “join” like in SQL.

I implemented the Map Key like below method.
and I can get all Data without setting startKey and endKey.
But If I set the keys the query returns nothing.
public List Query(string viewName, string startKey, string endKey, int limit, int offset, string emitKey, string emitValue)
List list = new List();
var view = Manager.SharedInstance.GetDatabase(DatabaseName).GetView(viewName);
view.SetMap((doc, emit) =>
if (!doc["_id"].ToString().Contains(viewName))
emit(doc[emitKey], doc[emitKey]);
}, “2.12”);

        var query = view.CreateQuery();
        query.Limit = limit;
        query.StartKey = startKey;
        query.EndKey = endKey;
        query.Descending = true;
        var queryResult =
        foreach (var item in queryResult)
            var doc = GetObject<T>(item.Document.UserProperties);
        return list;