mongodb-.net-driver

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

那年仲夏 提交于 2019-11-30 00:30:50
问题 Is there any way in C# to check if a collection with a specific name already exists in my MongoDB database? 回答1: You can do it like this: database.GetCollection("blah").Exists() 回答2: @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

C# mongodb driver 2.0 - How to upsert in a bulk operation?

白昼怎懂夜的黑 提交于 2019-11-29 18:52:54
问题 I migrated from 1.9 to 2.2 and reading the documentation I was surprised to discover that is not possible to upsert during a bulk operation anymore, since operations don't allow options. bulkOps.Add(new UpdateOneModel<BsonDocument>(filter, update)); collection.BulkWrite(bulkOps); Should be options.isUpsert = true; bulkOps.Add(new UpdateOneModel<BsonDocument>(filter, update, options)); collection.BulkWrite(bulkOps); Is this work in progress, intended, or I'm missing something? Thank you. 回答1:

MongoDB how to check for existence

穿精又带淫゛_ 提交于 2019-11-29 16:55:26
问题 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! 回答1: Use $count operator to avoid memory issues, it not loading documents from database into memory: int count = items.FindAs<LedgerDocument>(Query.EQ("name",

Mongo C# JSON reader was expecting a value but found 'replSetGetStatus'

萝らか妹 提交于 2019-11-29 16:25:15
I was unable to find the proper way to call shell command from Mongo C# driver version 2.7.2 public async Task RsStatus() { var res = await _admin.RunCommandAsync<object>("replSetGetStatus"); } Gives me an the error : JSON reader was expecting a value but found 'replSetGetStatus' I'm guessing this simply not the way to call shell methods. Can any one supply me with an example ? Thanks in advance. db.adminCommand function expects and object to be passed as a parameter ( here ) so you can take advantage of BsonDocumentCommand generic type and also get a result as a BsonDocument, try: var command

Unable to connect to MongoDB (MongoLabs) via C# client

混江龙づ霸主 提交于 2019-11-29 13:58:56
Question Background: I have setup in MongoLabs (mLab - https://mlab.com/ ) a database and have added a very simple Collection. I am using the MongoDB driver to attempt to connect and work with this collection via the C# 3.2 driver. The Issue: I am unable to connect to my database via the C# driver with a constant authentication expection, as shown: System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = [] } }, LatencyLimitingServerSelector{

Unknown discriminator value 'MyEvent'

心不动则不痛 提交于 2019-11-29 13:51:19
Using the MongoDB persistance engine in joliver/EventStore causing the error Unknown discriminator value 'MyEvent' . The issue is only caused when I try to load all events for replaying the events like this.storeEvent.Advanced.GetFrom(new DateTime(2010, 1,1)) The issues is caused in ExtensionsMethods.cs public class MyClassEvent : IDomainEvent { ... } public static Commit ToCommit(this BsonDocument doc, IDocumentSerializer serializer) { if (doc == null) return null; var id = doc["_id"].AsBsonDocument; var streamId = id["StreamId"].AsGuid; var commitSequence = id["CommitSequence"].AsInt32; var

C# mongodb - how to update nested array elements

落爺英雄遲暮 提交于 2019-11-29 12:37:55
I have the following JSON structure that represents an item { Id: "a", Array1: [{ Id: "b", Array2: [{ Id: "c", Array3: [ {...} ] }] }] } I need to be able to either replace the array element in Array2 with a new item or to replace just Array3 with a new array. Here is my code to replace the array item in Array2 : await Collection.UpdateOneAsync( item => item.Id.Equals("a") && item.Array1.Any(a => a.Id.Equals("b")) && item.Array1[-1].Array2.Any(b => b.Id.Equals("c")), Builders<Item>.Update.Set(s => s.Array1[-1].Array2[-1], newArray2Item) ); When executing this code I'm getting this error: "A

MongoDB: A timeout occured after 30000ms selecting a server using CompositeServerSelector

懵懂的女人 提交于 2019-11-29 12:01:27
I'm completely stumped. I am using the latest c# drivers (2.3.0.157) and the latest MongoDB (3.2). The DB is running as a standalone setup with no replication or sharding. I've tried running locally on Windows as well as remotely on Amazon LINUX. I continue to get a timeout error but mysteriously sometimes it just works (maybe once every 20 - 30 attempts). I am creating the connection as such: private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ToString(); private static readonly string DataBase = ConfigurationManager.ConnectionStrings[

How to get the Mongo database specified in connection string in C#

冷暖自知 提交于 2019-11-29 10:31:21
问题 I would like to connect to the database specified in the connection string, without specifying it again in GetDatabase . For example, if I have a connection string like this; mongodb://localhost/mydb I would like to be able to db.GetCollection("mycollection") from mydb . This would allow the database name to be configured easily in the app.config file. 回答1: Update: MongoServer.Create is obsolete now (thanks to @aknuds1). Instead this use following code: var _server = new MongoClient

Deserialising polymorphic types with MongoDB C# Driver

♀尐吖头ヾ 提交于 2019-11-29 07:50:43
问题 Assume, I have a base class public class Node{ public ObjectId Id; public String nodeName; public ObjectId parentNode; } and 2 derived classes public class PlotNode:Node{ public ObjectId Id; public String plotDetail; } public class EndNode:Node{ public ObjectId Id; public int resultCode; } Several objects of all 3 classes are serialized are in database. And only data i have is a list of ObjectId's, and only thing known about these ids is that they are certain to be Node ids but it's not know