[C#] C# 与 MongoDB 的 CURD

时间:2022-10-21 16:06:56

C# 与 MongoDB 的 CURD

        static void Main(string[] args)
{
//建立连接
var client = new MongoClient();
//建立数据库
var database = client.GetDatabase("TestDb");
//建立collection
var collection = database.GetCollection<BsonDocument>("foo"); var document = new BsonDocument
{
{"name","MongoDB"},
{"type","Database"},
{"count",},
{"info",new BsonDocument{{"x",},{"y",}}}
};
//插入数据
collection.InsertOne(document); var count = collection.Count(document);
Console.WriteLine(count); //查询数据
var document1 = collection.Find(document);
Console.WriteLine(document1.ToString()); //更新数据
var filter = Builders<BsonDocument>.Filter.Eq("name", "MongoDB");
var update = Builders<BsonDocument>.Update.Set("name", "Ghazi"); collection.UpdateMany(filter, update); //删除数据
var filter1 = Builders<BsonDocument>.Filter.Eq("count", ); collection.DeleteMany(filter1); BsonDocument document2 = new BsonDocument();
document2.Add("name", "MongoDB");
document2.Add("type", "Database");
document2.Add("count", ""); collection.InsertOne(document2); Console.Read();
}