gpt4 book ai didi

c# - 无法在 MongoDB Web API 中按 ID 获取文档

转载 作者:可可西里 更新时间:2023-11-01 10:41:00 27 4
gpt4 key购买 nike

我的 Get 请求允许我通过 ID 获取我的集合中的第一个文档,但不允许我通过 ID 获取任何其他文档,因此它只会通过 ID 或 null 返回第一个文档。我还有一个 GetAll 请求返回我数据库中的所有 1000 个文档,这似乎有效。

这是我的代码:

在 Controller 中获取请求:

 // GET api/something/id
[HttpGet]
public HttpResponseMessage Get(string id)
{

var something = _somethingService.Get(id);
if (something != null)
{
return Request.CreateResponse(HttpStatusCode.OK, something);

}
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Nothing for provided id.");
}

模型:

public class SomethingsModel
{
[BsonElement("_id")]
[BsonRepresentation(BsonType.String)]
public ObjectId id { get; set; }

public string SomethingId
{
get { return id.ToString(); }
set { id = ObjectId.Parse(value); }
}

[BsonElement("name")]
public string Name { get; set; }

[BsonElement("description")]
public string Description { get; set; }
//[BsonElement("category")]
//public string Category { get; set; }

[BsonElement("address")]
public AddressModel Address { get; set; }

[BsonElement("latlong")]
public LatLongModel LatLong { get; set; }

[BsonElement("price")]
public int Price { get; set; }

[BsonElement("rating")]
public int Rating { get; set; }

[BsonElement("photourl")]
public string PhotoUrl { get; set; }

}

Somethings 服务:

 public class SomethingService : ISomethingService
{
private readonly SomethingsUnitOfWork _sUnitOfwork;

public SomethingService()
{
_sUnitOfwork = new SomethingsUnitOfWork();
}

public SomethingsModel Get(string id)
{
return _sUnitOfwork.Somethings.Get(id);
}

public IQueryable<SomethingsModel> GetAll()
{
return _sUnitOfwork.Somethings.GetAll();
}

public void Delete(string id)
{
_sUnitOfwork.Somethings.Delete(s => s.SomethingId, id);
}

public void Insert(SomethingsModel something)
{
_sUnitOfwork.Somethings.Add(something);
}

public void Update(SomethingsModel something)
{
_sUnitOfwork.Somethings.Update(s => s.SomethingId, something.SomethingId, something);
}

}

SoemthingsRepository:

public class SomethingsRepository<T> where T : class
{
private MongoDatabase _database;
private string _tableName;
private MongoCollection<T> _collection;

// constructor to initialise database and table/collection
public SomethingsRepository(MongoDatabase db, string tblName)
{
_database = db;
_tableName = tblName;
_collection = _database.GetCollection<T>(tblName);
}


/// <summary>
/// Generic Get method to get record on the basis of id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public T Get(string id)
{
return _collection.FindOneById(id);

}

/// <summary>
/// Get all records
/// </summary>
/// <returns></returns>
public IQueryable<T> GetAll()
{
MongoCursor<T> cursor = _collection.FindAll();
return cursor.AsQueryable<T>();

}

/// <summary>
/// Generic add method to insert enities to collection
/// </summary>
/// <param name="entity"></param>
public void Add(T entity)
{
_collection.Insert(entity);
}

/// <summary>
/// Generic delete method to delete record on the basis of id
/// </summary>
/// <param name="queryExpression"></param>
/// <param name="id"></param>
public void Delete(Expression<Func<T, string>> queryExpression, string id)
{
var query = Query<T>.EQ(queryExpression, id);
_collection.Remove(query);
}

/// <summary>
/// Generic update method to delete record on the basis of id
/// </summary>
/// <param name="queryExpression"></param>
/// <param name="id"></param>
/// <param name="entity"></param>
public void Update(Expression<Func<T, string>> queryExpression, string id, T entity)
{
var query = Query<T>.EQ(queryExpression, id);
_collection.Update(query, Update<T>.Replace(entity));
}

}




And

我的文档架构:

{
"_id": {
"$oid": "591593fbb2c2a737588dbbcc"
},
"name": "Kulas Inc",
"description": "Bypass Esophag to Duoden with Synth Sub, Perc Endo Approach",
"address": {
"lineone": "9 Leroy Terrace",
"city": "Afonsoeiro",
"country": "Portugal",
"postcode": "2870-013"
},
"latlong": {
"latitude": "38.7",
"longitude": "-8.9167"
},
"price": 61,
"rating": 70,
"photourl": "http://dummyimage.com/160x221.jpg/dddddd/000000"
}

对代码量表示歉意。我不确定为什么我能够获得我收藏中的第一份文件,但其他文件都没有。我感觉我的代码中存在序列化问题,或者我的 _id 嵌套在每个文档中?

最佳答案

您的实体的 ID 类型为 ObjectId。当您从数据库中获取实体时,您应该使用此类型而不是字符串:

public T Get(string id)
{
return _collection.FindOneById(ObjectId.Parse(id));
}

注意事项:

  • 您可以使用 BsonClassMap 的小写约定来默认存储所有小写元素。
  • MongoDb 使用 _id 名称作为身份字段。您无需手动指定。
  • 您可以将方法的返回类型更改为 IHttpActionResult 并使用 ApiController 中的 OkNotFound 方法来创建适当的操作结果。<

关于c# - 无法在 MongoDB Web API 中按 ID 获取文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44178810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com