I'm developing Couchbase Lite on Android and I have a database, what is 
initialized according to JSON data file, what looks like this:
{
    "categories": [
        {
            "name": "Financial",
            "books": [
                {
                    "name": "Rich dad poor dad"
                },
                {
                    "name": "How to stay rich"
                }
            ]
        },
        {
            "name": "Science",
            "books": [
                {
                    "name": "Superman"
                },
                {
                    "name": "Green hornet"
                }
            ]
        }
    ]
}

How can I receive the "books"-array and gain access to "name"-value for 
certain category?

I separated all the categories in a database to a separate Documents and 
gave them ID according to category name(Is it reasonable to separate them 
or it doesn't make any difference?).
For example, if i want to get access to "financial" category i type:
Document d = database.getExistingDocument("financial");
and everything works fine. 

If i want to get all the categories i type:
List<Category> list = new ArrayList<Category>();
Database db = manager.getExistingDatabase(DATABASE_NAME);
Query query = db.createAllDocumentsQuery();
QueryEnumerator rowEnum = query.run();
for (Iterator<QueryRow> it = rowEnum; it.hasNext();) {
   QueryRow row = it.next();
   Document d = db.getExistingDocument(row.getDocumentId());
   list.add(new Category((String) d.getProperty("name")));
}

But how can i get access to "books"-array?
So far this is what i've come up with (Read APIs and GrocerySync example):
Document d = db.getExistingDocument(categoryID);
com.couchbase.lite.View viewItemsByDate = db.getView("booksForCategory");
viewItemsByDate.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
Object createdAt = document.get("books");
if (createdAt != null) {
emitter.emit(createdAt.toString(), document);
}
}
}, "1.0");
com.couchbase.lite.View v = db.getView("booksForCategory");
Query q = v.createQuery();
QueryEnumerator rowEnum = q.run();
for (Iterator<QueryRow> it = rowEnum; it.hasNext();) {
QueryRow row = it.next();
Book b = new Book(row.getProperty("name"));//???
}
But in my oppinion, it doesen't work correctly and this is not the way to 
do it. It returns wrong results and i'm pretty sure this is not how it 
should be done. 

(Oh, Do i define "getView("booksForCategory")" once in the constructor of 
the DAO or every single time?)

Could anyone help me and show me how it's done properly? How to properly 
get the array of data from the Document so i can get access to properties 
using Key's?

Thanks in advance!

-- 
You received this message because you are subscribed to the Google Groups 
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mobile-couchbase/6732a96e-7b7c-4a6f-b94b-d262008cc444%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to