mongodb-.net-driver

How to use $push update modifier in MongoDB and C#, when updating an array in a document

血红的双手。 提交于 2019-12-18 14:53:03
问题 I've run the following code in mongo shell: db.unicorns.insert({name: 'Dunx', loves: ['grape', 'watermelon']}); and now I've something like this in my MongoDB collection: {name: 'Dunx', loves: ['grape', 'watermelon']} As you can see loves is an array. Question How can I write C# code, with the official C# driver, that does the following: db.unicorns.update({name: 'Aurora'}, {$push: {loves: 'sugar'}}) The above code runs just fine in mongo shell. 回答1: it should be something like this: unicorns

How is an IAsyncCursor used for iteration with the mongodb c# driver?

╄→гoц情女王★ 提交于 2019-12-18 14:17:29
问题 I'm trying to get a list of all the databases in my server and ultimately print them out (i.e. use their names as string s). With the previous version of the c# driver I could call the Server.GetDatabases() , but that has been replaced with ListDatabasesAsync() . The return value is an IAsyncCursor<> and I'm not sure what to do with it. How does one iterate through the list of databases (or anything) with such a cursor? 回答1: Short answer: use the ForEachAsync extension method: var cursor =

How is an IAsyncCursor used for iteration with the mongodb c# driver?

僤鯓⒐⒋嵵緔 提交于 2019-12-18 14:17:08
问题 I'm trying to get a list of all the databases in my server and ultimately print them out (i.e. use their names as string s). With the previous version of the c# driver I could call the Server.GetDatabases() , but that has been replaced with ListDatabasesAsync() . The return value is an IAsyncCursor<> and I'm not sure what to do with it. How does one iterate through the list of databases (or anything) with such a cursor? 回答1: Short answer: use the ForEachAsync extension method: var cursor =

C# driver for MongoDb: how to use limit+count?

六眼飞鱼酱① 提交于 2019-12-18 13:23:44
问题 From MongoDb documentation: " On a query using skip() and limit(), count ignores these parameters by default. Use count(true) to have it consider the skip and limit values in the calculation. " That's exactly what I need to count resulted elements for the specific query until it's over defined limit like 1000, but I do not see any way to do it in c# driver. Count of IMongoCollection and SetCount of IMongoCursor are both parameter-less. Any idea? 回答1: Use the Size method instead of Count , as

MongoDb c# driver find item in array by field value

≯℡__Kan透↙ 提交于 2019-12-18 12:46:13
问题 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 ? 回答1: There

What is the most mature MongoDB driver for C#?

荒凉一梦 提交于 2019-12-18 10:51:03
问题 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? 回答1: 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

Unable to connect to MongoDB (MongoLabs) via C# client

人走茶凉 提交于 2019-12-18 07:44:41
问题 Question Background: I have setup in MongoLabs (mLab - https://mlab.com/) a database and have added a very simple Collection. I am using the MongoDB driver to attempt to connect and work with this collection via the C# 3.2 driver. The Issue: I am unable to connect to my database via the C# driver with a constant authentication expection, as shown: System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector

Deserialize object as an interface with MongoDB C# Driver

喜夏-厌秋 提交于 2019-12-18 04:39:19
问题 I am developing a project that uses MongoDB (with C# driver) and DDD . I have a class ( aggregate ) which have a property which type is an interface. In another class, I have implemented this interface. This class has another property which type is an interface and is setted with another implemented class. The code below explains better: // Interfaces public interface IUser { Guid Id { get; set;} IPartner Partner{ get; set; } } public interface IPartner { IPhone Mobile { get; set; } } public

MongoDB (server v 2.6.7) with C# driver 2.0: How to get the result from InsertOneAsync

 ̄綄美尐妖づ 提交于 2019-12-18 04:08:36
问题 I am testing MongoDB (server v 2.6.7) with the C# driver 2.0. When I am using the insert function InsertOneAsync for a document with an _id which exists I am expecting an error like the one you get from the Mongo shell: WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.Commands.$_id_ dup key: { : 0.0 }" }}) But the problem is that the insert with the C# driver does not throw an exception and

How to remove one 'document' by 'ID' using the Official C# Driver for MongoDB?

假如想象 提交于 2019-12-18 03:49:24
问题 Can someone please show me, if there is a better way to remove one document from MongoDB using the Official C# Driver than what I have below- var query = Query.EQ("_id", a.Id); database.GetCollection<Animal>("Animal").Remove(query); This code works, but seems too much work to me. The "Save" command for example- takes an instance and updates it. I want something like- Remove(item) . Remarks: I'm trying to use the official driver of C# rather than NoRM or Samus which seems out of date. 回答1: