mongodb-.net-driver

Mongo C# Driver: Deserialize BsonValue

喜欢而已 提交于 2019-12-03 03:27:29
I have a document in mongodb that is structured similar to this: { "_id": "abcdef01234", "Name": "Product A", "Dimensions": [ { "Height": 32, "Width": 64 }, { "Height": 16, "Width": 32 }, { "Height": 8, "Width": 16 } ] } I also have a class defined to represent dimensions (the sub document from above) public class Dimension { public int Height { get; set; } public int Width { get; set; } } I am selecting the "Product A" document in this manner: MongoServer srv = MongoServer.Create(myConnStr); BsonDocument doc = srv["db"]["products"].FindOneById(ObjectId.Parse("abcdef01234")); BsonValue

Get All 'documents' from MongoDB 'collection'

帅比萌擦擦* 提交于 2019-12-03 02:19:19
I need to retrieve all the documents that are in my collection in MongoDB, but I cannot figure out how. I have declared my 'collection' like this- private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project"); And I followed what is explained in this MongoDB tutorial. I adjusted it for my needs, like- var documents = await SpeCollection.Find(new Project()).ToListAsync(); However, I keep having the following error- MongoDB.Driver.IMongoCollection does not have a definition for 'Find' and the best override of the extension method [superlong stuff]. Find

Mongo Schema-less Collections & C#

…衆ロ難τιáo~ 提交于 2019-12-03 01:39:29
I'm exploring Mongo as an alternative to relational databases but I'm running into a problem with the concept of schemaless collections. In theory it sounds great, but as soon as you tie a model to a collection, the model becomes your defacto schema. You can no longer just add or remove fields from your model and expect it to continue to work. I see the same problems here managing changes as you have with a relational database in that you need some sort of script to migrate from one version of the database schema to the other. Am I approaching this from the wrong angle? What approaches do

MongoDB C# Driver: Ignore Property on Insert

心已入冬 提交于 2019-12-02 23:27:07
I am using the Official MongoDB C# Drive v0.9.1.26831, but I was wondering given a POCO class, is there anyway to ignore certain properties from getting inserted. For example, I have the following class: public class GroceryList { public string Name { get; set; } public FacebookList Owner { get; set; } public bool IsOwner { get; set; } } Is there a way, for the IsOwner to not get inserted when I insert a GroceryList object? Basically, I fetch the object from the database then set the IsOwner property in the app layer and then return it back to the controller, which than maps the object to a

MongoDB with C# - query with dynamically generated predicate

人盡茶涼 提交于 2019-12-02 20:40:39
问题 I am having trouble querying a MongoDB database using a predicate which is dynamically generated. The document structue i am trying to query on is: { "_id" : 3121 , "Active" : true , "CategoryId" : 1 , "Crci" : "IH" , "CultureId" : null , "DateUpdated" : { "$date" : 1381916923120 } , "Description" : "National Careers Service: Actuary" , "Keywords" : "" , "MaxLevel" : null , "MinLevel" : null , "PhoneNumber" : " " , "Priority" : 1 , "Title" : "National Careers Service: Actuary" , "WebUrl" :

What is the right way to manage MongoDB connections in ASP.Net MVC?

女生的网名这么多〃 提交于 2019-12-02 20:19:55
What is the best practice for managing the MongoServer class life cycle? Should I create one and close it at the end of each request or should it be kept as a singleton for the entire life of the app using something like StructureMap? Any help is appreciate. In the official documentation it is stated that MongoServer , MongoDatabase , and MongoCollection are thread safe, and that you're supposed to create one single MongoServer for each database that you connect to. Thus, MongoServer , MongoDatabase , and MongoCollection can safely be configured to be singletons. MongoServer will even help

Projection in Where Clause Query of a Embedded document in MongoDB Collection using C#

故事扮演 提交于 2019-12-02 18:22:32
问题 Filter the Collection in DB instead of Memory I'm having a Model Class, Save it in a MongoDB Collection then Query the same as per my expectation mentioned below. My Model Class: public Class Employee { public ObjectId Id { get; set; } public string EmpID { get; set; } public string EmpName { get; set; } public List<Mobile> EmpMobile { get; set; } public bool IsLive { get; set; } } public Class Mobile { public string MobID { get; set; } public string MobNumber { get; set; } public bool

Unit of work in mongodb and C#

家住魔仙堡 提交于 2019-12-02 18:01:02
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 repositories. Is creating a session in MongoDB a bad idea? Is there a way to separate the connection

How to have a different name for entries of a mongodb document in C#?

て烟熏妆下的殇ゞ 提交于 2019-12-02 11:35:54
问题 I'm trying to do CRUD operations on documents in MongoDB and C# . I would like to have fixed structured domain entities in C# with long meaningful property names but since the name of each property will be saved in MongoDB for each document, that's not a good idea. That's because property names will be redundantly saved in database and affect the total storage and performance. The solution I can think of to overcome this problem, is to use different names for properties in C# than in MongoDB

Slice with Projection with C#

浪尽此生 提交于 2019-12-02 10:00:52
问题 Is there any way to implement a slice along with a projection in just one query using the c# driver? Below is what i am trying to achieve using c#, but im stuck, can anyone help me out wit it? db.employee.find({"employeeId": "999"}, { "empActivity" : { "$slice": -1 } }, {"employeeId": 1, "empActivity.transId": 1, _id: 0}) Note : empActivity is a an array containing nested documents, my query above works perfectly via the mongo shell but i am not able to figure out its equivalent in C#. 回答1: