mongodb-.net-driver

Retrieve Relevance Ordered Result from Text Query on MongoDB Collection using the C# Driver

天涯浪子 提交于 2019-12-07 15:29:25
I am attempting to text query a collection and retrieve the results in the text match order. The docs explain pretty well how to do this in the shell: db.articles.find( { status: "A", $text: { $search: "coffee cake" } }, { score: { $meta: "textScore" } } ).sort( { date: 1, score: { $meta: "textScore" } } ) but it requires the projection of the additional score field from the find into the sort. In C#, I have a function that looks like this: public IEnumerable<T> TextSearch<T>(MongoCollection<T> coll, string text) { var cursor = coll.Find(Query.Text(text)) .SetSortOrder(SortBy<T>.MetaTextScore(

How to filter based on nested array element in C# Mongodb strongly typed driver

≯℡__Kan透↙ 提交于 2019-12-07 14:32:14
问题 I am using the official C# MongoDb strongly typed driver version 2.7.0-beta001 to interact with MongoDB v 4.0-rc1 on Windows 10 machine. Consider the following classes: public class Library { [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } public DateTime DateAdded { get; set; } public DateTime LastModified { get; set; } public string Title { get; set; } public Author Author { get; set; } public bool AllBooks { get; set; } } public class Author { [BsonId]

Mongodb .net async await

半世苍凉 提交于 2019-12-07 12:32:30
问题 Does the mongodb .net driver offer support for async/await operations? I can't seem to find any info on this. I'm looking for something like EntityFramework has: ToListAsync(), FindAsync(), CountAsync() Is this supported? 回答1: 2.0 Driver is released. Check it out: https://github.com/mongodb/mongo-csharp-driver Nuget: https://www.nuget.org/packages/MongoDB.Driver 回答2: I've made some changes in the official driver to make it async as possible. Basically, I've changed the MongoConnection class

How can I tell the MongoDB C# driver to store all Guids in string format?

给你一囗甜甜゛ 提交于 2019-12-07 09:46:04
问题 I'm currently applying the [BsonRepresentation(BsonType.String)] attribute to all Guid properties in my domain models to have those properties serialized in string format. Besides being tiresome to do, that doesn't work out sometimes, e.g. with custom Wrapper<T> classes: public class Wrapper<T> { public T Value { get; set; } // Further properties / business logic ... } When T is Guid , the Value property will be stored as binary data of type UuidLegacy (as will any property of type Guid that

What is the C# equivalent of push and root for MongoDB?

こ雲淡風輕ζ 提交于 2019-12-07 07:02:40
问题 I have a collection of people. I am trying to find the oldest person for each name. I am able to achieve that result using the mongoDB console command db.People.aggregate([ { '$sort': { 'Name': 1, 'Age': -1 } }, { '$group': { '_id': '$Name', 'docs': { '$push': '$$ROOT' }, } }, { '$project': { 'top_one': { '$slice': ['$docs', 1] } } } ]) What is the equivalent of this for the C# driver? I'm specifically having trouble with 'docs': { '$push': '$$ROOT' }, Here is my current C# query: collection

How to create a user in MongoDB

为君一笑 提交于 2019-12-07 04:50:10
问题 I'm using the latest version of the driver and MongoDB database 2.6 and I used to create users using the following code: MongoUser _user1 = new MongoUser("username", "password", false); MongoDatabase.AddUser(_user1); and now it is saying MongoDatabase.AddUser() is deprecated by showing the following information: ...is obsolete: Use the new user management command 'createUser' or 'updateUser'." Where is this new user management command? How do I create a user using the new MongoDB C# driver?

Using SetFields with MongoDB C# driver 2.0

寵の児 提交于 2019-12-07 04:29:21
问题 With the old driver I could specify the fields I wanted to return from a query as follows: var cursor = Collection.Find(query). SetFields(Fields<MealPlan>.Exclude (plan => plan.Meals)); How do I accomplish this with the 2.0 driver? 回答1: You need to use the Projection method on IFindFluent (which is what Find and Projection return): var findFluent = Collection.Find(query).Projection(Fields<MealPlan>.Exclude (plan => plan.Meals)) Now, this would eventually generate a cursor of BsonDocument s

MongoDB C# connections/disconnections (Official driver)

南楼画角 提交于 2019-12-07 01:40:13
问题 Please tell me how I must connect/disconnect to MongoDB via official C# driver? Question is simple and problem is trivial for first look, but: 1) Do I need to call Disconnect method by myself, or it will be closed by some method like Dispose? 2) Do I need to Connect every time when I need to make request to Mongo? Or it will be better to keep Connection? 3) Is method Reconnect are useful? Do you use it? Any additional advice? Thank you very much!!! Update: My question is about the life cycle

C# : Retrieve array values from bson document

孤者浪人 提交于 2019-12-07 00:50:01
问题 In my MongoDB collection, I have a document with an array entry. How do I get these array values as a string array in C#? I can get the document itself back fine but I can't seem to get the array values. This is where I'm up to : QueryDocument findUser = new QueryDocument("_id" , id); BsonDocument user = bsonCollection.FindOne(findUser); So in this user document, there is an array that I'd like to get and parse into a string array. The document looks something like this : { "firstname" : "jon

Maintain Id property name in embedded doc with mongo C# driver

試著忘記壹切 提交于 2019-12-06 23:36:52
问题 I have a mongo document that contains an array of embedded documents. The embedded documents have a property named "Id". { Name: "Outer object", Embedded: [ {Name: "Embedded A", Id: "5f1c591a71dc237199eeaeda"} ] } My C# mapping objects look something like this (a simplification, obviously) public class Outer { public string Name { get; set; } public IEnumerable<Inner> Inners { get; set; } } public class Inner { public string Name { get; set; } public string Id { get; set; } } When I write an