C# MongoDB multiple similar records -
i have started using mongodb , have question... how do following:
var testrec = new testclass { name = "john", address = "10 here st", recordtype = "a" }; db.save(testrec); testrec.recordtype = "b"; db.save(testrec);
i want second save save new document there should 2 documents same details except recordtype.
what seems happen overrides first document second.
can please let me know.
thanks dean
you need new id , insert instead of updating. here complete example:
using system; using mongodb.bson; using mongodb.driver; namespace mongotest { class testclass{ public string name; public string address; public string recordtype; public objectid id; } class mainclass { public static void main (string[] args) { var connectionstring = "mongodb://localhost"; var client = new mongoclient(connectionstring); var server = client.getserver(); var db = server.getdatabase("test"); var collection = db.getcollection("test"); var testrec = new testclass { name = "john", address = "10 here st", recordtype = "a", id = new objectid() }; collection.save(testrec); testrec.id = new objectid(); testrec.recordtype = "b"; collection.save(testrec); testrec.id = new objectid(); testrec.recordtype = "c"; collection.save(testrec); } } }
Comments
Post a Comment