我能够使用以下代码成功插入新文档,但无法获取新插入文档的 _id 。
插入后,user 为空。谢谢你!
MongoServer server = MongoServer.Create();
MongoDatabase test = server.GetDatabase("Test");
MongoCollection<BsonDocument> users = test.GetCollection("Users");
BsonDocument user = new BsonDocument();
user.Add("FirstName", "John");
user.Add("LastName", "Michael");
user.Add("Address", "123 Main Street");
user.Add("City", "Newport Beach");
user.Add("State", "CA");
user.Add("ZipCode", "92660");
user.Add("Email", "[email protected] /cdn-cgi/l/email-protection");
user.Add("CreatedDate", DateTime.Now);
user.Add("IPAddress", "10.1.1.1");
user = users.Save(user);
string idSTring = user["_id"].ToString().Replace("\"", "");
我用官方驱动做了一些测试,发现方法MongoCollection.Save返回null;因此,不要将结果分配给您构建的用户:
//user = users.Save(user);
users.Save(user);
string idStr = user["_id"].ToString();
Console.WriteLine("_id == {0}", idStr);
关于驱动检查this http://deserialized.com/the-state-of-mongodb-and-csharp/ and this https://stackoverflow.com/questions/2735246/what-is-the-most-mature-mongodb-driver-for-c
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)