Views and Queries not working in Couchbase Lite app


#1

I’m not sure if I have this correct, but I am unable to create a view and query the view.

I’m migrating from a sqlite database that contained 4 different tables (let’s call them A,B,C,D). Each of the rows are getting migrated to CouchbaseLite as a document with a field of “Type” of value A, B, C, or D to different the data of each sqlite table.

I’m trying to create views to represent all the data of type A, and all the data of type B, etc.

Here’s the code snippet to create the view, where “Identifier” is a field I want to use as the primary key.

	var typeAView = _database.GetView ("TypeAView");
	typeAView.SetMap ((doc, emit) => {

		if(doc["Type"] == "A")
		{
			emit(doc["Identifier"], doc);
		}
			
	}, "1");

Here’s my query to get all rows of Type A:

		var query = _database.GetView ("TypeAView").CreateQuery();
		var rows = query.Run ();
		foreach (QueryRow queryRow in rows)
		{
                      //do something here 
		}

The query returns no results, is there an issue with the view map function?


#2

Hi, if you have no result, it could be you are using an ‘old’ view that did not work previously. Can you try by upgrading the view version number?

typeAView.SetMap ((doc, emit) => {

	if(doc["Type"] == "A")
	{
		emit(doc["Identifier"], doc);
	}

}, "2");