mongodb-.net-driver

Retrieve data from mongodb using C# driver

孤街醉人 提交于 2019-12-04 02:36:30
I'm using official mongodb driver for c# in my test project and i've already insert document from c# web application to mongodb. In mongo console, db.blog.find() can display entries I've inserted. but when i tried to retrieve them, .net throw a exception "System.InvalidOperationException: ReadString can only be called when CurrentBsonType is String, not when CurrentBsonType is ObjectId." my entity class is very simple namespace MongoDBTest { public class Blog { public String _id { get; set; } public String Title { get; set; } } } and this is my retrieve code public List<Blog> List() {

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

MongoDB: update only specific fields

自作多情 提交于 2019-12-04 00:55:26
问题 I am trying to update a row in a (typed) MongoDB collection with the C# driver. When handling data of that particular collection of type MongoCollection<User> , I tend to avoid retrieving sensitive data from the collection (salt, password hash, etc.) Now I am trying to update a User instance. However, I never actually retrieved sensitive data in the first place, so I guess this data would be default(byte[]) in the retrieved model instance (as far as I can tell) before I apply modifications

MongoDB LinQ “Select” method will really retrieve only a subset of fields?

梦想的初衷 提交于 2019-12-03 21:45:32
问题 Searching across the internet how to retrieve a subset of fields in MongoDB, using C# official driver (but using LinQ as the base architecture) I found how to do this in MongoDB shell. // selecting only "field" of a collection db.collection.find( { field : 'value' }, { field: 1 } ); Then, I found at C# LinQ Tutorial the Select method, which is equivalent to this: collection.AsQueryable<T>().Select(x => new { x.field }); However, the tutorial says the method " is used to project a new result

Using Facets in the Aggregation Framework C#

不羁的心 提交于 2019-12-03 21:37:37
I would like to create an Aggregation on my data to get the total amount of counts for specific tags for a collection of books in my .Net application. I have the following Book class. public class Book { public string Id { get; set; } public string Name { get; set; } [BsonDictionaryOptions(DictionaryRepresentation.Document)] public Dictionary<string, string> Tags { get; set; } } And when the data is saved, it is stored in the following format in MongoDB. { "_id" : ObjectId("574325a36fdc967af03766dc"), "Name" : "My First Book", "Tags" : { "Edition" : "First", "Type" : "HardBack", "Published" :

C# Mongo FirstOrDefaultAsync hangs

萝らか妹 提交于 2019-12-03 21:29:39
问题 using the 2.0 driver the following code will sometimes hang and never return. public async Task<T> GetFirst(FilterDefinition<T> query) { return await GetCollection.Find(query).FirstOrDefaultAsync(); } if I debut and put a break point on the return line, everything returns normally. In the shell the query being run is something like this: db.Customers.find({"Name" : /test$/i}) 回答1: There are 2 solutions: Add a ConfigureAwait(false) at the end: return await GetCollection.Find(query)

MongoDB C# Driver - how to store _id as ObjectId but map to string Id property?

女生的网名这么多〃 提交于 2019-12-03 16:45:55
问题 I'm having trouble getting my model to represent an entity's Id property as a string but have it auto-generated and represented internally by MongoDb as a native ObjectId . class Account { public string Id { get; set; } ... } class AccountStore { static AccountStore() { BsonClassMap.RegisterClassMap<Account>(cm => { cm.AutoMap(); cm.SetIgnoreExtraElements(true); // map Id property here }); } public void Save(Account account) { _accounts.Save(account); } } For the line // map Id property here

How to $lookup with MongoDB C# driver?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 16:42:01
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 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 otherCollection on p.Name equals o.Key into joined select new { p.Name, AgeSum: joined.Sum(x => x.Age) }; This was

Adding BSON array to BsonDocument in MongoDB

一个人想着一个人 提交于 2019-12-03 16:36:49
问题 How can I add BsonArray to BsonDocument in MongoDB using a C# driver? I want a result something like this { author: 'joe', title : 'Yet another blog post', text : 'Here is the text...', tags : [ 'example', 'joe' ], comments : [ { author: 'jim', comment: 'I disagree' }, { author: 'nancy', comment: 'Good post' } ] } 回答1: You can create the above document in C# with the following statement: var document = new BsonDocument { { "author", "joe" }, { "title", "yet another blog post" }, { "text",

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

ⅰ亾dé卋堺 提交于 2019-12-03 16:27:05
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", voteId)); VoteOption vo = v.AnswerList.Find(x => x.OptionId == optionId); End set some value to it: vo