-
StackOverflow 文档
-
mongodb-csharp 教程
-
mongodb-csharp 入门
-
基本操作
class Program
{
static void Main(string[] args)
{
string serverName = "INTACT-ETL";
string databaseName = "SOMEDB";
string collectionName = "TestCol";
//Build Connection string
string connectionString = string.Format("mongodb://{0}", serverName);
//Create MongoClient and connect in one step
IMongoClient client = new MongoClient(connectionString);
//Create database object
IMongoDatabase database = client.GetDatabase(databaseName);
//Inserting one object
UserInfo userObject = new UserInfo {FirstName = "Bob",LastName = "Smith", DateOfBirth = DateTime.Now };
IMongoCollection<UserInfo> collection = database.GetCollection<UserInfo>(collectionName);
collection.InsertOne(userObject);
//Retrieving one object
UserInfo retrievedObject;
FilterDefinition<UserInfo> theFilter = Builders<UserInfo>.Filter.Eq(p => p.FirstName, "Bob");
retrievedObject = collection.Find<UserInfo>(theFilter).First();
Console.WriteLine("FirstName: {0}", retrievedObject.FirstName);
Console.WriteLine("LastName: {0}", retrievedObject.LastName);
Console.WriteLine("DateOfBirth: {0}", retrievedObject.DateOfBirth);
//Delete one object
DeleteResult result = collection.DeleteOne<UserInfo>(p => p.FirstName == "Bob");
Console.WriteLine("Is Acked : {0}",result.IsAcknowledged);
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
public class UserInfo
{
[MongoDB.Bson.Serialization.Attributes.BsonId]
public ObjectId? _id { get { return ObjectId.GenerateNewId(); } set { } }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
}