2017-05-26 127 views
4

嗨我想通過使用Facebook ID列表作爲參數來查詢我的Mongo數據庫,以便返回具有相應帳戶的用戶列表。該方法在Unity編輯器工作正常,但是當我在iOS上運行它,我得到一個構造錯誤(我已成立一個空白默認的構造函數,爲了解決這個問題但它仍然無法正常工作)MongoDB不查詢

初始方法

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

回調方法

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(); 
     } 

數據價值的關鍵方法

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

返回查詢結果

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

您是否可以添加希望查詢返回的文檔?還請爲'FBresult'添加輸入值。 – Veeram

+0

@Veeram我添加了編輯 – Prad

+0

謝謝。試着在沒有任何過濾器的情況下查詢數據庫,看看你是否得到任何結果(這將確認你正在查詢正確的數據庫/集合)。當你運行上面的代碼時,你會得到什麼樣的迴應?當你在代碼中記錄數值時,你看到了正確的'friendFBIDs'嗎?你能分享和驗證正確的輸入值嗎?IGraphResult FBresult'傳遞給方法'FriendsHighscoreHndlr'? – Veeram

回答

4

您是否嘗試過使用從蒙戈的.NET驅動程序的過濾器?

我會建議嘗試像下面的例子。 (注意:此事件正在被定義爲集合的預定義對象上觸發。)

 var filter = Builders<Object>.Filter.Eq(obj => obj.attribute, List<IDs>);