Friday, 15 January 2010

c# - Updating an entry in a MongoDB collection -



c# - Updating an entry in a MongoDB collection -

i cannot figure how update entry in mongodb collection. have looked @ c# driver docs, , think have followed them pretty closely.

however, 1 (or maybe both?) of arguments passing update method invalid. can tell me doing wrong?

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 programme { 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 server string connectionstring = "mongodb://localhost"; mongoclient client = new mongoclient(connectionstring); mongoserver server = client.getserver(); // database collection mongodatabase database = server.getdatabase("tutorial"); mongocollection collection = database.getcollection("books"); // query 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 = book in collection.asqueryable<book>() book.author == "ernest hemingway" select book; foreach (var book in query) { string bookinfo = string.format("{0} {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 = book in collection.asqueryable<book>() book.title == "some title" && book.author == "some author" select book; var update = new updatedocument { { "$set", new bsondocument("title", "war , peace") } }; bsondocument updatedbook = collection.update(query2, update); console.readline(); } } }

i little surprised update method returns bsondocument. why that?

earlier tried utilize update object shown in example:

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);

does object still exist? whenever type visual studio, error saying object not in namespace.

first had include

using mongodb.driver.builders;

then had cast linq query mongo query. here resulting code:

// update default book var query2 = book in collection.asqueryable<book>() book.title == "some title" && book.author == "some author" select book; // cast linq query mongo query var mongoquery = ((mongoqueryable<book>)query2).getmongoquery(); var update = new updatedocument { { "$set", new bsondocument("title", "war , peace") } }; collection.update(mongoquery, update);

took little research of pieces fit!

c# mongodb

No comments:

Post a Comment