2017-05-27 20 views
-1

新しいapiでmongodbを使用して挿入、削除、更新、検索する方法について知りたいです。 これは私の挿入クエリです。私はこの新しいAPIで更新と削除を書きたいと思う。mongodbを使用してc#.netアプリケーションを挿入、更新、削除する方法

//クエリ

string connectionString = "mongodb://127.0.0.1:27017"; 
      MongoClient client0 = new MongoClient(connectionString); 
      IMongoDatabase mydatabase = client0.GetDatabase("mydb"); 
      IMongoCollection<user_data> mycollection = mydatabase.GetCollection<user_data>("testcollection"); 
      mycollection.InsertOne(new user_data { first_name = txtFirstName.Text, last_name = txtLastName.Text, age = txtAge.Text, location = txtLocation.Text }); 
      { 
       MessageBox.Show("Saved Successfully!"); 
      } 
+0

[アイテムを削除する](https://stackoverflow.com/questions/8867032/how-to-remove-one-document-by-id-using-the-official-c-sharp-driver-for-mongo) 、 [アイテムの更新](https:// stac koverflow.com/questions/30257013/mongodb-c-sharp-driver-2-0-update-document) 検索機能を使用してください。 – Skami

答えて

0

を挿入し、私はCSHARPドライバのドキュメントが必ずしも明確ではないか、最新の...ここで

は私の実装であることに同意:

public class MongoRepository 
{ 
    const string DB_NAME = "dbname"; 
    public const string HOST = "serveradress"; 
    const string CONNECT_STRING = "connectionstring"; 
    private IMongoClient _client; 
    private IMongoDatabase _db { 

     get { return this._client.GetDatabase(DB_NAME); } 
    } 
    public MongoRepository() 
    { 
     _client = new MongoClient(CONNECT_STRING); 
    } 
    //Add a BSON document from a json string 
    public void Add(string json, string collectionName) 
    { 
     var document = BsonSerializer.Deserialize<BsonDocument>(json); 
     var collection = _db.GetCollection<BsonDocument>(collectionName); 
     collection.InsertOne(document); 
    } 

    //Add an item of the given type 
    public void Add<T>(T item) where T : class, new() 
    { 
     _db.GetCollection<T>(typeof(T).Name).InsertOne(item); 

    } 
    public async Task UpdateDocument<T>(ObjectId Id, T item) 
    { 
     var startTime = DateTime.UtcNow; 
     try 
     { 
      var bsonWriter = new BsonDocumentWriter(new BsonDocument(), BsonDocumentWriterSettings.Defaults); 
      BsonSerializer.Serialize<GlobalModel_Root>(bsonWriter, GlobalModel.rootGM); 
      var doc = bsonWriter.Document; 
      var collection = _db.GetCollection<BsonDocument>(typeof(T).Name); 
      var filter = Builders<BsonDocument>.Filter.Eq("_id", Id); 
      var entity = collection.Find(filter).FirstOrDefault(); 
      using (var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(60))) 
      { 
       await collection.ReplaceOneAsync(filter, doc, null, timeoutCancellationTokenSource.Token); 
      } 
     } 
     catch (OperationCanceledException ex) 
     { 
      var endTime = DateTime.UtcNow; 
      var elapsed = endTime - startTime; 
      Console.WriteLine("Operation was cancelled after {0} seconds.", elapsed.TotalSeconds); 
     } 
    } 
    public void Delete<T>(T item) where T : class, new() 
    { 
     //WorkAround for DeleteOne parameter 
     ObjectFilterDefinition<T> filter = new ObjectFilterDefinition<T>(item); 
     // Remove the object. 
     _db.GetCollection<T>(typeof(T).Name).FindOneAndDelete(filter); 
    } 
} 
関連する問題