Hello all. I’m developing a gaming inventory management system for a game using mongodb.
Here I have the file from the Account “gg” and character “pipi”.
inside the array invenItem there is all my obejcts itens.
I need to do a search by the id,name and them inside the array by the dwCode, dwHead and dwChkSum. Then I need to extract the “IDA” and put to a variable.
I will only have one document per character.
Forgot my actual code:
auto cursor = InventoryCollection.find_one(
make_document(
kvp("ID", Player->getAccount()),
kvp("nick", Player->getName()),
kvp("invenItem.dwHead", static_cast<signed long>(Head)),
kvp("invenItem.dwCode", static_cast<signed long>(Code)),
kvp("invenItem.dwChkSum", static_cast<signed long>(ChkSum))
));
Hello all.
With the lack of the support from the MongoDB team/community I got to find my own way to this after several hours.
For anyone with the same problem I came to this solution:
mongocxx::pipeline pipe{};
pipe.match(make_document(kvp("ID", "gg"), kvp("nick", "pipi"))); //if my main element has this
pipe.unwind("$invenItem"); //unwind the array
pipe.sort(document{} << "invenItem.IDA" << 1 << finalize); // sort by the IDA element
pipe.match(
make_document(
kvp("invenItem", make_document(kvp("$ne", bsoncxx::builder::basic::array{})))
)
); // make an array with a invenItem document if it isn't empty
auto cursor2 = InventoryCollection.aggregate(pipe, mongocxx::options::aggregate{});
int i = 0;
for (auto doc : cursor2) {
std::cout << bsoncxx::to_json(doc) << std::endl;
Player->InvenItemInfo[i].dwCode = doc["invenItem"]["dwCode"].get_int32();
Player->InvenItemInfo[i].dwKey = doc["invenItem"]["dwHead"].get_int32();
Player->InvenItemInfo[i].dwSum = doc["invenItem"]["dwChkSum"].get_int32();
i++;
if (i > 100)
{
break;
}
}
My main problem now is the for (auto doc : cursor2) part taking around ~1 ms. (5 invenItem documents)
While searching a file and opening it with commands like fread then copying the data from it to a struct take around 0.5ms. (15+ invenItem documents)