Tag: mongodb

如何将BsonDocument对象反序列化回类

从服务器获取后,如何将BsonDocument对象反序列化回类? QueryDocument _document = new QueryDocument(“key”, “value”); MongoCursor _documentsReturned = _collection.FindAs(_document); foreach (BsonDocument _document1 in _documentsReturned) { //deserialize _document1 //? } 我是否使用BsonReader进行反序列化?

c#MongoDB驱动程序:FindOneAndUpdate如何知道它是否找到了文件?

因此,如果对象存在,我正在使用MongoDB驱动程序更新数据库中的对象字段值。 IMongoDatabase db = _mongoClient.GetDatabase(DataBase); IMongoCollection collection = db.GetCollection(str_collection); collection.FindOneAndUpdate( e => e._id == eventoId && e._visitantes.Any(v => v._empresa == empresa && v._nombre == nombre && v._apellidos == apellidos), Builders.Update.Set(e => e._visitantes[-1]._asistido, true)); 我的问题是:我怎么知道对象在数据库中找到了? 我看过文档,但没有找到任何内容。 如果它不存在,我不想创建一个新对象,只是我想知道一个对象是否已发现更改该值。 谢谢。

如何在MongoDB C#Driver 2.0中记录我的查询?

刚刚将我的应用程序升级到最新的稳定MongoDB C#Driver 2.0。 在迁移过程中,基本function已被破坏,即使是最简单的查询,例如: this.collection.Find(e => e.Id == id).SingleOrDefaultAsync()也不会返回正确的数据。 检查了类映射和约定,但我希望看到输出查询以正确识别问题。 那么,应该如何在MongoClient端完成? 在数据库级别设置分析是可能的,但不是一个好的解决方案,因为我们有几个应用程序和开发人员使用数据库。 我的应用程序目前在UI,业务和EF数据访问中使用Ninject.Extensions.Logging和log4net 。

MongoDB和C#Find()

我有以下代码,我是mongodb的新手,我需要帮助找到集合中的特定元素。 using MongoDB.Bson; using MongoDB.Driver; namespace mongo_console { public class User { public ObjectId Id { get; set; } public string name { get; set; } public string pwd { get; set; } } class Program { static void Main(string[] args) { MongoClient client = new MongoClient(); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(“Users”); […]

MongoDB + C#驱动程序+查询元素数组,其中每个数组元素包含要查询的子文档

我正在使用官方MongoDB C#驱动程序的1.5.0.4566版。 我正在使用Mongo 2.06版。 这是我的文档结构的样子(省略了本讨论不需要的字段): { “Parents” : [ { “CreatedOn”: ISODate(“2012-07-28T15:30:06.623Z”), “Title”: “Simple Title”, “Children”: [ { “StatusId”: 1, “Active” : true, SubChild : { “ExpiresOn”: ISODate(“2012-07-28T15:30:06.623Z”)}}, { “StatusId”: 1, “Active” : true, SubChild : { “ExpiresOn”: ISODate(“2012-08-28T15:30:06.623Z”)}} ] }, { “CreatedOn”: ISODate(“2012-07-28T15:30:06.623Z”), “Title”: “Another Simple Title”, “Children”: [ { “StatusId”: 1, “Active” : […]

如何使用c#2.0驱动程序将数据插入mongodb集合?

我在我的c#控制台应用程序中使用MongoClient连接到MongoDB https://github.com/mongodb/mongo-csharp-driver/releases/tag/v2.0.0-rc0 我的代码 class Program { static void Main(string[] args) { const string connectionString = “mongodb://localhost:27017”; // Create a MongoClient object by using the connection string var client = new MongoClient(connectionString); //Use the MongoClient to access the server var database = client.GetDatabase(“test”); var collection = database.GetCollection(“entities”); var entity = new Entity { Name = “Tom” […]

MongoDB .NET在upsert上没有生成_id

我正在尝试使用.NET驱动程序将文档插入MongoDB 2.4.4。 似乎不会在upserts上自动生成_id ,尽管它在普通插入上正确生成了_id 。 如何让驱动程序正确生成_id ? 这是一个展示问题的小例子。 public class MongoObject { [BsonId(IdGenerator = typeof(StringObjectIdGenerator))] [BsonRepresentation(BsonType.ObjectId)] public string MongoID { get; set; } public int Index { get; set; } } var obj = new MongoObject() { Index = 1 }; //This inserts the document, but with a _id set to Null _collection.Update(Query.EQ(“Index”, BsonValue.Create(1)), Update.Replace(obj), […]

我如何与C#和MongoDB’和’多个$ elemMatch子句?

我使用10Gen认可的c#驱动程序用于mongoDB用于ac#应用程序和数据浏览我正在使用Mongovue。 以下是两个示例文档架构: { “_id”: { “$oid”: “4ded270ab29e220de8935c7b” }, “Relationships”: [ { “RelationshipType”: “Person”, “Attributes”: { “FirstName”: “Travis”, “LastName”: “Stafford” } }, { “RelationshipType”: “Student”, “Attributes”: { “GradMonth”: “”, “GradYear”: “”, “Institution”: “Test1”, } }, { “RelationshipType”: “Staff”, “Attributes”: { “Department”: “LIS”, “OfficeNumber”: “12”, “Institution”: “Test2”, } } ] }, { “_id”: { “$oid”: “747ecc1dc1a79abf6f37fe8a” }, […]

如何使用MongoDB的官方C#驱动程序删除’ID’中的’文档’?

有人可以告诉我,如果有更好的方法从使用官方C#驱动程序删除MongoDB中的一个document ,而不是我在下面 – var query = Query.EQ(“_id”, a.Id); database.GetCollection(“Animal”).Remove(query); 这段代码有效,但对我来说似乎太过分了 。 例如,“保存”命令需要一个实例并对其进行更新。 我想要类似的东西 – Remove(item) 。 备注:我正在尝试使用C#的官方驱动程序,而不是看起来过时的NoRM或Samus 。

带有C#驱动程序2.0的MongoDB(服务器v 2.6.7):如何从InsertOneAsync获取结果

我正在使用C#驱动程序2.0测试MongoDB(服务器v 2.6.7)。 当我使用插入函数InsertOneAsync存在一个存在_id的文档时,我期待一个像你从Mongo shell获得的错误: WriteResult({ “nInserted” : 0, “writeError” : { “code” : 11000, “errmsg” : “insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.Commands.$_id_ dup key: { : 0.0 }” }}) 但问题是带有C#驱动程序的插入不会抛出exception,我找不到插入的WriteResult 。 当我查看数据库时,似乎什么都没发生。 所以我的问题是插入现有的_id时会对InsertOneAsync什么期望? Visual Studio中的代码: IMongoCollection commandsCollection = db.GetCollection(“Commands”); var bson = new BsonDocument { {“_id”, i.Value}, {“label”, i.Key} […]