Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C中序列化为JSON的mongodb文档中删除ObjectId#_C#_Json_Reactjs_Mongodb_Parsing - Fatal编程技术网

C# 从C中序列化为JSON的mongodb文档中删除ObjectId#

C# 从C中序列化为JSON的mongodb文档中删除ObjectId#,c#,json,reactjs,mongodb,parsing,C#,Json,Reactjs,Mongodb,Parsing,我这里有点问题。我使用此功能从mongodb集合获取所有产品: public async Task<string> getAllProducts() { List<string> all = new List<string>(); var document = await getCollection("produits").Find(new BsonDocument()).ToCursorAsy

我这里有点问题。我使用此功能从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);
        }
问题是我无法在javascript中解析它,因为:ObjectId(“5e49bdf5f040e808847a17d7”)

当然,在解析之前我可以做一些字符串魔术,但是我宁愿在服务器端更正它。有没有办法解决这个问题,得到这样的结果

{ "_id" : "5e49bdf5f040e808847a17d7", 
    "email" : "example@gmail.com", 
    "quantite" : 1, 
    "matricule" : 1}

试试这个。它将序列化没有objectid内容的字符串id

公共静态异步任务getAllProducts() { var collection=db.GetCollection(“produits”); var all=新列表(); 使用(var cursor=await collection.FindAsync(“{}”)) { while(等待cursor.MoveNextAsync()) { foreach(cursor.Current.ToArray()中的var doc) { 全部。添加(doc); } } } 返回Newtonsoft.Json.JsonConvert.SerializeObject(全部); }
通过为mongodb对象创建类进行修复。

public class Product
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public int matricule { get; set; }
    public int quantite { get; set; }
    public string email { get; set; }
    public float prix { get; set; }
    public string image { get; set; }
}
获取它们并使用BsonSerializer反序列化:

public async Task<List<Product>> getAllProducts(){
    var collection = await getCollection("produits").Find(new BsonDocument()).ToListAsync();
    List<Product> all = new List<Product>();
    foreach(var doc in collection){
        all.Add(BsonSerializer.Deserialize<Product>(doc));
    }
    return all;
}
公共异步任务getAllProducts(){ var collection=await getCollection(“produits”).Find(new BsonDocument()).toListSync(); List all=新列表(); foreach(集合中的var单据){ 添加(BsonSerializer.Deserialize(doc)); } 全部归还; } 应要求返回它们:

[HttpGet]
public async Task<string> ShowProductsAsync()
{
    MongodbModel model = new MongodbModel();
    var products = await model.getAllProducts();
    Console.WriteLine(products);
    return JsonConvert.SerializeObject(products);
}
[HttpGet]
公共异步任务ShowProductsAsync()
{
MongodbModel model=新的MongodbModel();
var products=await model.getAllProducts();
控制台、写入线(产品);
返回JsonConvert.SerializeObject(产品);
}
字符串GetAllProduits(){
var collection=database.GetCollection(“produits”);
var project=Builders.Projection.Exclude(“_id”);
var filter=Builders.filter.Empty;
var rlt=collection.Find(filter.Project)(项目);
返回rlt.ToList().ToJson();
}

好了!是的,这解决了这个问题,非常感谢。但它确实有一个小小的缺陷,每个键都有自己的json,而不是一个包含所有键的json,但谢天谢地,这是一个解决方法。如果你能解释一下你在这段代码中做了什么,那就太好了。
[HttpGet]
public async Task<string> ShowProductsAsync()
{
    MongodbModel model = new MongodbModel();
    var products = await model.getAllProducts();
    Console.WriteLine(products);
    return JsonConvert.SerializeObject(products);
}
string GetAllProduits(){
    var collection = database.GetCollection<BsonDocument>("produits");
    var project = Builders<BsonDocument>.Projection.Exclude("_id");
    var filter = Builders<BsonDocument>.Filter.Empty;
    var rlt=collection.Find(filter).Project(project);
    return rlt.ToList().ToJson();
}