mongodb-.net-driver

handle @ in mongodb connection string

試著忘記壹切 提交于 2019-11-26 23:27:40
问题 I have connection string <add key="MongoDBConnectionString" value="mongodb://user:password@123@127.0.0.1/?safe=true"/> password is password@123 so host is parsing 123@127.0.0.1, how can i handle @ in credential Edit I have tried to escape it but still same problem <add key="MongoDBConnectionString" value="mongodb://user:password@123@127.0.0.1/?safe=true"/> Edit Thanks to alexjamesbrown for helping, following escaping working for me. <add key="MongoDBConnectionString" value="mongodb://user

MongoDB C# 2.0 TimeoutException

∥☆過路亽.° 提交于 2019-11-26 23:15:55
We've recently upgraded our web application to MongoDB C# Driver 2.0 and deployed to production. Below a certain load, the application runs fine. Once the load on the production server exceeds a certain limit, the CPU of the application instantly falls down to 0 and after about 30 seconds, this exception is logged several times: System.TimeoutException message: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = System.Collections.Generic.List`1[MongoDB.Driver.TagSet] } },

MongoDB .Net driver 2.0 Pull (remove element)

。_饼干妹妹 提交于 2019-11-26 22:34:59
Can you help me to run correctly "Pull (remove)" with 2.0 driver. I have a collection like this and I want to remove first follower named as fethiye by follower field. { "_id": ObjectId("554e05dfc90d3d4dfcaa2aea"), "username": "bodrum", "followerList": [ { "_id": ObjectId("554e0625a51586362c33c6df"), "follower": "fethiye", "avatar": "fethiye.png" }, { "_id": ObjectId("554e0625a51586362c33c6df"), "follower": "izmir", "avatar": "izmir.png" } ] } How can I fix this query? var filter = new BsonDocument("username", "bodrum"); var update = Builders<Person>.Update.Pull("followerList:follower",

Redirect output of mongo query to a csv file

流过昼夜 提交于 2019-11-26 22:30:58
问题 I am using MongoDB 2.2.2 for 32-bit Windows7 machine. I have a complex aggregation query in a .js file. I need to execute this file on the shell and direct the output to a CSV file. I ensure that the query returns a "flat" json (no nested keys), so it is inherently convertible to a neat csv. I know about load() and eval() . eval() requires me to paste the whole query into the shell and allows only printjson() inside the script, while I need csv. And, the second way: load() ..It prints the

Is there an “Explain Query” for MongoDB Linq?

若如初见. 提交于 2019-11-26 22:18:43
问题 Is there a way to run .explain() or equivalent on Linq queries? I would want to know The text of the actual JSON query The output of .explain() (indexes used, etc) It would also be nice to have the execution time of the query 回答1: You can get the Json easily enough if you have a query wrapper; var qLinq = Query<T>.Where(x => x.name=="jim"); Console.WriteLine(qLinq.ToJson()); There's also an Explain() method on MongoCursor, so you could do this; var exp = Collection.FindAs<T>(qLinq).Explain()

MongoDb use filter to match a list

痞子三分冷 提交于 2019-11-26 22:11:37
问题 I have a list of BsonDocument : var list = db.GetCollection<BsonDocument>(collectionName); var myIds = list.Find(_ => true) .Project(Builders<BsonDocument>.Projection.Include("_id")) .ToList(); that contains: myIds = "{ { "_id" : "cc9d9282-c9d2-4cba-a776-ffddsds274d5" }, { "_id" : "2c1ddd82-c9d2-4dda-afr6-d79ff1274d56" }, { "_id" : "ss969281-c9d2-4cba-a776-d79ffds274d5" } }" And want to query like this: var deleted =list.DeleteMany(Builders<MessageExchange>.Filter.In("_id", myIds)); I also

Custom deserialization

北城余情 提交于 2019-11-26 21:56:13
问题 I have collection with thousands of documents, in document there's field named Rate , problem is currently its type is string, so when it's not available, the old developer set it to "N/A". For now I want to change the type of this field to numeric in C# (set it to 0 when n/a), but if I do so I can't load the past data. Can we customize the deserialization so it will convert N/A to 0? 回答1: You need to create an IBsonSerializer or SerializerBase<> and attach it to the property you wish to

Convert MongoDB BsonDocument to valid JSON in C#

落花浮王杯 提交于 2019-11-26 21:33:54
问题 I am working with the MongoDB C# driver. I have a BsonDocument with some data which includes some MongoDB-specific types (like ObjectIDs and ISODates). I want to convert this to a valid general-purpose JSON string. In other words, I can't have something like _id: ObjectId(...) or date: ISODate(...) but would prefer _id: "..." and date: "..." . Basically, I want to convert these special types that only MongoDB recognizes to regular strings so they can be parsed more easily. The problem is that

MongoDB and C#: Case insensitive search

穿精又带淫゛_ 提交于 2019-11-26 20:19:41
问题 I am using MongoDB and the C# driver for MongoDB. I recently discovered that all queries in MongoDB are case-sensitive. How can I make a case-insensitive search? I found one way to do this: Query.Matches( "FirstName", BsonRegularExpression.Create(new Regex(searchKey,RegexOptions.IgnoreCase))); 回答1: The simplest and safest way to do that is using Linq : var names = namesCollection.AsQueryable().Where(name => name.FirstName.ToLower().Contains("hamster")); As explained in the tutorial ToLower ,

Upserting in Mongo DB using official C# driver

泄露秘密 提交于 2019-11-26 19:18:56
问题 In the official documentation of mongodb they mention upserts, so it would be really nice to write an upsert command instead of: if (_campaignRepo.Exists(camp)) { _campaignRepo.DeleteByIdAndSystemId(camp); } _campaignRepo.Save(camp); something which would implement that logic on the db level if it is possible. So what is the way to do an upsert if there is one? 回答1: The following code is from a working app: weekplanStore.Update( Query.EQ("weekNumber", week), Update.Replace(rawWeekPlan),