MongoDB C# Driver 2.0 – Update document
I think you’re looking for ReplaceOneAsync(): MyType myObject; // passed in var filter = Builders<MyType>.Filter.Eq(s => s.Id, id); var result = await collection.ReplaceOneAsync(filter, myObject)
I think you’re looking for ReplaceOneAsync(): MyType myObject; // passed in var filter = Builders<MyType>.Filter.Eq(s => s.Id, id); var result = await collection.ReplaceOneAsync(filter, myObject)
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, ToLowerInvariant, ToUpper and ToUpperInvariant all perform matches in a case insensitive way. After that you can use all the supported string methods like Contains or StartsWith. This example will generate: { “FirstName” … Read more
You can use a combination of sort and limit to emulate min: > db.foo.insert({a: 1}) > db.foo.insert({a: 2}) > db.foo.insert({a: 3}) > db.foo.find().sort({a: 1}).limit(1) { “_id” : ObjectId(“4df8d4a5957c623adae2ab7e”), “a” : 1 } sort({a: 1}) is an ascending (minimum-first) sort on the a field, and we then only return the first document, which will be the … Read more
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() Console.WriteLine(exp.ToJson()); So if you want the time taken, “millis” is in there; var msTaken = exp.First(x => x.Name == … Read more
MongoDB.Bson (2.5+) has support to map between BsonValues and .Net objects. BsonTypeMapper Class To map a BsonValue (or BsonDocument) to .Net object use var dotNetObj = BsonTypeMapper.MapToDotNetValue(bsonDoc); You can then use your choice of serialization library. For example, JsonConvert.SerializeObject(dotNetObj); If you have a List of BsonDocument var dotNetObjList = bsonDocList.ConvertAll(BsonTypeMapper.MapToDotNetValue);
This post may help: I figured it out. This JIRA ticket has the details. Effectively, we’ve made a distinction between connecting to a standalone server and connecting directly to a replica set member, where the latter is relatively uncommon. Unfortunately, MongoLab’s Single-Node settings are actually a single node replica set and this causes us to … Read more
When using a filter to remove array elements, you need to use the PullFilter builder instead of Pull (which matches whole elements). var collection = db.GetCollection<Person>(“people”); var filter = new BsonDocument(“username”, “bodrum”); var update = Builders<Person>.Update.PullFilter(“followerList”, Builders<Follower>.Filter.Eq(“follower”, “fethiye”)); var result = collection.FindOneAndUpdateAsync(filter, update).Result; Or somewhat more succinctly, using lambdas: var update = Builders<Person>.Update.PullFilter(p => p.followerList, … Read more
There is no need to parse the JSON. Everything here can actually be done directly with either LINQ or the Aggregate Fluent interfaces. Just using some demonstration classes because the question does not really give much to go on. Setup Basically we have two collections here, being entities { “_id” : ObjectId(“5b08ceb40a8a7614c70a5710”), “name” : “A” … Read more
Following example show how to save file and read back from gridfs(using official mongodb driver): var server = MongoServer.Create(“mongodb://localhost:27020”); var database = server.GetDatabase(“tesdb”); var fileName = “D:\\Untitled.png”; var newFileName = “D:\\new_Untitled.png”; using (var fs = new FileStream(fileName, FileMode.Open)) { var gridFsInfo = database.GridFS.Upload(fs, fileName); var fileId = gridFsInfo.Id; ObjectId oid= new ObjectId(fileId); var file = … Read more
MongoDB used a process wide write lock to guarantee that only one write operation (update/insert/remove) can be performed at a time. As such it automatically solves concurrency issues since write concurrency simply isn’t allowed. If 4 threads attempt an update operation one of them will take the write lock, do its update and release the … Read more