1、安装nuget包
MongoDB.Driver
2、简单代码
using MongoDB.Bson; using MongoDB.Driver; using System.Buffers; using System.Collections.Concurrent; using System.Diagnostics; namespace ConsoleApp4 { internal class Program { static void Main(string[] args) { var client = new MongoClient("mongodb://192.168.11.137"); var database = client.GetDatabase("MyTestDB"); var collection = database.GetCollection<UserInfo>("UserInfo"); collection.InsertOne(new UserInfo { Name = "Jack", CreateTime = DateTime.Now, }); var query = collection.Find(m => m.Name.Contains("Ja")).FirstOrDefault(); } } class UserInfo { public ObjectId Id { get; set; } public string Name { get; set; } public DateTime CreateTime { get; set; } } }
public ObjectId Id { get; set; } 必须要在model类中定义,否则查询会报错
标签:set,get,MongoDB,使用,var,using,Net,public From: https://www.cnblogs.com/IWings/p/17053382.html