2017-07-28 125 views
0

我將一個MongoDB(Azure)與一個MVC .NET C#項目連接起來。到目前爲止,連接和對象定義工作得非常好。我的問題是,當我嘗試添加方法FIND()以返回對象USER中的所有數據時。如何使用FIND()在MVC.net上的MongoDB查詢C#項目

我的模型:

using System; 
using System.Collections.Generic; 
using MongoDB.Bson; 
using MongoDB.Bson.Serialization.Attributes; 
using MongoDB.Driver; 
using MongoDB.Driver.Builders; 

namespace backendnet.Models 
{ 
    public class MongoCore 
    {  
     public class DB 
     { 
      static MongoClient Client = new MongoClient("mongodb://mydbconnect"); 
      static public IMongoDatabase Database = Client.GetDatabase("mydb"); 
      static public IMongoCollection<User> Users = Database.GetCollection<User>("users"); 
     } 

     public class User 
     { 
      [BsonId] 
      public ObjectId Id { get; set; } 
      [BsonElement("email")] 
      public string Email { get; set; } 
      [BsonElement("password")] 
      public string Password { get; set; } 
      [BsonElement("name")] 
      public List<DimensionName> Name { get; set; } 
      [BsonElement("address")] 
      public List<DimensionAddress> Address { get; set; } 
      [BsonElement("permissions")] 
      public List<DimensionPermissions> Permissions { get; set; } 
      [BsonElement("status")] 
      public string Status { get; set; } 
      [BsonElement("created")] 
      public string Created { get; set; } 
      [BsonElement("updated")] 
      public string Updated { get; set; }  
     } 

     public class DimensionName 
     { 
      [BsonElement("first")] 
      public string First { get; set; } 
      [BsonElement("last")] 
      public string Last { get; set; }  
     } 

     public class DimensionAddress 
     { 
      [BsonElement("stree")] 
      public string Stree { get; set; } 
      [BsonElement("number")] 
      public string Number { get; set; } 
      [BsonElement("city")] 
      public string City { get; set; } 
      [BsonElement("state")] 
      public string State { get; set; } 
      [BsonElement("zipcode")] 
      public string Zipcode { get; set; } 
      [BsonElement("type")] 
      public string Type { get; set; } 
     } 

     public class DimensionPermissions 
     { 
      [BsonElement("list")] 
      public string List { get; set; } 
      [BsonElement("create")] 
      public string Create { get; set; } 
      [BsonElement("edit")] 
      public string Edit { get; set; } 
      [BsonElement("delete")] 
      public string Delete { get; set; } 
     } 
    } 
} 

我的控制器:

using System; 
using System.Collections.Generic; 
using System.Web.Mvc; 
using backendnet.Models; 
using MongoDB.Bson; 

namespace backendnet.Controllers 
{  
    public class DashboardController : Controller 
    {  
     private string _viewFolder = "../Admin/Dashboard"; 

     public ActionResult Index() 
     { 
      var results = new MongoCore.DB(); 
      ViewData["ListPost"] = results.ToJson(); 

      return View (_viewFolder); 
     }    
    } 
} 

我查看部分:

<p>HERE: @ViewData["ListPost"]</p> 

我得到這個:

位置:{}

所以我嘗試添加在Model - > DB的方法Find

MongoCursor<User> cursor = Users.Find("Email" != ""); 

但始終顯示錯誤:

Expression is always 'true' ["Email" != ""]

五月任何人告訴我什麼,我在這裏失蹤?

回答

1

我不看你調用MongoDB.Find()?我粘貼了我用於MongoDB C#驅動程序的代碼,以獲得基於MongoDB數據庫中的key:value對的記錄。

Find或FindAsync方法都需要一個BsonDocument參數,它可以使用Builders創建,如下所示。你的過濾器可以是空的,這將獲得所有記錄,因爲你沒有過濾掉任何東西。

調用find方法後,您將能夠使用Lambda或其他查詢方法訪問信息。你可以在我的查詢中看到我只需要一條記錄,所以我要求FirstOrDefault。希望這可以幫助。

async Task<Document> IDal.GetRecordAsync(string key, string value) 
    { 
     try 
     { 
      if (Database == null) ((IDal)this).StartConnection(); 
      var filter = Builders<BsonDocument>.Filter.Eq(key, value); 
      var cursor = await Collection.FindAsync(filter); 
      var bsondocument = cursor.FirstOrDefault(); 
      return bsondocument == null ? null : _converter.ConvertBsonDocumentToDocument(bsondocument); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex); 
      return null; 
     } 
    } 
+0

感謝@CodeSurvivor,但我看不出如何實現我的實際模型內部解決方案 –

0

我改變了一點點我的模型和控制器方法。

我的模型(DB法):

public class DB 
{ 
    public IMongoDatabase Database; 
    public DB() 
    { 
     var client = new MongoClient("mongodb://mydbconnect"); 
     Database = client.GetDatabase("mydb"); 
    } 
    public IMongoCollection<User> Data => Database.GetCollection<User>("users"); 
} 

和我的控制器:

public ActionResult Index() 
{ 
    DB sc = new DB(); 
    ViewData["ListPost"] = sc.Data.Find(Builders<User>.Filter.Where(t => t.Email != "")).ToJson(); 

    return View (_viewFolder); 
} 

現在我得到這個在我看來:

位置:{ 「_t」:「FindFluent 2", "Filter" : { "_t" : "ExpressionFilterDefinition 1「,」Expression「:{」_t「:」Expression1`1「}}}

所以我不知道FIND()我你是否已經退回文件。或者,如果我需要以不同的方式在視圖中打印@ViewData [「ListPost」]?

0
public ActionResult GetUsers() 
{ 
MongoServer objServer = MongoServer.Create("Server=localhost:27017"); 
MongoDatabase objDatabse = objServer.GetDatabase("DBName"); 
List UserDetails = objDatabse.GetCollection("Colletion_Name").FindAll().ToList(); 
return View(UserDetails); 
} 
+0

這是演示MongoDB的find()方法,你可以爲你的一個 –

+0

整合要素我編輯的代碼,得到的所有數據,你可以試試這個。在這裏,我使用localhost作爲服務器,你可以給你的服務器地址,並且爲你的數據庫名稱輸入** DBName **,爲你的Collection輸入** Collection_Name **。試試這個快樂的編碼。 –