mongodb-.net-driver

Update nested array document

自闭症网瘾萝莉.ら 提交于 2019-12-03 16:18:28
say i have this model { _id : 1, ref: '1', children: [ { ref:'1.1', grandchildren: [ { ref:'1.1.1', visible: true; } ] } ] } I'm aware that positional operator for nested arrays isn't available yet. https://jira.mongodb.org/browse/SERVER-831 but wondered whether its possible to atomically update the document in the nested array? In my example, i'd like to update the visible flag to false for the document for ref 1.1.1. I have the children record ref == '1.1' and the grandchildrenref == '1.1.1' thanks Yes, this is possible only if you knew the index of the children array that has the

Deserializing field when type is changed using MongoDb csharp driver

≡放荡痞女 提交于 2019-12-03 12:24:12
I am testing a number of scenarios with MongoDb to see how to recover from possible data issues. I have classes (Addresses with collection of Address) with a zipcode property in Address which was originally cast as string. I saved out multiple records of Addresses and could retrieve them all fine. like this, var allAddresses = addresses.FindAllAs(); I changed the zip code property to int and saved some records. I then changed the zip code property back to string. When I attempt to read the collection back I get an error deserializing, as expected. var allAddresses = addresses.FindAllAs(); My

Mongo C# Driver: Deserialize BsonValue

拜拜、爱过 提交于 2019-12-03 12:18:31
问题 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);

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

Collection ID length in MongoDB

末鹿安然 提交于 2019-12-03 11:05:12
i am new to mongodb and stack overflow. I want to know why on mongodb collection ID is of 24 hex characters? what is importance of that? Why is the default _id a 24 character hex string? The default unique identifier generated as the primary key ( _id ) for a MongoDB document is an ObjectId . This is a 12 byte binary value which is often represented as a 24 character hex string, and one of the standard field types supported by the MongoDB BSON specification . The 12 bytes of an ObjectId are constructed using: a 4 byte value representing the seconds since the Unix epoch a 3 byte machine

Get All 'documents' from MongoDB 'collection'

ε祈祈猫儿з 提交于 2019-12-03 10:47:37
问题 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

MongoDB atomic update via 'merge' document

孤街醉人 提交于 2019-12-03 08:47:39
I know that I can atomically update an existing Mongo document by setting specific fields. The following code will do it: var update = MongoDB.Driver.Builders.Update.Set("InsideLegMeasurement", 32.4); SafeModeResult result = personCollection.Update(query, update, UpdateFlags.Multi,SafeMode.True); However, can I atomically update several fields by passing in a document that I want to 'merge' with the existing doc? Imagine I have a document as follows: {"favcolor":"red","favfood":"pasta"} and I want to update an existing doc with these values. I want to do this: var update = MongoDB.Driver

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

﹥>﹥吖頭↗ 提交于 2019-12-03 06:40:38
问题 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. 回答1: 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 ,

How to improve MongoDB insert performance

坚强是说给别人听的谎言 提交于 2019-12-03 06:30:44
问题 The result: If you are operating on a dataset that is fault tolerant, or doing a one time process you can verify, changing WriteAcknowledge to Unacknowledged can help. Also, bulk operations are IsOrdered by default, which I was not aware off. Setting this to False actually makes the operation perform in bulk, otherwise it operates as one thread of updates. MongoDB 3.0 / WiredTiger / C# Driver I have a collection with 147,000,000 documents, of which I am performing updates each second

Adding BSON array to BsonDocument in MongoDB

浪子不回头ぞ 提交于 2019-12-03 05:11:48
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' } ] } You can create the above document in C# with the following statement: var document = new BsonDocument { { "author", "joe" }, { "title", "yet another blog post" }, { "text", "here is the text..." }, { "tags", new BsonArray { "example", "joe" } }, { "comments", new BsonArray { new