mongodb-.net-driver

Creating a mongodb capped collection using c# api

 ̄綄美尐妖づ 提交于 2019-12-06 23:25:05
问题 Using the C# MongoDB driver, we currently create our collection like so: MongoServer mongoServer = MongoServer.Create("some conn str"); MongoDatabase db = mongoServer.GetDatabase("mydb"); MongoCollection logs = db.GetCollection("mycoll"); I would like to use mycoll as a capped collection. I haven't seen any examples or documentation specifics on how to create a capped collection using the C# driver. I've found tons of JS examples, and even a Java example (here: Creating a mongodb capped

Filter only by Date using mongoDB c# driver

二次信任 提交于 2019-12-06 21:57:52
问题 I am using mongoDB c# latest driver i.e 3.+ in my project. I have different date filter criteria like Today,Last Day,Yesterday,This Month etc by using daterangepicker. Here is my model public class Student { public Student() { } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime CreatedOn { get; set; } [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime ModifiedOn { get; set; } public

How do I resolve a year/month/day date to a more specific date with time data in MongoDB?

不羁岁月 提交于 2019-12-06 15:22:42
问题 I'm working on converting my blog over to use /year/month/day type URLs. I've ran into a pretty bad problem with the concept though. I want to only have down to hour/minute resolution, but of course all of my entries are stored with a full time specific as possible(and I don't want to change that). How exactly do I go about resolving a specific date to a less specific date with MongoDB? I'm using 10gen's official MongoDB mapper for .Net. My data-model looks like: public class BlogEntryData {

MongoDB substring product search order by highest match

百般思念 提交于 2019-12-06 13:55:53
问题 I'm not so good in Mongodb. That's my first project using this. I'm working on some sort of shop website. We're using C# and newest C# driver for mongodb. I need an idea how to implement my algorithm to filter products based on user's input. Here's how my algorithm works/should work. User type something like "Blue adidas men spiderman cotton t-shirt" I split whole phrase into single words so I have "blue", "adidas", "men", "spiderman", "cotton", "t-shirt" I check each word against retailer,

Insert new document using InsertOneAsync (.NET Driver 2.0)

廉价感情. 提交于 2019-12-06 08:38:29
问题 In the older .Net API version : MongoClient client = new MongoClient(); var server = client.GetServer(); var db = server.GetDatabase("foo"); var collection = db.GetCollection<BsonDocument>("bar"); var document = new BsonDocument { { "_id", 1 }, { "x", 2 } }; collection.Save(document); It worked. When i use new .Net Driver 2.0 : var client = new MongoClient("mongodb://localhost:27017"); var database = client.GetDatabase("foo"); var collection = database.GetCollection<BsonDocument>("bar"); var

How to force mongo to store members in lowercase?

扶醉桌前 提交于 2019-12-06 08:09:33
I have a collection of BsonDocuments, for example: MongoCollection<BsonDocument> products; When I do inserts into the collection, I want the member name to always be lowercase. After reading the documentation, it appears that ConventionPack is the way to go. So, I've defined one like this: public class LowerCaseElementNameConvention : IMemberMapConvention { public void Apply(BsonMemberMap memberMap) { memberMap.SetElementName(memberMap.MemberName.ToLower()); } public string Name { get { throw new NotImplementedException(); } } } And right after I get my collection instance I register the

MongoDB 2.4's “Limit Number of Elements in an Array after an Update” using C# driver?

狂风中的少年 提交于 2019-12-06 07:28:47
MongoDB 2.4 added a new "Limit Number of Elements in an Array after an Update" feature . This is how it can be used through the shell: db.students.update( { _id: 1 }, { $push: { scores: { $each : [ { attempt: 3, score: 7 }, { attempt: 4, score: 4 } ], $sort: { score: 1 }, $slice: -3 } } } ) How can this be accomplished with the MongoDB's C#-driver? Here is an example test that shows how to do this without using typed classes: https://github.com/mongodb/mongo-csharp-driver/blob/master/MongoDB.DriverUnitTests/Builders/UpdateBuilderTests.cs#L492 The relevant piece of code you are looking for is

MongoDb Distinct with query C# driver

Deadly 提交于 2019-12-06 07:15:58
I am trying to use the db.collection.distinct(field, query) command, documented here . I am trying to call this with the C# driver, documented here . Currently I am using the code: _repository.Search(item.searchCriteria).Select(i => i.messageId).Distinct().ToList() where messageId is a string and the Search function does: //Create search accross all properties of type. public IQueryable<SearchType> Search(SearchType entity) { Type entityType = entity.GetType(); var propertiesToSearch = entityType.GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public); query =

MongoDB C# Driver doesn't release connections then errors

血红的双手。 提交于 2019-12-06 07:06:48
I'm using the latest versions of MongoDB (on a Win 64 Server) and the C# driver. I have a windows service that is doing 800 reads and updates per minute, and after a few minutes the current threads used goes above 200 and then every single mongodb call gives this error: System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed

mongodb c# select specific field

不问归期 提交于 2019-12-06 05:29:51
Need some help creating a generic method for selecting fields by their name. something like this: T GetDocField<T>(string doc_Id, string fieldName) The best I got is using projection which gives me the doc with only the wanted field seted: public T GetDocField<T>(string Doc_Id, string fieldName) { var value = DocCollection.Find(d => d.Id == Doc_Id) .Project<T>(Builders<Doc>.Projection .Include(new StringFieldDefinition<Doc> (fieldName))).FirstOrDefaultAsync().Result; note: I'm using the new c# driver (2.0) Thanks!! You can do next: public async Task<TValue> GetFieldValue<TEntity, TValue>