mongodb-csharp-2.0

How to do an upsert with MongoDB 2.0?

不问归期 提交于 2019-11-30 18:53:37
The interface of MongoDB has completely changed from the previous one. Here you can see the official documentation with some examples about how to search, insert and update but what about upserts? Idea for meta: I've tried to search on google and on SO but many resources refer to the old interface. Maybe it would be nice to create a MongoLegacy tag. Pass an instance of UpdateOptions as the options parameter in UpdateOneAsync(filter, update, options) , e.g.: collection.UpdateOneAsync(p => p.Id == user.Id, Builders<User>.Update.Set(p => p.Name, "John"), new UpdateOptions { IsUpsert = true });

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 do an upsert with MongoDB 2.0?

怎甘沉沦 提交于 2019-11-30 01:56:53
问题 The interface of MongoDB has completely changed from the previous one. Here you can see the official documentation with some examples about how to search, insert and update but what about upserts? Idea for meta: I've tried to search on google and on SO but many resources refer to the old interface. Maybe it would be nice to create a MongoLegacy tag. 回答1: Pass an instance of UpdateOptions as the options parameter in UpdateOneAsync(filter, update, options) , e.g.: collection.UpdateOneAsync(p =>

C# MongoDB.Driver GetServer is Gone, What Now?

爱⌒轻易说出口 提交于 2019-11-29 03:51:21
From the mongoDB.Driver docs ( http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/ ) Get a Reference to a Server Object To get a reference to a server object from the client object, write this: var server = client.GetServer(); In the latest release the GetServer method is gone, but the doc have not been updated, what do we use now? Thanks for your time. GetServer is part of the old API. To use the new, shiny and async -ready API simply call GetDatabase directly on the client to get an IMongoDatabase and GetCollection on it to get an IMongoCollection : var db = client

Translate FilterDefinition<TDocument> to regular json mongo query that i can run in a mongo shell

耗尽温柔 提交于 2019-11-28 20:37:49
I have many complex queries that I sometimes wish to check directly against Mongo for debugging \ explaining() purposes. With the newer 2.0+ c# driver, i'm not sure how to do this. With the previous version there was a thing called IMongoQuery and This worked. A simple example: FilterDefinition<LalalaEvent> filter = Builders<LalalaEvent>.Filter .Where(e => ids.Contains(e.Id) && e.Deleted != true ); If you're using the latest version of the driver, which is 2.0.1 you can easily put that filter in a Find operation, get back an IFindFluent and print its ToString : var filter = Builders

Get generated script in MongoDB C# driver

只愿长相守 提交于 2019-11-28 11:31:43
I am using MongoDB.Driver 2.0.0. Is there any way to see a generated script from linq to MongoDB? For example my query is like: IFindFluent<ProductMapping, ProductMapping> findFluent = Collection.Find( x => hashValues.Contains(x.UrlHash) && x.ProductTopic == topicId); How would this (or more complex queries) be represented in the MongoDB shell? EDIT Please see i3arnon's answer for a client-side method using Render() that is usually easier. You can use the integrated mongodb profiler to see what the database has actually received: db.setProfilingLevel(2); // log every request // show the

BsonSerializationException when serializing a Dictionary<DateTime,T> to BSON

心已入冬 提交于 2019-11-28 08:19:58
I've recently moved to the new MongoDB C# driver v2.0 from the deprecated v1.9 . Now, when I serialize a class that has a dictionary I sometimes run into the following BsonSerializationException : MongoDB.Bson.BsonSerializationException: When using DictionaryRepresentation.Document key values must serialize as strings. Here's a minimal reproduce: class Hamster { public ObjectId Id { get; private set; } public Dictionary<DateTime,int> Dictionary { get; private set; } public Hamster() { Id = ObjectId.GenerateNewId(); Dictionary = new Dictionary<DateTime, int>(); Dictionary[DateTime.UtcNow] = 0;

Difference between Find and FindAsync

本小妞迷上赌 提交于 2019-11-28 08:12:20
I am writing a very, very simple query which just gets a document from a collection according to its unique Id. After some frusteration (I am new to mongo and the async / await programming model), I figured this out: IMongoCollection<TModel> collection = // ... FindOptions<TModel> options = new FindOptions<TModel> { Limit = 1 }; IAsyncCursor<TModel> task = await collection.FindAsync(x => x.Id.Equals(id), options); List<TModel> list = await task.ToListAsync(); TModel result = list.FirstOrDefault(); return result; It works, great! But I keep seeing references to a "Find" method, and I worked

How do I log my queries in MongoDB C# Driver 2.0?

若如初见. 提交于 2019-11-28 07:58:27
问题 Just upgraded my application to the latest stable MongoDB C# Driver 2.0. During the migration, basic functionality has been broken and even the simplest query like: this.collection.Find(e => e.Id == id).SingleOrDefaultAsync() doesn't return the correct data. Checked the class mappings and conventions but I would like to see the output query in order to properly identify the issue. So, how should it be done on the MongoClient side? Setting profiling on the database level is possible but not a

C# MongoDB.Driver GetServer is Gone, What Now?

末鹿安然 提交于 2019-11-27 22:21:15
问题 From the mongoDB.Driver docs (http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/) Get a Reference to a Server Object To get a reference to a server object from the client object, write this: var server = client.GetServer(); In the latest release the GetServer method is gone, but the doc have not been updated, what do we use now? Thanks for your time. 回答1: GetServer is part of the old API. To use the new, shiny and async -ready API simply call GetDatabase directly