mongodb-.net-driver

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

ASP.NET 5 with MongoDB

≯℡__Kan透↙ 提交于 2019-12-01 03:23:22
Trying to get an ASP.NET 5 website integrated with the MongoDB C# driver but running into a few issues. First of all, the examples listed here http://docs.mongodb.org/ecosystem/drivers/csharp/ are all flagged as obsolete. Secondly, I'm getting really weird compile errors (type or namespace could not be found) when I try and build even though everything looks OK in the IDE. Here's my very basic HomeController.cs using Microsoft.Framework.DependencyInjection; using Microsoft.AspNet.Mvc; using MongoDB.Driver; using System; namespace Docker.Web.Controllers { public class HomeController :

Strange behavior of MongoDB LINQ provider for fields called “id”

99封情书 提交于 2019-12-01 01:54:30
问题 Here's a JSON document where Mongo LINQ provider fails: {"results": {"text":"@twitterapi http://tinyurl.com/ctrefg", "to_user_id":396524, "to_user":"TwitterAPI", "from_user":"jkoum", "metadata": { "result_type":"popular", "recent_retweets": 109 }, "id":1478555574, "from_user_id":1833773, "iso_language_code":"nl", "source":"<a href=\"http://twitter.com/\">twitter<\/a>", "profile_image_url":"http://s3.amazonaws.com/twitter_production/profile_images/118412707/2522215727_a5f07da155_b_normal.jpg",

MongoDB .NET Driver Group By Time Range

佐手、 提交于 2019-12-01 01:14:48
I am a noob in MongoDB and wanted to know how to count total documents inserted into the collection for every 15 minutes interval starting 12 AM UTC until the current UTC time. Below is a sample document { "_id" : ObjectId("5ade8bfc6b941c7726a54f01"), "Country" : "US" "Timestamp" : ISODate("2018-04-24T01:44:28.040Z"), } Here is the expected output: { "Count": 245, "ReceiveDateString": "5/2/2018 12:00:00 AM" }, { "Count": 239, "ReceiveDateString": "5/2/2018 12:15:00 AM" }, { "Count": 252, "ReceiveDateString": "5/2/2018 12:30:00 AM" }, { "Count": 255, "ReceiveDateString": "5/2/2018 12:45:00 AM"

C# Mongo FirstOrDefaultAsync hangs

*爱你&永不变心* 提交于 2019-11-30 23:59:37
using the 2.0 driver the following code will sometimes hang and never return. public async Task<T> GetFirst(FilterDefinition<T> query) { return await GetCollection.Find(query).FirstOrDefaultAsync(); } if I debut and put a break point on the return line, everything returns normally. In the shell the query being run is something like this: db.Customers.find({"Name" : /test$/i}) There are 2 solutions: Add a ConfigureAwait(false) at the end: return await GetCollection.Find(query).FirstOrDefaultAsync().ConfigureAwait(false); Just return the Task<T> , since the result of FirstOrDefaultAsync() is the

ASP.NET 5 with MongoDB

最后都变了- 提交于 2019-11-30 23:41:36
问题 Trying to get an ASP.NET 5 website integrated with the MongoDB C# driver but running into a few issues. First of all, the examples listed here http://docs.mongodb.org/ecosystem/drivers/csharp/ are all flagged as obsolete. Secondly, I'm getting really weird compile errors (type or namespace could not be found) when I try and build even though everything looks OK in the IDE. Here's my very basic HomeController.cs using Microsoft.Framework.DependencyInjection; using Microsoft.AspNet.Mvc; using

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

Mapping a private backing field with MongoDB C#

谁说我不能喝 提交于 2019-11-30 22:46:26
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 exists in my document in mongo) Note: This model is shared by NHibernate (hence the virtual ) I can't

How do you increment a field in mongodb using c#

眉间皱痕 提交于 2019-11-30 20:46:39
Thought this would be pretty straight forward, but my value is remaining the same (0). What I'd like to do is increment my UnreadMessages field when the user receives a message they haven't read and then decrement it when they have. So I thought code like this would work: var userHelper = new MongoHelper<User>(); //increment userHelper.Collection.Update(Query.EQ("Id", userId.ToId()), Update.Inc("UnreadMessages", 1)); //decrement userHelper.Collection.Update(Query.EQ("Id", userId.ToId()), Update.Inc("UnreadMessages", -1)); After running these no errors are thrown but the value doesn't change

Insert element into nested array in Mongodb

淺唱寂寞╮ 提交于 2019-11-30 19:46:13
I have this : { "_id" : ObjectId("4fb4fd04b748611ca8da0d48"), "Name" : "Categories", "categories" : [{ "_id" : ObjectId("4fb4fd04b748611ca8da0d46"), "name" : "SubCategory", "sub-categories" : [{ "_id" : ObjectId("4fb4fd04b748611ca8da0d47"), "name" : "SubSubCategory", "standards" : [] }] }] } I would like to add a new SubCategory using the C# driver. Is there an optimal way to do this? You can do this using FindOneAndUpdateAsync and positional operator public async Task Add(string productId, string categoryId, SubCategory newSubCategory) { var filter = Builders<Product>.Filter.And( Builders