mongodb-.net-driver

Transition from Fluent Mongo to Mongo C# 1.4 Driver

痴心易碎 提交于 2019-12-05 13:29:30
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? 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 be missing and some new ones to be present. One difference is that the official C# driver only supports LINQ

Using SetFields with MongoDB C# driver 2.0

て烟熏妆下的殇ゞ 提交于 2019-12-05 11:00:31
With the old driver I could specify the fields I wanted to return from a query as follows: var cursor = Collection.Find(query). SetFields(Fields<MealPlan>.Exclude (plan => plan.Meals)); How do I accomplish this with the 2.0 driver? You need to use the Projection method on IFindFluent (which is what Find and Projection return): var findFluent = Collection.Find(query).Projection(Fields<MealPlan>.Exclude (plan => plan.Meals)) Now, this would eventually generate a cursor of BsonDocument s since it doesn't know how the projection looks. You can call the generic Projection instead to add that type:

Return BsonDocument in ApiController

北城余情 提交于 2019-12-05 04:03:20
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 as JSON, which the browser can't interpret. So I would like to ask if there is a way to disable the

C# : Retrieve array values from bson document

萝らか妹 提交于 2019-12-05 03:53:25
In my MongoDB collection, I have a document with an array entry. How do I get these array values as a string array in C#? I can get the document itself back fine but I can't seem to get the array values. This is where I'm up to : QueryDocument findUser = new QueryDocument("_id" , id); BsonDocument user = bsonCollection.FindOne(findUser); So in this user document, there is an array that I'd like to get and parse into a string array. The document looks something like this : { "firstname" : "jon", "secondname" : "smith", "loves" : ["this","that","other stuff"] } If I got your problem correctly,

Maintain Id property name in embedded doc with mongo C# driver

别等时光非礼了梦想. 提交于 2019-12-05 03:49:56
I have a mongo document that contains an array of embedded documents. The embedded documents have a property named "Id". { Name: "Outer object", Embedded: [ {Name: "Embedded A", Id: "5f1c591a71dc237199eeaeda"} ] } My C# mapping objects look something like this (a simplification, obviously) public class Outer { public string Name { get; set; } public IEnumerable<Inner> Inners { get; set; } } public class Inner { public string Name { get; set; } public string Id { get; set; } } When I write an outer to the database, the C# driver changes the name of the Inner.Id property to _id. How do I

MongoDB and DateTimeOffset type

北城以北 提交于 2019-12-05 03:22:41
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 is data in the collection. If I change from DateTimeOffset to DateTime I will get back a result. Is the

How to $lookup with MongoDB C# driver?

泪湿孤枕 提交于 2019-12-05 01:39:09
问题 How do I perform a $lookup with the MongoDB C# driver? I cannot find it in their driver doc here: https://docs.mongodb.org/getting-started/csharp/query/ But if I understand this ticket in their JIRA correctly, it should be in the 2.2 version of the driver: https://jira.mongodb.org/browse/CSHARP-1374 回答1: If you use the AsQueryable() extension method on IMongoCollection<T>, you can then use the LINQ interface, as an example. var query = from p in collection.AsQueryable() join o in

Update/Delete a sub document in mongodb using C# driver

帅比萌擦擦* 提交于 2019-12-05 01:34:14
问题 I have 2 classes: public class Vote { public string VoteId { get; set; } public string Question { get; set; } public List<VoteAnswer> AnswerList { get; set; } } And: public class VoteOption { public string OptionId { get; set; } public string OptionName { get; set; } public double VoteCount { get; set; } } How can i update/delete a VoteOption in a Vote where VoteId = voteId and OptionId = optionId ? Using C# driver. First I get VoteOption by: var v = col.FindOneAs<Vote>(Query.EQ("VoteID",

c# mongodb case sensitive search

狂风中的少年 提交于 2019-12-04 21:29:28
I have a collection in which I store Email and password of user. I obviously don't want to require the user to insert his email case sensitive and to be exactly as when he first registered. I'm using mongodb 2.0 c# driver, I'm repeating it because I saw solutions to queries written with regex but I'm afraid I cant user it in here. my query looks like var filter = Builders<ME_User>.Filter.And( Builders<ME_User>.Filter.Eq(u => u.Email, email), Builders<ME_User>.Filter.Eq(u => u.Password, password)); ME_User foundUser = null; var options = new FindOptions<ME_User> { Limit = 1 }; using (var cursor

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

风流意气都作罢 提交于 2019-12-04 20:16:14
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 the new 2.2.3 driver I have an error mocking this. Is there another way? I've seen that the new driver