mongodb-csharp-2.0

Linq to MongoDB Filter

こ雲淡風輕ζ 提交于 2019-12-24 00:58:24
问题 I have a MongoDB collection, listed below: { "_id" : ObjectId("001"), "ticker" : "MSFT=US", "exchange" : "OTC", "localtick" : "MSFT", "compname" : "Microsoft", "currency" : "USD", "insertedtime" : ISODate("2016-06-13T23:10:09.341+0000") } { "_id" : ObjectId("002"), "ticker" : "TSLA=CA", "exchange" : "TSX", "localtick" : "TSLA", , "compname" : "Tesla", "currency" : "CAD", "insertedtime" : ISODate("2016-06-13T23:10:09.809+0000") } But when I try to do a filter in my query: var documents =

Update field in array mongodb c# driver

杀马特。学长 韩版系。学妹 提交于 2019-12-23 10:19:10
问题 I try to update status field for object from p2l array var update = Builders<BsonDocument>.Update.Set("p2l.$.status",BsonValue.Create(status)) It seems that code will work fine, but how to implement it with typed builder and set all fields with lambda ? I found a solution by the following link How to update a field in an array's subdocument contained in an array's subdocument in MongoDB using C# driver? But it suitable only for old version of driver. 回答1: You can try something like: Builders

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

Building indexes in MongoDB with .NET driver 2.0

蓝咒 提交于 2019-12-21 03:59:34
问题 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. 回答1: 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"

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 c# driver find item in array by field value

≯℡__Kan透↙ 提交于 2019-12-18 12:46:13
问题 i found the way to check is the value contains in simple array : var filter = Builders<Post>.Filter.AnyEq(x => x.Tags, "mongodb"); But how to find a complex item with many fields by a concrete field ? I found the way to write it via the dot notation approach with BsonDocument builder, but how can i do it with typed lambda notations ? upd i think it some kind of builderInst.AnyIn(p => p.ComplexCollection.Select(ml => ml.Id), mlIds) but can't check right now, is anyone could help ? 回答1: There

Get generated script in MongoDB C# driver

烂漫一生 提交于 2019-12-17 19:46:10
问题 I am using MongoDB.Driver 2.0.0. Is there any way to see a generated script from linq to MongoDB? For example my query is like: IFindFluent<ProductMapping, ProductMapping> findFluent = Collection.Find( x => hashValues.Contains(x.UrlHash) && x.ProductTopic == topicId); How would this (or more complex queries) be represented in the MongoDB shell? 回答1: EDIT Please see i3arnon's answer for a client-side method using Render() that is usually easier. You can use the integrated mongodb profiler to

BsonSerializationException when serializing a Dictionary<DateTime,T> to BSON

百般思念 提交于 2019-12-17 18:35:11
问题 I've recently moved to the new MongoDB C# driver v2.0 from the deprecated v1.9. Now, when I serialize a class that has a dictionary I sometimes run into the following BsonSerializationException : MongoDB.Bson.BsonSerializationException: When using DictionaryRepresentation.Document key values must serialize as strings. Here's a minimal reproduce: class Hamster { public ObjectId Id { get; private set; } public Dictionary<DateTime,int> Dictionary { get; private set; } public Hamster() { Id =

MongoDB C# driver 2.0 InsertManyAsync vs BulkWriteAsync

橙三吉。 提交于 2019-12-10 12:34:36
问题 I have to insert many documents in a MongoDB collection, using the new C# 2.0 driver. Is using either collection.InsertManyAsync(...) collection.BulkWriteAsync(...) making any difference? (particularly about performance). From what i understand from MongoDB documentation, an insert with an array of documents should be a bulk operation under the hood. Is that correct? Thanks for your help. 回答1: I found the answer looking at the driver source code: the InsertManyAsync uses internally the

Unwind then Group aggregation in MongoDB C#

霸气de小男生 提交于 2019-12-08 16:05:16
问题 I'm having some trouble with the new C# 2.0 MongoDB driver and the aggregation pipeline. Basically, I'm trying to return the most popular elements within an array field on the object. The field type is: IList<string> FavouritePlaceIds { get; set; } . I have the following MongoDB aggregation which is working as expected: db.users.aggregate([ { $unwind : "$FavouritePlaceIds" }, { $group: { "_id": "$FavouritePlaceIds", "count": {$sum: 1}}}, { $sort : { "count": -1 }} ]) However, the issue is now