mongodb-.net-driver

Unit of work in mongodb and C#

余生颓废 提交于 2019-12-20 09:14:38
问题 I know that MongoDB is not supposed to support unit of work, etc. But I think it would be nice to implement the repository which would store only the intentions (similar to criteria) and then commit them to the DB. Otherwise in every method in your repository you have to create connection to DB and then close it. If we place the connection to DB in some BaseRepository class, then we tie our repository to concrete DB and it is really difficult to test repositories, to test IoC which resolve

What would be the MongoDB C# driver equivalent of the following query using the array update operator $[<identifier>]

China☆狼群 提交于 2019-12-20 06:28:15
问题 From https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#up.S[%3Cidentifier%3E] Given the following collection { "_id" : 1, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 6 }, { "grade" : 85, "mean" : 90, "std" : 4 }, { "grade" : 85, "mean" : 85, "std" : 6 } ] } { "_id" : 2, "grades" : [ { "grade" : 90, "mean" : 75, "std" : 6 }, { "grade" : 87, "mean" : 90, "std" : 3 }, { "grade" : 85, "mean" : 85, "std" : 4 } ] } and query db.students2.update( { }, { $set: {

How to count, select and update nested elements in mongodb + C#

社会主义新天地 提交于 2019-12-20 05:49:10
问题 everyone! I am creating a open source social network (I don't know if I can post the URL here) using .net core 2.1, mongo atlas using the driver v2.9.2 I am migrating from MS SQL (wich I know how to do stuff on it) to mongo (wicth I am still learning). I have two entities, UserContent and UserContentComment, both inheriting from Entity: public class Entity { public Guid Id { get; set; } public Guid UserId { get; set; } public DateTime CreateDate { get; set; } public Entity() { CreateDate =

InsertMany Document in a MongoDB Collection using C# BsonArray

安稳与你 提交于 2019-12-20 04:28:15
问题 How to Insert more than one document in a Single statement using InsertMany() MongoDB Method in C# My MongoDB Database and Connections IMongoClient _client; IMongoDatabase _database; _client = new MongoClient(); _database = _client.GetDatabase("test"); var collection = _database.GetCollection<BsonDocument>("EmpInfo"); I'm having a Collection - BsonArray var EmpInfoArray = new BsonArray { new BsonDocument { {"EmpID", "100"}, {"EmpName", "John"}, {"EmpMobile", new BsonArray { new BsonDocument {

How to create a new database in MongoDB using the c# driver

为君一笑 提交于 2019-12-19 16:51:42
问题 I have read through the mongodb documentation and cannot seem to find out how to create a new database. For example, in the documentation it says I can access the "test" database like this: db.test.find() Now what if I want to create my own database using syntax like this: db.MyDB.find() Also, is there any documentation I can read online I can further read about creating databases and collections using the DOS interface and the c# driver? 回答1: I may be wrong, but buried in this documentation

MongoDb C# Typed Aggregations with Group Unwind and Project

爱⌒轻易说出口 提交于 2019-12-19 09:51:34
问题 I have a collection like this: [{ "_id": 1, "OtherProperties": 100 "PersonInventory": [{ "FirstName": "Joe", "MiddleName": "Bob", "LastName": "Blogs", "PersonId": 1 }] },{ "_id": 2, "OtherProperties": 1005 "PersonInventory": [{ "FirstName": "Joe", "MiddleName": "Bob", "LastName": "Blogs", "PersonId": 1 }] }] And I am trying to select all the unique persons in the root docs using here newer type inference mongodb c# driver syntax. Tried this so far but getting errors saying the method is not

Mongodb, linq driver. How to construct Contains with variable or statements

心不动则不痛 提交于 2019-12-19 07:52:21
问题 I'm using the Mongo LINQ Driver for C#, works great. Sorting a lot of properties but heres a problem I can't solve, its probably simple. var identifierList = new []{"10", "20", "30"}; var newList = list.Where(x => identifierList.Contains(x.Identifier)); This is NOT supported ... So I could do something like: var newList = list.Where(x => x.Identifier == "10" || x.Identifier == "20" || x.Identifier == "30"); But since the list is variable ... how do I construct the above? Or are there even

$project or $group does not support <document>

谁说胖子不能爱 提交于 2019-12-19 06:41:48
问题 I'm trying to run aggregate with projection but i get NotSupportedException: $project or $group does not support <document> . I am running version 2.4.4 of driver with mongodb v3.4. var filter = Builders<T>.Filter.Regex(x=>x.Value,"/test/gi"); var aggregate = collection.Aggregate() .Match(filter) .Project(x => new { Idx = x.Value.IndexOf("test"), Result = x }) .SortBy(x => x.Idx); I thought IndexOfCP is supported. What am i doing wrong here? 回答1: The problem is caused not by IndexOf but by

$project or $group does not support <document>

谁说我不能喝 提交于 2019-12-19 06:41:03
问题 I'm trying to run aggregate with projection but i get NotSupportedException: $project or $group does not support <document> . I am running version 2.4.4 of driver with mongodb v3.4. var filter = Builders<T>.Filter.Regex(x=>x.Value,"/test/gi"); var aggregate = collection.Aggregate() .Match(filter) .Project(x => new { Idx = x.Value.IndexOf("test"), Result = x }) .SortBy(x => x.Idx); I thought IndexOfCP is supported. What am i doing wrong here? 回答1: The problem is caused not by IndexOf but by

MongoDB: Getting the list of all databases?

大城市里の小女人 提交于 2019-12-18 15:12:45
问题 How do I list all databases for a connection using Mongo C# Driver? 回答1: Very easily: var server = MongoServer.Create("mongodb://localhost/?safe=true"); var databaseNames = server.GetDatabaseNames(); 回答2: The MongoServer class was deprecated in version 2.0.0. You can use ListDatabasesAsync using (var cursor = await client.ListDatabasesAsync()) { await cursor.ForEachAsync(d => Console.WriteLine(d.ToString())); } 回答3: Working Solution: MongoClient client = new MongoClient("mongodb://localhost