mongodb-.net-driver

mongodb c# select specific field

北慕城南 提交于 2019-12-22 12:27:15
问题 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

Storing System.Type with MongoDb

馋奶兔 提交于 2019-12-22 11:26:21
问题 When I store this class: class MyClass{ ... public Type SomeType {get;set;} ... } SomeType property gets serialized like this: "SomeType" : { "_t" : "RuntimeType" } and every subsequent query fails. I'm using the official C# driver. How do I get it to store the actual Type? Thanks. 回答1: Here's a sample serializer for System.Type that serializes the name of the Type as a BSON string. This has some limitations in that the Deserialize method fails if the type name is not a system type or in the

Is there a way to create or update a MongoDB index?

人盡茶涼 提交于 2019-12-22 10:28:51
问题 According to the documentation on the createIndexes command: If you create an index with one set of options and then issue createIndexes with the same index fields but different options, MongoDB will not change the options nor rebuild the index. The solution is to drop the index and create it from scratch, but that's costly. Is there a way to create an index when there isn't one, do nothing if there is an index with the same options, and replace the index if the options have changed? This

MongoDB : update entire document except _id using C# driver

给你一囗甜甜゛ 提交于 2019-12-22 09:08:16
问题 I have to update all the fields except _id. I want to avoid to manually update the 16 fields... All the new fields are stored inside a BsonDocument Thanks for ideas 回答1: As @Philipp hinted there is a way way to do this. You can actually use the save function ( http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Save%3CTDocument%3Emethod ) which will do what he says for you in the database end. So imagine you have a document of: { _id: {}, d: 1 } And that _id

Transition from Fluent Mongo to Mongo C# 1.4 Driver

老子叫甜甜 提交于 2019-12-22 08:32:16
问题 We are using FluentMongo and now that LINQ support has been added into the C# driver, we are going to remove the dependency on Fluent and go with the official C# driver alone. Has anyone done this already, and was it simple and straightforward? Is there anything we need to be looking out for? 回答1: Hopefully others will report back to you as well, but as the implementer of the LINQ support in the 1.4 C# driver I can tell you a bit about what to expect. Overall you can expect some features to

How do I set the serialization options for the geo values using the official 10gen C# driver?

安稳与你 提交于 2019-12-22 08:13:36
问题 Considering this class: public class Location { public Coordinates Geo { get; set; } public Location() { Geo = new Coordinates(); } public class Coordinates { public decimal Lat { get; set; } public decimal Long { get; set; } } } I have a geospatial index on the collection set like { Geo: "2d" } . Unfortunately the driver tries to store lat/lon coordinates as strings, instead of numbers and I get an error that says Tue Mar 15 16:29:22 [conn8] insert database.locations exception 13026 geo

save an object with a bidirectional relationship in mongodb using official c# driver

南笙酒味 提交于 2019-12-22 04:54:06
问题 I have two class like this: public Class Company { public IList<Employee> Employees; } public Class Employee { public Company WorkPlace; } when I want to save an object of class Company: MongoDatabase Database = MongoServer.GetDatabase("db"); var workPlace = new Company(); var employee = new Employee { WorkPalce = workPlace} workPlace.Employees = new List<Employee>{ employee }; Database.GetCollection<Company>("company").Save(workPlace); StackOverFlow Exception will be thrown. 回答1: This is

MongoDB and DateTimeOffset type

泄露秘密 提交于 2019-12-22 04:33:21
问题 I am trying to find all documents that are created within a specified time. I am using c# and the mongodb c# driver. My entity is as follows: public class Entity { public Gid Id { get; private set; } public DateTimeOffset CreationTimestamp { get; private set; } public Entity() { } } So I thought I could do this: DateTime compareTime = DateTime.UtcNow.AddMinutes(-15); var result = _collection.Find(Query.GT("CreationTimestamp", compareTime)).Count(); Result is a count of zero even though there

Return BsonDocument in ApiController

会有一股神秘感。 提交于 2019-12-22 04:00:53
问题 I'm working on an API in ASP.NET MVC 4, I'm using MongoDB as a backend. Due to MongoDB storing and returning BSON objects, and MVC4 returning JSON objects, I figured that it would be rather easy to simply return the BSON on the rest calls. This didn't work, so I found the .toJson() method on th BsonDocument class, converts the BSON object to a JSON-string representation. Unfortunately when I return this string through my ApiController , MVC apparently thinks it should re-serialize the string

Cant Mock my Get() function from my repository - MongoDB.Driver 2.2.3

社会主义新天地 提交于 2019-12-22 00:13:55
问题 In my unit test I want to test my method that I created for filtering data from MongoDB. When I try to mock my function like this: _repo.GetFluent<Location>((Arg.Any<Expression<Func<Location, bool>>>())) .Returns(x => locations.Where(x.Arg<Expression<Func<Location, bool>>>()).ToList()); It underlines the Returns saying: Cannot convert lambda expression. Before when I worked on my simple project using the 2.0.0 MongoDB driver I had no problem mocking my Get() function like this, but now with