I am using mongodb as my backend. I have to store similar document ids in a list for each document. Here is a sample record in my collection.
{
"_id": ObjectID("...."),
"doc_name": "my_doc1",
"doc_id": "doc_id_1",
"similar_docs": ["doc_id_6", "doc_id_86", "doc_id_93"],
"more_fields": "..."
}
This way I am keeping track of similar documents to each document.
Now I wish to have the complete json for all the similar documents to a given document. Right now I am implementing it using a for loop over the list of doc_id’s making a request to database for each doc_id. Is there any subtle way to do this work.
You can have a single query, something along these lines:
But if you wanted a way to somehow “expand” those ids into documents when querying for parent document, then you can’t do that. Unless you store full documents in that array (which can be expensive or impossible (if resulting document exceeds max document size)).