Is it encouraged to query a MongoDB database in a loop? -
in sql database, if wanted access sort of nested data, such list of tags or categories each item in table, i'd have use obscure form of joining in order send sql query once , loop through result cursor.
my question is, in nosql database such mongodb, ok query database repeatedly such can previous task follows:
cursor = query items each item in cursor tags = query item's tags
i know can store tags in array in item's document, i'm assuming somehow not possible store inside same document. if case, expensive requery database repeatedly or designed used way?
no, neither in mongo, nor in other database should query database in loop. , 1 reason performance: in web apps, database bottleneck , devs trying make small amount of db calls possible, whereas here trying make many possible.
i mongo can want in many ways. of them are:
- putting tags inside document
{itemname : 'item', tags : [1, 2, 3]}
- knowing list of elements, not need loop find information them. can fetch results in 1 query $in :
db.tags.find({ field: { $in: [<value1>, <value2>, ... <valuen> ] }})
Comments
Post a Comment