mongodb-.net-driver

MongoDb c# driver find item in array by field value

☆樱花仙子☆ 提交于 2019-11-30 08:02:10
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 ? There is ElemMatch var filter = Builders<Post>.Filter.ElemMatch(x => x.Tags, x => x.Name == "test"); var res =

How to get the Mongo database specified in connection string in C#

和自甴很熟 提交于 2019-11-30 07:48:14
I would like to connect to the database specified in the connection string, without specifying it again in GetDatabase . For example, if I have a connection string like this; mongodb://localhost/mydb I would like to be able to db.GetCollection("mycollection") from mydb . This would allow the database name to be configured easily in the app.config file. Andrew Orsich Update: MongoServer.Create is obsolete now (thanks to @aknuds1). Instead this use following code: var _server = new MongoClient(connectionString).GetServer(); It's easy. You should first take database name from connection string

Updating an embedded document in MongoDB with official C# driver

半世苍凉 提交于 2019-11-30 05:27:12
If I have a Company collection which contains embedded Divisions: { "_id": 1 "_t": "Company", "Name": "Test Company" "Divisions": [ { "_id": 1 "_t": "Division", "Name": "Test Division 1" }, { "_id": 2 "_t": "Division", "Name": "Test Division 2" } ] } What is the best way to save/update an entire Division when using the official 10gen C# driver ? (The latest 0.9 release.) I'm using Update.AddToSetWrapped to add Divisions, and that works fine, but I'd also like to be able to update documents depending on their _id. For example, if I define the following Update method: public void UpdateDivision

Storing a Dictionary with polymorphic values in mongoDB using C#

眉间皱痕 提交于 2019-11-30 04:56:40
问题 Let us say we have a key with values which are polymorphic in their sense. Consider the next sample project: public class ToBeSerialized { [BsonId] public ObjectId MongoId; public IDictionary<string, BaseType> Dictionary; } public abstract class BaseType { } public class Type1 : BaseType { public string Value1; } public class Type2:BaseType { public string Value1; public string Value2; } internal class Program { public static void Main() { var objectToSave = new ToBeSerialized { MongoId =

Is there mongodb C# driver support System.Dynamic.DynamicObject in .NET 4?

两盒软妹~` 提交于 2019-11-30 04:52:23
Im working on a project that use .NET Razor and mongodb. I would like to do something like this: @{ var feeds = DP.Database.GetCollection("feeds").FindAll(); } <ul> @foreach (dynamic feed in feeds) { <li>@feed.message - @feed.from.name</li> } </ul> However, the current mongodb C# driver FindAll() return collection of BsonDocument which does not support dynamic object. Anybody know a .NET 4 dynamic supported mongodb C# driver? Thanks a lot Currently, there is no support for dynamic in the MongoDB driver. This is because it is based on .NET 3.5. However, since a .NET 4.0 assembly can reference a

How do you increment a field in mongodb using c#

爷,独闯天下 提交于 2019-11-30 04:49:47
问题 Thought this would be pretty straight forward, but my value is remaining the same (0). What I'd like to do is increment my UnreadMessages field when the user receives a message they haven't read and then decrement it when they have. So I thought code like this would work: var userHelper = new MongoHelper<User>(); //increment userHelper.Collection.Update(Query.EQ("Id", userId.ToId()), Update.Inc("UnreadMessages", 1)); //decrement userHelper.Collection.Update(Query.EQ("Id", userId.ToId()),

Creating MongoDB Unique Key with C#

佐手、 提交于 2019-11-30 04:36:36
I am fighting to create a unique field EmailAddress . I've already seen in forums that I have to create an index, but it didn't work out for me so far. Does anyone have a code example? Do I have to create the index on every save/call, or is it enough to create it only once? I tried this code: DB.GetCollection<User>(Dbname) .EnsureIndex(new IndexKeysBuilder() .Ascending("EmailAddress"), IndexOptions.SetUnique(true)); DB.GetCollection<User>(Dbname).Save(user, SafeMode.True); My User model looks like this: public class User { [Required(ErrorMessage = "Email Required")] public string EmailAddress

MongoDB custom serializer implementation

陌路散爱 提交于 2019-11-30 03:04:28
问题 I am new to MongoDB, and am trying to get the C# driver to work serializing F# classes. I have it working with the class automapper using mutable F# fields & a parameterless constructor, but really I need to retain immutability, so I started looking at implementing an IBsonSerializer to perform custom serialization. I haven't found any documentation for writing one of these so have just tried to infer from the driver source code. I have run into a problem whereby when the Deserialize method

BsonValue and custom classes in MongoDB C# Driver

你。 提交于 2019-11-30 01:37:18
问题 I'm trying to use $push in an update query in mongodb, with the c# driver. The Update.Push(...) method requires a string name (that's fine), and a BsonValue to be 'pushed'. This is where I run into problems. I'm trying to push a non simple type to the field's array. For example: { $push : { "arrayfield" : { "a" : 7, "b" : 12 } } } This works fine in the Mongo console, but I can't figure out how to make my object into a BsonValue. I've tried BsonValue.Create(myObjectInstance) but that gives me

What is the most mature MongoDB driver for C#?

两盒软妹~` 提交于 2019-11-30 00:43:35
So, there are mongodb-csharp simple-mongodb NoRM as C# drivers for MongoDB available. Which one of them is the most mature and stable one? Why would you choose one over the other two? Are they production ready? The mongodb-csharp driver is about to make a huge push regarding support for typedcollections which will include full Linq support. I think you'll find that it is easy to work. The other 2 projects are also steaming ahead. If you want .NET 4.0 support, simple-mongodb would be your best bet. NoRM has a whole bunch of committers who are all great coders, so no problem with it except it