mongodb-csharp-2.0

How to do findAll in the new mongo C# driver and make it synchronous

蓝咒 提交于 2019-12-08 14:30:26
问题 I was using official C# driver to do a FindAll and upgraded to the new driver 2.0. FindAll is obsolete and is replaced with Find. I am trying to convert a simple method that returns me a list of Class1 . Cant find a realistic example using a POCO in their documentation var collection = database.GetCollection<ClassA>(Collection.MsgContentColName); return collection.FindAll().ToList(); Can someone please help me convert with 2.0 driver and return a list and not a task? 回答1: you could do this to

MongoDB C# driver 2.0: How to get the result from MapReduceAsync

浪子不回头ぞ 提交于 2019-12-07 17:38:57
问题 MongoDB C# driver 2.0: How to get the result from MapReduceAsync I'm using MongoDB version 3, C# driver 2.0 and would get the result of MapReduceAsync method. I have this collection "users": { "_id" : 1, "firstName" : "Rich", "age" : "18" } { "_id" : 2, "firstName" : "Rob", "age" : "25" } { "_id" : 3, "firstName" : "Sarah", "age" : "12" } The code in VisualStudio: var map = new BsonJavaScript( @" var map = function() { emit(NumberInt(1), this.age); };"); var reduce = new BsonJavaScript(@" var

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

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>

How to set MongoDB Change Stream 'OperationType' in the C# driver?

淺唱寂寞╮ 提交于 2019-12-06 02:05:29
问题 When running the new MongDB Server, version 3.6, and trying to add a Change Stream watch to a collection to get notifications of new inserts and updates of documents, I only receive notifications for updates, not for inserts. This is the default way I have tried to add the watch: IMongoDatabase mongoDatabase = mongoClient.GetDatabase("Sandbox"); IMongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>("TestCollection"); var changeStream = collection.Watch()

MongoDB C# driver 2.0: How to get the result from MapReduceAsync

徘徊边缘 提交于 2019-12-05 21:51:13
MongoDB C# driver 2.0: How to get the result from MapReduceAsync I'm using MongoDB version 3, C# driver 2.0 and would get the result of MapReduceAsync method. I have this collection "users": { "_id" : 1, "firstName" : "Rich", "age" : "18" } { "_id" : 2, "firstName" : "Rob", "age" : "25" } { "_id" : 3, "firstName" : "Sarah", "age" : "12" } The code in VisualStudio: var map = new BsonJavaScript( @" var map = function() { emit(NumberInt(1), this.age); };"); var reduce = new BsonJavaScript(@" var reduce = function(key, values) { var sum = 0; values.forEach(function(item) { sum += NumberInt(item);

FindAll in MongoDB .NET Driver 2.0

[亡魂溺海] 提交于 2019-12-05 16:23:31
问题 I want to query my MongoDB collection without any filter with MongoDB .NET Driver 2.0 but I didn't find a way. I have the following workaround but it looks weird :D var filter = Builders<FooBar>.Filter.Exists(x => x.Id); var fooBars = await _fooBarCollection.Find(filter) .Skip(0) .Limit(100) .ToListAsync(); Is there a way to issue queries without a filter in MongoDB .NET Driver 2.0? 回答1: You can't use Find without a filter. You can however use a filter that passes everything: var findFluent =

Insert new document using InsertOneAsync (.NET Driver 2.0)

孤街醉人 提交于 2019-12-04 15:56:44
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 document = new BsonDocument { { "_id", 1 }, { "x", 2 } }; await collection.InsertOneAsync(document);

FindAll in MongoDB .NET Driver 2.0

岁酱吖の 提交于 2019-12-04 02:00:56
I want to query my MongoDB collection without any filter with MongoDB .NET Driver 2.0 but I didn't find a way. I have the following workaround but it looks weird :D var filter = Builders<FooBar>.Filter.Exists(x => x.Id); var fooBars = await _fooBarCollection.Find(filter) .Skip(0) .Limit(100) .ToListAsync(); Is there a way to issue queries without a filter in MongoDB .NET Driver 2.0? You can't use Find without a filter. You can however use a filter that passes everything: var findFluent = await _fooBarCollection.Find(_ => true); Or you can use an empty document which is equivalent: var

Building indexes in MongoDB with .NET driver 2.0

…衆ロ難τιáo~ 提交于 2019-12-03 11:58:35
What's the new way to build indexes with the new driver 2.0? There's no documentation whatsoever about this. Apparently this now works with the new IndexKeysDefinitionBuilder<> interface but that's all I got so far. You need to call and await CreateOneAsync with an IndexKeysDefinition you get by using Builders.IndexKeys : static async Task CreateIndex() { var client = new MongoClient(); var database = client.GetDatabase("db"); var collection = database.GetCollection<Hamster>("collection"); await collection.Indexes.CreateOneAsync(Builders<Hamster>.IndexKeys.Ascending(_ => _.Name)); } If you don