mongodb-.net-driver

MongoDB: Using $sample with C# driver

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 12:38:07
问题 I'm trying to express the following query using the MongoDB C# driver (2.4.4): db.media.aggregate({ $sample: { size: 1 }}) This what I have so far: BsonDocument sample = new BsonDocument { { "$sample", new BsonDocument { { "size", 1 } } } }; MongoBlob mongoBlob = await _collection .Aggregate() .Group<MongoBlob>(sample) .FirstOrDefaultAsync(); I cannot put the sample to .Aggregate(AggregateOptions options = null) and putting it into the .Group(...) is obviously wrong. There is also no any like

How to mock IFindFluent interface

…衆ロ難τιáo~ 提交于 2020-01-24 11:28:46
问题 I am writing unit tests with mongo db and I need to test somehow methods that works with data returned from mongo. For example method IFindFluent<Transcript, Transcript> GetTranscriptByUserId(int userId); Should return some transcript. But instead it returns interface that has couple of props - Filter , Options , Sort , and other. I am going to test method Paginane of Paginator<T> class public PaginatedObject<T> Paginate(IFindFluent<T,T> items, int limit, int page) { if (limit == 0) { limit =

MongoDB Aggregate function in C#

天涯浪子 提交于 2020-01-23 03:45:06
问题 I am trying to display/list data after using aggregation function but it isn't happening. This code works absolutely fine. var connectionstring = "mongodb://localhost:27017"; var client = new MongoClient(connectionstring); var db = client.GetDatabase("school"); var col = db.GetCollection<BsonDocument>("students"); var filter = new BsonDocument("type", "homework"); var filter2 = Builders<BsonDocument>.Filter.Eq("scores.type", "homework"); var myresults = await col.Find(filter2) .Limit(2)

C# MongoDB complex class serialization

荒凉一梦 提交于 2020-01-22 12:57:11
问题 I'm working with C# MongoDB driver and I have this rather complex JSON struct to save: { "name" : "value", "age": 1, "isFemale": true, "Hobbies" : { //All data within the "Hobbies" node is dynamic //and may change from one item to another. "stringItem" : "value", "intItem" : 0.0, "listOfItems" : [ { "field" : 1696.0 } ], "intArray" : [ 566.0, 1200.0 ] }, "Collection" : [ //All data within the "Collection" node is dynamic //and may change from one item to another. { "field" : "string",

Enum can't be deserialized when using Linq

回眸只為那壹抹淺笑 提交于 2020-01-15 10:33:55
问题 So I have this enum public enum JobStatus { Created = 0, Assigning = 1, Assigned = 2, Started = 3, Finished = 4 } In this interface public interface IJob { Guid Id { get; set; } JobStatus Status { get; set; } bool IsFaulted { get; set; } string ErrorMessage { get; set; } } I plop one of these guys in the database, and it goes fine. This is what that looks like. { "_id" : { "$uuid" : "4e5002b6-3c80-b497-4a33-46b0ea5a39bf"} , "_t" : "MyJobObject" , "Status" : 0 , "IsFaulted" : false ,

Enum can't be deserialized when using Linq

南楼画角 提交于 2020-01-15 10:33:43
问题 So I have this enum public enum JobStatus { Created = 0, Assigning = 1, Assigned = 2, Started = 3, Finished = 4 } In this interface public interface IJob { Guid Id { get; set; } JobStatus Status { get; set; } bool IsFaulted { get; set; } string ErrorMessage { get; set; } } I plop one of these guys in the database, and it goes fine. This is what that looks like. { "_id" : { "$uuid" : "4e5002b6-3c80-b497-4a33-46b0ea5a39bf"} , "_t" : "MyJobObject" , "Status" : 0 , "IsFaulted" : false ,

Partial mongodb upsert using the c# driver?

久未见 提交于 2020-01-15 08:59:51
问题 Mongo version 1.8.2. Assume I have a class like public class Acc { public int _id { get; set; } public int? Foo { get; set; } public int? Bar{ get; set; } } Acc a = new Acc { _id = 1, Foo = 3 }; I'd like to call myCollection.Save(a), such that if it doesn't exist, its inserted (easy so far) if it does exist, Foo is updated, but, but Bar remains whatever it currently is (perhaps non-null...) How do I achieve this partial upsert? Many thanks. 回答1: It would be quite easy to do it with 2

MongoDB Aggregation Pipeline C#

自闭症网瘾萝莉.ら 提交于 2020-01-15 08:28:09
问题 I have the following Mongo query: db.BusRatings.aggregate( [ { $match: { VId: 2020}}, { $project: { vid: '$VId', sb:'$SvcRating.StaffBehavior' ,bq: '$SvcRating.BusQuality' , src: '$SvcDetails.SrcNm', dst: '$SvcDetails.DestNm', py: '$SvcRating.Punctuality'}}, { $group: { _id: {vid: '$vid', src: '$src', dst: '$dst'}, numratings: {$sum: 1}, avgpy : { $avg : '$py'}, avgbq: { $avg: '$bq'},avgsb: { $avg: '$sb'}} }, { $match : { numratings: { $gt: 3 } } }, { $project: {_id: 0, 'vid' : '$_id.vid',

Unit Testing with IMongoQueryable

陌路散爱 提交于 2020-01-15 07:13:07
问题 I am using .NET Core 2.0 and the .NET Core MongoDB driver. I have created a repository like so: public interface IRepository<T> { IMongoQueryable<T> Get() } I have done this to give flexibility to whoever uses this to be able to do LINQ much like they would do using EF. The problem is when it comes to unit testing and I'm trying to create an in-memory database so I can check states before and after operation. Some stuff I tried: public class InMemoryRepository : IRepository<ConcreteType> {

How to retrieve all embedded document value using the official C# driver for MongoDB?

不打扰是莪最后的温柔 提交于 2020-01-14 14:26:11
问题 Given the the following classes and sample document, How do I retrieve a AnswerChoice document from the Question collection where _id in AnswerChoice is '4d6d336ae0f84c23bc1fae00' using the official C# driver. Thank you. public class Question { [BsonId] public ObjectId QuestionId {get;set;} public string Question {get;set;} public List<AnswerChoice> AnswerChoices {get;set;} } public class AnswerChoice { [BsonId] public ObjectId AnswerChoiceId {get;set;} public string Answer {get;set;} public