As a newcomer to MongoDB, I've set up two collections: "groups" for group information and "groupUsers" to establish user-group relationships.
Currently, my goal is to retrieve all groups that the current user belongs to and then store this information in an array. However, I'm facing an issue where only the first or last document is being added to the array, despite knowing that the user is associated with 2 groups.
This is how I am attempting to achieve it:
var playerID = Spark.getPlayer().getPlayerId();
var dataList = Spark.runtimeCollection('groupUsers');
var myGroups = dataList.find({ user: playerID });
var groups = [];
if( myGroups.hasNext() ) {
var dataList2 = Spark.runtimeCollection('groups');
var obj = myGroups.next();
var thisGroup = dataList2.find({ "_id": {"$oid": ""+obj.group}});
groups.push(thisGroup);
}
Spark.setScriptData("myUserGroups", groups);
I hope this explanation is clear enough. Any assistance on what may be going wrong or pointing me in the right direction would be greatly appreciated :-)
Thank you in advance.