问题
We are looking at implementing Couchbase for an iPad App built with Xamarin.IOS. This is the first time we are digging into Couchbase and wanted to make sure that our use case is something that is doable with Couchbase.
Our primary use case is to be able to query the data stored in the NoSQL database. Views seem like the only way to do this. It seems strange that the actual document cannot be queried directly like other NoSQL databases like MongoDB.
Say our data is in the following format:
{
"Id":1,
"FirstName":"John",
"LastName":"Doe",
"DepartmentId":1,
"StatusId":2
}
We would want to query this data as follows:
- All records with DepartmentId = 1
- All records with StatusId = 2
- All records with DepartmentId = 1 AND StatusId = 2
- All records with DepartmentId = 1 OR StatusId = 2
From this presentation, http://www.slideshare.net/Couchbase/advanced-couchbase-lite I can see that we can create a view with a compound key. Something like [ DepartmentId, StatusId ]
But we are not able to query with just one part of the compound key. Couchbase seems to return results only if both the parts of the compound key are passed eg. [ 1, 2 ] This would return all records with department 1 & status 2.
Is this possible with Couchbase at all? Or should we be looking at some other solution? We don't want to go the SQL way because we have quite complex JSON objects, and don't want to bother with converting it into tabular data. NOSQL is the way ahead but is this the wrong NOSQL DB for us?
C# that we tried :
// Create the View
var view = db.GetView("all-docs");
view.SetMap((doc, emit) =>
{
var keys = new List<object>();
keys.Add(doc["DepartmentId"].ToString());
keys.Add(doc["StatusId"].ToString());
emit(keys, doc);
}, "1");
// Create Query
var query = view.CreateQuery();
List<Object> searchQuery = new List<Object>();
List<Object> key = new List<Object>();
// Add Query for Keys
key.Add(status);
key.Add(priority);
searchQuery.Add(key);
query.Keys = searchQuery;
// Run Query
var results = await query.RunAsync();
Lastly, any benchmarks on the performance of Couchbase Lite Queries if we are dealing with about 1000-5000 objects?
回答1:
To query on a range of values, you can use startKey
and endKey
as documented in http://developer.couchbase.com/documentation/mobile/1.2/develop/references/couchbase-lite/couchbase-lite/query/query/index.html. So when you have a view based on [DeptId, StatusId]
, you get
all docs with
DeptId
= 1 by settingstartKey
to[1, 0]
andendKey
to[1, maxint]
all docs with
DeptId
= 1 andStatusId
= 2 by setting the key to[1, 2]
all docs with
StatusId
= 2 by using another view that hasStatusId
as the first or only component.I'm not aware of a way to directly simulate an OR operator, so you might to need to retrieve both sets separately and merge them programmatically. (This pertains to CB lite. On CB server you have N1QL which is more powerful.)
The overall performance on the mobile device will not only depend on CB lite on its own but also on the handling of the documents after they got retrieved from the database. Of course, getting 100 records of 5 attributes each from a relational DB is faster than building up a mesh of Java objects from 100 complex JSON documents. So I'm afraid you'll have to benchmark yourself, using your document sizes, structure, and query results.
来源:https://stackoverflow.com/questions/36598905/is-couchbase-lite-meant-for-querying-data