mongodb-csharp-2.0

Get All 'documents' from MongoDB 'collection'

ε祈祈猫儿з 提交于 2019-12-03 10:47:37
问题 I need to retrieve all the documents that are in my collection in MongoDB, but I cannot figure out how. I have declared my 'collection' like this- private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project"); And I followed what is explained in this MongoDB tutorial. I adjusted it for my needs, like- var documents = await SpeCollection.Find(new Project()).ToListAsync(); However, I keep having the following error- MongoDB.Driver.IMongoCollection does

Get All 'documents' from MongoDB 'collection'

帅比萌擦擦* 提交于 2019-12-03 02:19:19
I need to retrieve all the documents that are in my collection in MongoDB, but I cannot figure out how. I have declared my 'collection' like this- private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project"); And I followed what is explained in this MongoDB tutorial. I adjusted it for my needs, like- var documents = await SpeCollection.Find(new Project()).ToListAsync(); However, I keep having the following error- MongoDB.Driver.IMongoCollection does not have a definition for 'Find' and the best override of the extension method [superlong stuff]. Find

Store Json string as MongoDB array in C#

随声附和 提交于 2019-12-02 20:25:49
问题 I want to store a JSON string in an array format in MongoDB.This is my C# code for storing notification into MongoDB, here the field "Event_meta" field sending as a JSON string format to MongoDB. public async Task Handle(TaskPropertyUpdatedEvent eventmsg) { try { var meta = JsonConvert.SerializeObject(eventmsg.Event_meta); var notificationEvent = new Notifications() { Content = eventmsg.Content, Type = "TaskEvent", UserId = eventmsg.UpdatedById, EntityId = eventmsg.TaskId.ToString(),

Store Json string as MongoDB array in C#

只愿长相守 提交于 2019-12-02 09:15:15
I want to store a JSON string in an array format in MongoDB.This is my C# code for storing notification into MongoDB, here the field "Event_meta" field sending as a JSON string format to MongoDB. public async Task Handle(TaskPropertyUpdatedEvent eventmsg) { try { var meta = JsonConvert.SerializeObject(eventmsg.Event_meta); var notificationEvent = new Notifications() { Content = eventmsg.Content, Type = "TaskEvent", UserId = eventmsg.UpdatedById, EntityId = eventmsg.TaskId.ToString(), AddedDate = eventmsg.UpdatedDate, Active = true, ShowNotification = false, InternalEvent = true, UserName =

MongoDB Driver 2.0 C# is there a way to find out if the server is down? In the new driver how do we run the Ping command?

好久不见. 提交于 2019-12-01 17:41:20
问题 How do you call the Ping command with the new C# driver 2.0? In the old driver it was available via Server.Ping() ? Also, Is there a way to find out if the server is running/responding without running the actual query? Using mongoClient.Cluster.Description.State doesn't help because it still gave the disconnected state even after the mongo server started responding. 回答1: You can check the cluster's status using its Description property: var state = _client.Cluster.Description.State If you

MongoDB Driver 2.0 C# is there a way to find out if the server is down? In the new driver how do we run the Ping command?

元气小坏坏 提交于 2019-12-01 17:27:42
How do you call the Ping command with the new C# driver 2.0? In the old driver it was available via Server.Ping() ? Also, Is there a way to find out if the server is running/responding without running the actual query? Using mongoClient.Cluster.Description.State doesn't help because it still gave the disconnected state even after the mongo server started responding. You can check the cluster's status using its Description property: var state = _client.Cluster.Description.State If you want a specific server out of that cluster you can use the Servers property: var state = _client.Cluster

MongoDb C# Typed Aggregations with Group Unwind and Project

走远了吗. 提交于 2019-12-01 08:38:39
I have a collection like this: [{ "_id": 1, "OtherProperties": 100 "PersonInventory": [{ "FirstName": "Joe", "MiddleName": "Bob", "LastName": "Blogs", "PersonId": 1 }] },{ "_id": 2, "OtherProperties": 1005 "PersonInventory": [{ "FirstName": "Joe", "MiddleName": "Bob", "LastName": "Blogs", "PersonId": 1 }] }] And I am trying to select all the unique persons in the root docs using here newer type inference mongodb c# driver syntax. Tried this so far but getting errors saying the method is not available (i think this is to do with the grouping), can anyone show me where I am going wrong?

Getting a single object from mongodb in C#

久未见 提交于 2019-12-01 07:33:04
I've picked up a piece of code that is using the MongoDB driver like this to get a single object from a collection...this can't be right, can it? Is there a better way of getting this? IMongoCollection<ApplicationUser> userCollection; .... userCollection.FindAsync(x => x.Id == inputId).Result.ToListAsync().Result.Single(); Yes, there is. First of all don't use FindAsync , use Find instead. On the IFindFluent result use the SingleAsync extension method and await the returned task inside an async method: async Task MainAsync() { IMongoCollection<ApplicationUser> userCollection = ...; var

Getting a single object from mongodb in C#

不想你离开。 提交于 2019-12-01 03:38:23
问题 I've picked up a piece of code that is using the MongoDB driver like this to get a single object from a collection...this can't be right, can it? Is there a better way of getting this? IMongoCollection<ApplicationUser> userCollection; .... userCollection.FindAsync(x => x.Id == inputId).Result.ToListAsync().Result.Single(); 回答1: Yes, there is. First of all don't use FindAsync , use Find instead. On the IFindFluent result use the SingleAsync extension method and await the returned task inside

Mongo C# driver 2.0 - Find count without fetching documents

我只是一个虾纸丫 提交于 2019-11-30 23:34:44
A general count query will be doing a int count = collection.Find(filter).Count(); Now that loads all the records as per the filter, so lets says I have 1 million records and out of those 0.5 million match my filter, so I'll have collection already filled with 0.5 documents. This is good enough if you want the documents, but what if you just want to know the count and not really need the documents, for memory sake. Can I do something like this int count = collection.Find(filter).SetLimit(1).Count(); This gives me the same count as the first expression, but I hope that the memory will not