简体   繁体   English

如何使用Web api asp .net用bson id过滤从mongodb中获取数据?

[英]How can I take data from mongodb by filtering with bson id it with web api asp .net?

I would like prepare Employee Detail page with asp.net web api. 我想使用asp.net Web API准备“员工详细信息”页面。 And I wanted to take data from mongodb and filter it according to id. 我想从mongodb中获取数据并根据ID对其进行过滤。 But 'null' value appearing on the page. 但是“ null”值出现在页面上。 What can I do for fixing the problem? 我该如何解决该问题? Can you give me an idea? 你能给我个主意吗? Thank you in advance. 先感谢您。

My Employee Detail page is 我的员工详细信息页面是

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using MongoDB.Driver; using MongoDB.Bson; using TESTMONGO_C.Models; using System.Configuration; namespace TESTMONGO_C.Controllers { public class EmployeeDetailDataController : ApiController { [HttpGet] public object GetEmployeeById(string id) { string constr = ConfigurationManager.AppSettings["connectionString"]; var Client = new MongoClient(constr); var DB = Client.GetDatabase("test"); var collection = db.GetCollection<EmployeeDetail>("employee").Find(new BsonDocument("_id",ObjectId.Parse(id))).ToList(); return Json(collection); } } } 

My EmployeeDetail model is 我的EmployeeDetail模型是

 using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace TESTMONGO_C.Models { public class EmployeeDetail { [BsonRepresentation(BsonType.ObjectId)] public string id { get; set; } public string name { get; set; } public string department { get; set; } public string address { get; set; } public string city { get; set; } public string country { get; set; } } } 

Try following: 请尝试以下操作:

    [HttpGet]
    public object GetEmployeeById(string id)
    {
        string constr = ConfigurationManager.AppSettings["connectionString"];
        var Client = new MongoClient(constr);
        var DB = Client.GetDatabase("test");
        var collection = DB.GetCollection<EmployeeDetail>("employee");
        return Json(collection.Find(new BsonDocument("id", id)).ToList());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从URL传递到Web Api控制器以在ASP.NET MVC中进行测试 - how can pass data From URL to web Api controller for testing in asp.net mvc 如何将文件发布到 ASP.NET Web api - How can I post file to ASP.NET Web api 如何通过node.js将数据从MongoDB传递到我的web api? - How do I pass the data from MongoDB to my web api through node.js? 如何在ReactJS中过滤API数据(响应)? - How can I Filtering API data(response) in ReactJS? 从流星应用程序中的两个mongodb集合中获取数据(对_id进行过滤) - Getting data from two mongodb collections (filtering on _id) in Meteor Application 我想使用所选按钮的ID,但是如何从中获取ID? this.id不起作用 - I want to use the id of the selected button but how can i take the id from it? this.id doesnt work WEB API + ASP.NET尝试以json格式显示来自WEB.API的数据 - WEB API + ASP.NET trying to display data from WEB.API in json format 如何使用 AJAX 从视图中将 id 发布到 ASP.Net controller? - How I can POST an id to ASP.Net controller from view using AJAX? Mongoskin-如何在m​​ongodb字段_id上将主键字符串转换为bson? - Mongoskin - How to convert primary key string to bson at mongodb fields _id? Nodejs-MongoDB 如何通过 _id 和 title 获取数据 - Nodejs-MongoDB How can i get data by _id and title
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM