mongodb-.net-driver

How to insert data into a mongodb collection using the c# 2.0 driver?

一曲冷凌霜 提交于 2019-11-27 17:35:39
问题 I'm using the MongoClient in my c# console application to connect to MongoDB https://github.com/mongodb/mongo-csharp-driver/releases/tag/v2.0.0-rc0 My code class Program { static void Main(string[] args) { const string connectionString = "mongodb://localhost:27017"; // Create a MongoClient object by using the connection string var client = new MongoClient(connectionString); //Use the MongoClient to access the server var database = client.GetDatabase("test"); var collection = database

Redirect output of mongo query to a csv file

删除回忆录丶 提交于 2019-11-27 17:06:10
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 output on the screen, and again in json format. Is there a way Mongo can do this conversion from json to

MongoDB C# Driver 2.0 - Update document

吃可爱长大的小学妹 提交于 2019-11-27 13:30:30
I'm currently upgrading my code to MongoDB C# driver 2.0 and I'm having issues upgrading the code to update documents. using the old version I was able to do something like this: MyType myObject; // passed in var collection = _database.GetCollection<MyType>("myTypes"); var result = collection.Save(myObject); I'm struggling to find a way to do this in the new version. I have found a few examples of updating single fields like var filter = Builders<MyType>.Filter.Eq(s => s.Id, id); var update = Builders<MyType>.Update.Set(s => s.Description, description); var result = await collection

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

拈花ヽ惹草 提交于 2019-11-27 13:01:13
问题 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 ); 回答1: If you're using the latest version of the driver, which is 2.0.1 you can easily put that

How to create indexes in MongoDB via .NET

随声附和 提交于 2019-11-27 12:41:59
问题 I've programmatically created a new document collection using the MongoDB C# driver. At this point I want to create and build indexes programmatically. How can I do that? 回答1: Starting from v2.0 of the driver there's a new async -only API. The old API should no longer be used as it's a blocking facade over the new API and is deprecated. The currently recommended way to create an index is by calling and awaiting CreateOneAsync with an IndexKeysDefinition you get by using Builders.IndexKeys :

Storing Enums as strings in MongoDB

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 11:02:56
问题 Is there a way to store Enums as string names rather than ordinal values? Example: Imagine I've got this enum: public enum Gender { Female, Male } Now if some imaginary User exists with ... Gender gender = Gender.Male; ... it'll be stored in MongoDb database as { ... "Gender" : 1 ... } but i'd prefer something like this { ... "Gender" : "Male" ... } Is this possible? Custom mapping, reflection tricks, whatever. My context: I use strongly typed collections over POCO (well, I mark ARs and use

Truncate a collection

百般思念 提交于 2019-11-27 10:14:58
问题 How do I truncate a collection in MongoDB or is there such a thing? Right now I have to delete 6 large collections all at once and I'm stopping the server, deleting the database files and then recreating the database and the collections in it. Is there a way to delete the data and leave the collection as it is? The delete operation takes very long time. I have millions of entries in the collections. 回答1: You can efficiently drop all data and indexes for a collection with db.collection.drop().

Get _id of an inserted document in MongoDB?

点点圈 提交于 2019-11-27 07:43:43
问题 say I have a product listing. When I add a new product I save it using something like var doc=products.Insert<ProductPDO>(p); The problem is that I want after this is done to redirect the user to the page with the product. So I need to redirect to say /products/<ObjectID> However, I see no way of getting the ObjectID right afterwards without manually querying the database and look for a document with all the same fields and such. Is there an easier way? (also, doc in this instance returns

MongoDB .NET Driver Group By Time Range

落爺英雄遲暮 提交于 2019-11-27 06:31:51
问题 I am a noob in MongoDB and wanted to know how to count total documents inserted into the collection for every 15 minutes interval starting 12 AM UTC until the current UTC time. Below is a sample document { "_id" : ObjectId("5ade8bfc6b941c7726a54f01"), "Country" : "US" "Timestamp" : ISODate("2018-04-24T01:44:28.040Z"), } Here is the expected output: { "Count": 245, "ReceiveDateString": "5/2/2018 12:00:00 AM" }, { "Count": 239, "ReceiveDateString": "5/2/2018 12:15:00 AM" }, { "Count": 252,

How to use SetField in FindOne in MongoDB For C# Driver

假装没事ソ 提交于 2019-11-27 06:10:13
问题 I use offical C# Driver for mongodb, I want to use SetFields from a FindOne query like Find. var query = Query.EQ("Name", name); Users.Find(query).SetFields(Fields.Exclude("Password")); Is it possible to do that as FindOne return a actual class instead of mongodb cursor. 回答1: SetFields method of MongoCursor. Method FindOne just wrapper around MongoCursor and internally it looks so: public virtual TDocument FindOneAs<TDocument>() { return FindAllAs<TDocument>().SetLimit(1).FirstOrDefault(); }