mongodb-.net-driver

MongoDB custom serializer implementation

筅森魡賤 提交于 2019-11-30 19:24:41
I am new to MongoDB, and am trying to get the C# driver to work serializing F# classes. I have it working with the class automapper using mutable F# fields & a parameterless constructor, but really I need to retain immutability, so I started looking at implementing an IBsonSerializer to perform custom serialization. I haven't found any documentation for writing one of these so have just tried to infer from the driver source code. I have run into a problem whereby when the Deserialize method is called on the serializer, the CurrentBsonType is set to EndOfDocument rather than the start as I am

Mapping a private backing field with MongoDB C#

大兔子大兔子 提交于 2019-11-30 18:11:04
问题 I'm trying to get a private backing field mapped in MongoDB. My model looks like: public class Competitor { private IList<CompetitorBest> _competitorBests; public virtual int CompetitorId { get; set; } public virtual string Name { get { if (Type == "Team") return TeamName; return FirstName + " " + LastName; } } public virtual IEnumerable<CompetitorBest> CompetitorBests { get { return _competitorBests.ToArray(); } } } I'm basically trying to map _competitorBests, to be CompetitorBests (which

BsonValue and custom classes in MongoDB C# Driver

﹥>﹥吖頭↗ 提交于 2019-11-30 17:39:47
I'm trying to use $push in an update query in mongodb, with the c# driver. The Update.Push(...) method requires a string name (that's fine), and a BsonValue to be 'pushed'. This is where I run into problems. I'm trying to push a non simple type to the field's array. For example: { $push : { "arrayfield" : { "a" : 7, "b" : 12 } } } This works fine in the Mongo console, but I can't figure out how to make my object into a BsonValue. I've tried BsonValue.Create(myObjectInstance) but that gives me an error saying the .NET type cannot be mapped to a BsonValue. Am I missing something simple? Andrew

How to check if collection exists in MongoDB using C# driver?

為{幸葍}努か 提交于 2019-11-30 17:13:13
Is there any way in C# to check if a collection with a specific name already exists in my MongoDB database? im1dermike You can do it like this: database.GetCollection("blah").Exists() @im1dermike answer is no longer working for c# driver version 2.0+ Here is an alternative: public async Task<bool> CollectionExistsAsync(string collectionName) { var filter = new BsonDocument("name", collectionName); //filter by collection name var collections = await GetDatabase().ListCollectionsAsync(new ListCollectionsOptions { Filter = filter }); //check for existence return await collections.AnyAsync(); }

MongoDB: Getting the list of all databases?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 12:35:44
How do I list all databases for a connection using Mongo C# Driver? Very easily: var server = MongoServer.Create("mongodb://localhost/?safe=true"); var databaseNames = server.GetDatabaseNames(); The MongoServer class was deprecated in version 2.0.0. You can use ListDatabasesAsync using (var cursor = await client.ListDatabasesAsync()) { await cursor.ForEachAsync(d => Console.WriteLine(d.ToString())); } Working Solution: MongoClient client = new MongoClient("mongodb://localhost:27017"); using (IAsyncCursor<BsonDocument> cursor = client.ListDatabases()) { while (cursor.MoveNext()) { foreach (var

How to use $push update modifier in MongoDB and C#, when updating an array in a document

拜拜、爱过 提交于 2019-11-30 11:41:32
I've run the following code in mongo shell: db.unicorns.insert({name: 'Dunx', loves: ['grape', 'watermelon']}); and now I've something like this in my MongoDB collection: {name: 'Dunx', loves: ['grape', 'watermelon']} As you can see loves is an array. Question How can I write C# code, with the official C# driver, that does the following: db.unicorns.update({name: 'Aurora'}, {$push: {loves: 'sugar'}}) The above code runs just fine in mongo shell. it should be something like this: unicorns.Update(Query.EQ("name", "Aurora"), Update.Push("loves", "sugar")); I would like to also illustrate how to

MongoDB how to check for existence

半城伤御伤魂 提交于 2019-11-30 11:23:54
I would like to know how can I check the existence of an object with mongoDB and C#. I've found a way to do it but I had to use Linq thanks to Any() method, but I'd like to know if it's possible to do it without Linq ? database.GetCollection<ApplicationViewModel>("Applications").Find(Query.EQ("Name", applicationName)).Any() Thanks guys! Use $count operator to avoid memory issues, it not loading documents from database into memory: int count = items.FindAs<LedgerDocument>(Query.EQ("name", appName)).Count(); if(count > 0) { //then doc exists } Operator $exists in mongodb can be used to identfy

C# driver for MongoDb: how to use limit+count?

。_饼干妹妹 提交于 2019-11-30 09:52:16
From MongoDb documentation: " On a query using skip() and limit(), count ignores these parameters by default. Use count(true) to have it consider the skip and limit values in the calculation. " That's exactly what I need to count resulted elements for the specific query until it's over defined limit like 1000, but I do not see any way to do it in c# driver. Count of IMongoCollection and SetCount of IMongoCursor are both parameter-less. Any idea? Use the Size method instead of Count , as that honors Skip and Limit. Console.WriteLine(collection.Find(query).SetSkip(0).SetLimit(1).Size()); Looks

How do I get the date a MongoDB collection was created using MongoDB C# driver?

社会主义新天地 提交于 2019-11-30 09:19:04
问题 I need to iterate through all of the collections in my MongoDB database and get the time when each of the collections was created (I understand that I could get the timestamp of each object in the collection, but I would rather not go that route if a simpler/faster method exists). This should give you an idea of what I'm trying to do: MongoDatabase _database; // code elided var result = _database.GetAllCollectionNames().Select(collectionName => { _database.GetCollection( collectionName ) //.

How can I 'AND' multiple $elemMatch clauses with C# and MongoDB?

余生长醉 提交于 2019-11-30 08:34:54
问题 I am using the 10Gen sanctioned c# driver for mongoDB for a c# application and for data browsing I am using Mongovue. Here are two sample document schemas: { "_id": { "$oid": "4ded270ab29e220de8935c7b" }, "Relationships": [ { "RelationshipType": "Person", "Attributes": { "FirstName": "Travis", "LastName": "Stafford" } }, { "RelationshipType": "Student", "Attributes": { "GradMonth": "", "GradYear": "", "Institution": "Test1", } }, { "RelationshipType": "Staff", "Attributes": { "Department":