mongodb-.net-driver

C# mongo queries with json strings

随声附和 提交于 2019-12-17 23:43:18
问题 This seems so basic that I'm sure I've just overlooked a class or a method somewhere, but for the life of me, I can't find it. I've got a json string like so: { SendId: 4, "Events.Code" : { $all : [2], $nin : [3] } } I can run this in the mongo shell against a find() or a count() and get what I'm looking for. What is the easiest way to deal with this in C#? Here's what I've found: The methods I'm finding are all wanting an IMongoQuery , which is just a marker interface BsonDocument has a nice

Get only a specified field in MongoDB with C#

跟風遠走 提交于 2019-12-17 23:06:52
问题 first time i'm using MongoDB. I have read this example: SELECT a,b FROM users WHERE age=33 db.users.find({age:33}, {a:1,b:1}) But I can't translate it into C#. Can anyone help me? 回答1: You can do it using SetFields method of MongoCursor class, below full example: var server = MongoServer.Create(connectionString); var db = _server.GetDatabase("dbName"); db.GetCollection("users"); var cursor = Photos.FindAs<DocType>(Query.EQ("age", 33)); cursor.SetFields(Fields.Include("a", "b")); var items =

.NET best practices for MongoDB connections?

五迷三道 提交于 2019-12-17 21:40:31
问题 I've been playing with MongoDB recently (It's AMAZINGLY FAST) using the C# driver on GitHub. Everything is working just fine in my little single threaded console app that I'm testing with. I'm able to add 1,000,000 documents (yes, million) in under 8 seconds running single threaded. I only get this performance if I use the connection outside the scope of a for loop. In other words, I'm keeping the connection open for each insert rather than connecting for each insert. Obviously that's

Get generated script in MongoDB C# driver

烂漫一生 提交于 2019-12-17 19:46:10
问题 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? 回答1: 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

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

百般思念 提交于 2019-12-17 18:35:11
问题 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 =

MongoDB C# Driver - Ignore fields on binding

半世苍凉 提交于 2019-12-17 15:35:31
问题 When using a FindOne() using MongoDB and C#, is there a way to ignore fields not found in the object? EG, example model. public class UserModel { public ObjectId id { get; set; } public string Email { get; set; } } Now we also store a password in the MongoDB collection, but do not want to bind it to out object above. When we do a Get like so, var query = Query<UserModel>.EQ(e => e.Email, model.Email); var entity = usersCollection.FindOne(query); We get the following error Element 'Password'

How do I get the _id of the rcently inserted document after an insert using mongo csharp?

為{幸葍}努か 提交于 2019-12-14 04:17:38
问题 I was able to successfully insert a new document using the following code but I was not able to get the _id of the newly inserted document. After the insert, user is null. Thank you! MongoServer server = MongoServer.Create(); MongoDatabase test = server.GetDatabase("Test"); MongoCollection<BsonDocument> users = test.GetCollection("Users"); BsonDocument user = new BsonDocument(); user.Add("FirstName", "John"); user.Add("LastName", "Michael"); user.Add("Address", "123 Main Street"); user.Add(

MongoDB C# Driver Create Index

こ雲淡風輕ζ 提交于 2019-12-14 03:48:32
问题 I just updated my MongoDB from version 2.5.0 to 2.7.0. Visual Studio tells me that the following way to create an index is obsolete: protected override Task OnPerformMaintenanceAsync(CancellationToken cancellationToken) => NotificationLogs.Indexes.CreateOneAsync(Builders<NotificationLog>.IndexKeys.Ascending(_ => _.TimestampUtc)); It suggests me to use CreateIndexModel. The only problem is that I cannot find an example to get this working that will do the same. I tried: protected Task

How to retrieve values from an array in MongoDB using C#

霸气de小男生 提交于 2019-12-14 03:13:33
问题 Below in the code that retrieves the elements in the form of a BsonArray. I just want to fetch the numeric value from the array and use that value to calculate the sum. var fields = "secondary.amount"; foreach (var document in collection.FindAllAs<BsonDocument>().SetFields(fields)) { foreach (string name in document.Names) { BsonElement element = document.GetElement(name); Console.WriteLine("{0}", element.Value); } } I tried converting the bson element to an int64, int32, double and then use

Possible to do partial update, but complete document upsert in mongodb?

寵の児 提交于 2019-12-14 03:04:30
问题 Using mongoDB, I want to update a specific field conditionally (update field if existing value is less than update). However, if the document doesn't exist, I want the whole document to be inserted. What previously worked: I have previously used a combination of $max (to set the field) and $setOnInsert (to set the rest of the document), which did what i wanted. This prevented future updates from decrementing the given field. For example, having the following document schema: { _id: [ObjectId]