mongodb-.net-driver

How do I use Linq to query nested dynamic BsonDocuments?

戏子无情 提交于 2019-12-24 00:44:02
问题 public class Foo { public ObjectId _id { get; set; } public BsonDocument properties { get; set; } } public void FindFoos() { var client = new MongoClient(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString); var server = client.GetServer(); var db = server.GetDatabase("FooBar"); //var collection = db.GetCollection<GeoJsonFeature<GeoJson2DGeographicCoordinates>>("Sections"); var collection = db.GetCollection<Foo>("Foos"); collection.Insert(new Foo { properties = new BsonDocument

MongoDB Many Indexes vs. Single Index on array of Sub-Documents?

强颜欢笑 提交于 2019-12-23 16:40:17
问题 Wondering which would be the more efficient technique for indexing my document's various timestamps that I need to keep track of, keeping in mind my application is fairly heavy on writing, but heavy enough on reading that without the indexes, the queries are too slow. Is it better to have a field for each timestamp, and index each field, or store the timestamps and their associated type in an array field, and index each field of that array? First option, separate fields, and an index for each

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

Using the mongo C# driver, how to serialize an array of custom object in order to store it?

吃可爱长大的小学妹 提交于 2019-12-23 07:01:59
问题 I have a product document that contains an array of documents. For example { id: 1, name: "J-E-L-L-O", store:[{id: 1, name: "Store X"}, {id: 2, name: "Store Y"}] } I would like to change the name of "Store Y" to Store Z", for instance. At the time, I don't know the index of the object. So, I pull the entire array, find the object to update, change the name, and then attempt to set the value of "store" with the updated array. productCollection.Update(query, Update.Set("store", storeList

Add custom fields in mongodb query

≯℡__Kan透↙ 提交于 2019-12-23 06:04:30
问题 I have a question for this situation with a collection in mongodb this is my Post collection { "_id" : ObjectId("530f67584fb1a510bc18c03f"), "creatorId" : "5f6f2c26-4101-4eae-90d1-20d109bea925", "creationDate" : ISODate("0001-01-01T00:00:00Z"), "category" : 23, "location" : [ -60.67045855832774, 52.86982649605247 ], "replies" : [ { "_id" : ObjectId("531acfc34fb1a50edc86fdcb"), "creatorId" : "0891f887-a6bc-4183-be10-2653b7b45e79" }, { "_id" : ObjectId("531acfc34fb1a50edc86fdcb"), "creatorId" :

MongoDB C# Driver and server generated ObjectIds

那年仲夏 提交于 2019-12-23 04:32:33
问题 Is there a way get server generated ObjectIds instead of C# Driver generated ObjectIds? Using MongoCollectionSettings.AssignIdOnInsert = false only gets you zeroed out ObjectIds. I'm doing multi-threaded inserts on a capped collection, and I'm seeing threads switch between the ObjectId generation and the insert. This causes out of order ObjectIds in a collection that's supposed to be ordered for tailed cursors sorting on natural order. Right now I'm using a static lock object for the inserts,

MongoDB C# Driver and server generated ObjectIds

回眸只為那壹抹淺笑 提交于 2019-12-23 04:32:11
问题 Is there a way get server generated ObjectIds instead of C# Driver generated ObjectIds? Using MongoCollectionSettings.AssignIdOnInsert = false only gets you zeroed out ObjectIds. I'm doing multi-threaded inserts on a capped collection, and I'm seeing threads switch between the ObjectId generation and the insert. This causes out of order ObjectIds in a collection that's supposed to be ordered for tailed cursors sorting on natural order. Right now I'm using a static lock object for the inserts,

How to convert ISODate to DateTime?

天大地大妈咪最大 提交于 2019-12-23 04:08:36
问题 In a collection I have IsoDates stored as follows: SubmitDateTime" : ISODate("2015-03-02T07:39:05.463Z") Now I want to map this property to MyModel with the following property: public class MyModel { public DateTime SubmitDateTime { get; set; } } And then map it as follows: GetCollection<MyModel>("collection_name").Find(myQuery).ToListAsync(); Unfortunately I get this error: System.ArgumentOutOfRangeException The value -9223372036854775808 for the BsonDateTime MillisecondsSinceEpoch is

Case insensitive property mapping

别说谁变了你拦得住时间么 提交于 2019-12-23 03:13:03
问题 When serializing a MongoDB document to a POCO is there any way to make properties map case insensitive? For example I'd like this document: { "id": "1" "foo": "bar" } to map to this class: public MyObj { public int Id {get; set;} public string Foo {get; set;} } 回答1: To do that I think you will have 2 options. The first would be to write out a class map manually BsonClassMap.RegisterClassMap<MyClass>(cm => { cm.AutoMap(); cm.GetMemberMap(c => c.Foo).SetElementName("foo"); }); The second would

MongoDB C# Driver doesn't release connections then errors

萝らか妹 提交于 2019-12-22 17:46:04
问题 I'm using the latest versions of MongoDB (on a Win 64 Server) and the C# driver. I have a windows service that is doing 800 reads and updates per minute, and after a few minutes the current threads used goes above 200 and then every single mongodb call gives this error: System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because