Extracting document from DB, which way is faster?

Hi. I am wondering.
Let say I wish to create an Hashmap and save it to the DB.
Each hashMap has it’s own unique Id which I create on my app and put inside it in a field called id, for this example the id will be “123”.

In order the save it to the db, I create a new MutableDocument(“123”) and put my hashmap inside it
( newDoc.setValue(“myHash”, myHashMap); )
Also, i set newDoc.setValue(“id”, “123”);

Bottom line is that both the MutableDocument and the HashMap has the same id 123.
In order to extract my hashmap back, I can do it in three ways:

mDatabase.getDocument(“123”));

or

use query: query = QueryBuilder.select(SelectResult.all())
.from(DataSource.database(mDatabase))
.where(Expression.property(“id”).equalsTo(Expression.string(“123”)));
ResultSet res = query.execute();

Or

use query: query = QueryBuilder.select(SelectResult.all())
.from(DataSource.database(mDatabase))
.where(Expression.property(“myHash.id”).equalsTo(Expression.string(“123”)));
ResultSet res = query.execute();

The question is which which way is better? What is the best practice for it? which way is faster?
Thanks.

getDocument(“123”) is the fastest. The queries will probably be nearly as fast if you create an index on that property … otherwise they’ll be slow (and their run time will go up as O(n) with the number of documents.) However, adding indexes will make your database somewhat larger, and will slightly slow down every insert/update.

In general there’s no point in duplicating the document ID inside the document. It just wastes space and doesn’t really provide any benefit.

Thanks for your response!

Just to add a little more info, getDocument()-style lookups are what is meant by key/value retrieval. That’s the base underlying mechanism Couchbase is optimized for (like many other NoSQL dbs). That would be the fastest to use on Couchbase Server as well.