2014-06-18 94 views
2

我無法確定如何更新MongoDB集合中的條目。我看了一下C#驅動程序docs,我想我已經非常仔細地跟着他們了。更新MongoDB集合中的條目

但是,我傳遞給Update方法的一個參數(或者兩個?)是無效的。誰能告訴我我做錯了什麼?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using MongoDB.Bson; 
using MongoDB.Bson.Serialization.Attributes; 
using MongoDB.Driver; 
using MongoDB.Driver.Linq; 


namespace Csharp_Linq 
{ 
    class Program 
    { 
     public class Book 
     { 
      // Fields 
      public string title { get; set; } 
      public string author { get; set; } 
      public ObjectId id { get; set; } 

      // Constructors 
      public Book() 
      { 
       this.title = "some title"; 
       this.author = "some author"; 
      } 

      public Book(string title, string author) 
      { 
       this.title = title; 
       this.author = author; 
      } 

     } 

     static void Main(string[] args) 
     { 
      // Connect to the server 
      string connectionString = "mongodb://localhost"; 
      MongoClient client = new MongoClient(connectionString); 
      MongoServer server = client.GetServer(); 

      // Get the database then the collection 
      MongoDatabase database = server.GetDatabase("tutorial"); 
      MongoCollection collection = database.GetCollection("books"); 

      // Query the collection 
      int count = 
       (from book in collection.AsQueryable<Book>() 
       select book) 
       .Count(); 

      string numBooks = String.Format("This collection has {0} books.", count); 
      Console.WriteLine(numBooks); 

      var query = 
       from book in collection.AsQueryable<Book>() 
       where book.author == "Ernest Hemingway" 
       select book; 

      foreach (var book in query) 
      { 
       string bookInfo = String.Format("{0} by {1}", book.title, book.author); 
       Console.WriteLine(bookInfo); 
      } 

      // Insert new books 
      Book scaryBook = new Book("Dr. Sleep", "Stephen King"); 
      Book[] batch = 
      { 
       new Book(), 
       scaryBook 
      }; 
      collection.InsertBatch(batch); 

      // Update default book 
      var query2 = 
       from book in collection.AsQueryable<Book>() 
       where book.title == "some title" && book.author == "some author" 
       select book; 

      var update = new UpdateDocument { 
       { "$set", new BsonDocument("title", "War and Peace") } 
      }; 
      BsonDocument updatedBook = collection.Update(query2, update); 

      Console.ReadLine(); 
     } 
    } 
} 

我有點驚訝,Update方法實際上返回一個BsonDocument。它爲什麼這樣做?

早些時候,我試圖用更新對象作爲示例所示:

MongoCollection<BsonDocument> books; 
var query = Query.And(
    Query.EQ("author", "Kurt Vonnegut"), 
    Query.EQ("title", "Cats Craddle") 
); 
var update = Update.Set("title", "Cat's Cradle"); 
BsonDocument updatedBook = books.Update(query, update); 

這是否對象還存在嗎?每當我將它輸入到Visual Studio中時,我都會收到一個錯誤,提示該對象不在名稱空間中。

回答

0

Update對象是MongoDB.Driver.Builders命名空間的一部分,雖然這在本教程中並不明顯。

認爲問題出在您的查詢上。除非你有書

book.title == "some title" && book.author == "some author" 

你不會找到任何更新。

如果將此代碼添加到代碼文件中,您將成爲第二個示例(一次)。

using MongoDB.Driver.Builders; 
2

首先,我必須包括

using MongoDB.Driver.Builders; 

然後,我有我的Linq查詢轉換爲蒙戈查詢。這裏是由此產生的代碼:

 // Update default book 
     var query2 = 
      from book in collection.AsQueryable<Book>() 
      where book.title == "some title" && book.author == "some author" 
      select book; 
     // Cast linq query to Mongo query 
     var mongoQuery = ((MongoQueryable<Book>)query2).GetMongoQuery(); 

     var update = new UpdateDocument { 
      { "$set", new BsonDocument("title", "War and Peace") } 
     }; 
     collection.Update(mongoQuery, update); 

花了一點研究,讓所有的作品適合!

+0

CASTIN LINQ查詢到蒙戈幫助了我很多。謝謝。 – miksiii

0

您可以通過以下方式更新條目。以項目的情況下添加到購物車

db.carts.update({ 
_id: "the_users_session_id", status:'active' 
}, { 
$set: { modified_on: ISODate() }, 
$push: { 
    products: { 
    sku: "111445GB3", quantity: 1, title: "Simsong One mobile phone", price:1000 
    } 
} 
}); 

請參考下面的更多信息http://advancedmongodb.blogspot.in/