mongodb-.net-driver

How to create indexes in MongoDB via .NET

谁都会走 提交于 2019-11-28 20:05:50
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? 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 : static async Task CreateIndex() { var client = new MongoClient(); var database = client.GetDatabase(

Truncate a collection

北城以北 提交于 2019-11-28 17:10:37
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. You can efficiently drop all data and indexes for a collection with db.collection.drop() . Dropping a collection with a large number of documents and/or indexes will be significantly more efficient

How to deserialize a BsonDocument object back to class

不想你离开。 提交于 2019-11-28 16:43:41
问题 How do I deserialize a BsonDocument object back to the class after getting it from the server? QueryDocument _document = new QueryDocument("key", "value"); MongoCursor<BsonDocument> _documentsReturned = _collection.FindAs<BsonDocument>(_document); foreach (BsonDocument _document1 in _documentsReturned) { //deserialize _document1 //? } Do I deserialize using a BsonReader? 回答1: There are three ways actually: 1.Specify type you want to load directly in FindAs<> var docs = _collection.FindAs

Can I do a text query with the mongodb c# driver

给你一囗甜甜゛ 提交于 2019-11-28 16:24:29
Is there a way to submit a query that is expressed in the shell query syntax to the mongo c# driver For example Something like Coll.find { "myrecs","$query : { x : 3, y : "abc" }, $orderby : { x : 1 } } "); To take an example from the shell guide Andrew Orsich There is no exact same functionality you want. But you can create BsonDocument from json for query: var jsonQuery = "{ x : 3, y : 'abc' }"; BsonDocument doc = MongoDB.Bson.Serialization .BsonSerializer.Deserialize<BsonDocument>(jsonQuery); And after that you can create query from BsonDocument: var query = new QueryComplete(doc); // or

.NET best practices for MongoDB connections?

喜夏-厌秋 提交于 2019-11-28 15:46:53
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 contrived. I thought I'd crank it up a notch to see how it works with multiple threads. I'm doing this because

Get _id of an inserted document in MongoDB?

扶醉桌前 提交于 2019-11-28 13:15:50
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 null for some reason) Scott Hernandez The Insert method automatically sets the property that is declared

Custom deserialization

自闭症网瘾萝莉.ら 提交于 2019-11-28 12:43:32
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? You need to create an IBsonSerializer or SerializerBase<> and attach it to the property you wish to serialize using the BsonSerializerAttribute . Something like the following: public class

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

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

a 夏天 提交于 2019-11-28 11:15:45
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. 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(); } If you want add Exclude Fields functionality to it you can simply add extention method for MongoCollection :

Mongo C# JSON reader was expecting a value but found 'replSetGetStatus'

半城伤御伤魂 提交于 2019-11-28 10:52:09
问题 I was unable to find the proper way to call shell command from Mongo C# driver version 2.7.2 public async Task RsStatus() { var res = await _admin.RunCommandAsync<object>("replSetGetStatus"); } Gives me an the error : JSON reader was expecting a value but found 'replSetGetStatus' I'm guessing this simply not the way to call shell methods. Can any one supply me with an example ? Thanks in advance. 回答1: db.adminCommand function expects and object to be passed as a parameter (here) so you can