Sunday, June 4, 2017

MongoDB not querying

Leave a Comment

Hi I am trying to query my Mongo Database by using a list of Facebook ID's as the parameter in order to return the list of users with corresponding accounts. The method works fine in the Unity Editor however when I run it on iOS I get a constructor error (I have set a blank default constructor in order to solve the issue however it still doesn't work)

Initial Method

 public void FetchData()     {         //data = Mongo.Instance.players.FindAll().ToList();         if (FB.IsLoggedIn)         {             FB.API("me/friends", HttpMethod.GET, FriendsHighscoreHndlr);         }     } 

Callback Method

public void FriendsHighscoreHndlr (IGraphResult FBresult){                         var dict = Json.Deserialize(FBresult.ToString()) as Dictionary<string,object>;             var friendList = new List<object>();             friendList = (List<object>)(dict["data"]);              int _friendCount = friendList.Count;             Debug.Log("Found friends on FB, _friendCount ... " +_friendCount);             List<string> friendIDsFromFB = new List<string>();             for (int i=0; i<_friendCount; i++) {                 string friendFBID = getDataValueForKey( (Dictionary<string,object>)(friendList[i]), "id");                 string friendName =    getDataValueForKey( (Dictionary<string,object>)(friendList[i]), "name");                 Debug.Log( i +"/" +_friendCount +" " +friendFBID +" " +friendName);                 friendIDsFromFB.Add(friendFBID);             }             //friendIDsFromFB.Add(AccessToken.CurrentAccessToken.UserId);             var query = Query.In("facebookID", BsonArray.Create(friendIDsFromFB));             //Debug.Log(query);             data = Mongo.Instance.players.Find(query).ToList();         } 

Data Value for Key Method

private string getDataValueForKey(Dictionary<string, object> dict, string key) {             object objectForKey;             if (dict.TryGetValue(key, out objectForKey)) {                 return (string)objectForKey;             } else {                 return "";             }         } 

Return Query Result

{     "_id" : ObjectId("XXXXXX"),     "facebookID" : "XXXXXXXXXXXXXX",     "name" : "John Doe",     "highScore" : 40501 } 

1 Answers

Answers 1

Have you tried using a filter from Mongo's .NET drivers?

I would suggest trying something like the example below. (Note: this event is being fired on a pre-defined object which is being defined as a collection.)

        var filter = Builders<Object>.Filter.Eq(obj => obj.attribute, List<IDs>); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment