我这里有点问题。我正在使用此功能从 mongodb 集合中获取我的所有产品:
public async Task<string> getAllProducts()
{
List<string> all = new List<string>();
var document = await getCollection("produits").Find(new BsonDocument()).ToCursorAsync();
foreach (var doc in document.ToEnumerable())
{
var res = doc.ToJson();
all.Add(res);
}
return JsonConvert.SerializeObject(all);
}
它向我的 React 前端返回一个看起来像这样的 JSON。
{ "_id" : ObjectId("5e49bdf5f040e808847a17d7"),
"email" : "example@gmail.com",
"quantite" : 1,
"matricule" : 1}
问题是我无法在我的 javascript 中解析它,因为:ObjectId("5e49bdf5f040e808847a17d7")
当然,我可以在解析它之前做一些字符串魔术,但是 id 而是在服务器端对其进行更正。那么有没有办法摆脱这个问题并得到这样的结果?
{ "_id" : "5e49bdf5f040e808847a17d7",
"email" : "example@gmail.com",
"quantite" : 1,
"matricule" : 1}