mongodb-.net-driver

How do I get the last item in a MongoDB collection?

只谈情不闲聊 提交于 2020-01-14 09:47:08
问题 I'm using MongoDB for sometime to perform all sort of fast inserts or having as a log, but I'm having some trouble to get a really simple query How, in Mongo, would I do to get a similiar to this T-SQL SELECT TOP 1 [date] FROM [Collection] ORDER BY [date] desc In other words, what is the last date in the collection. I'm trying to use FindOne or any other that can return one document, but none accepts a sortBy property... how would I do this? var query = Query.EQ("status", "pending"); var

Use string variable in LINQ lambda expression

风流意气都作罢 提交于 2020-01-13 20:30:11
问题 I have a class property name as string variable and want to use that in LINQ query. Below example: public class Demo { public string prop1 {get; set;} public string prop2 {get; set;} public string prop3 {get; set;} } I can do this var data = db.Single<Demo>(d => d.prop1 == "value"); But don't know what's the property is at runtime and getting that string parameter like string propname = "prop2"; Is there any possibility to use that in lambda expression d => d.propname == "value" ? I am not

Element 'Id' does not match any field or property of class

旧时模样 提交于 2020-01-12 11:52:10
问题 I got the result from the collection in MongoDB, the structure is the same as below [DataContract] public class Father { [BsonId] [DataMember] public MongoDB.Bson.ObjectId _id { get; set; } [DataMember] public string Id { get; set; } [DataMember] public List<Child> childs { get; set; } } [DataContract] public class Child { [DataMember] public string Id { get; set; } [DataMember] public int Name { get; set; } } When I try this: List<Father> f = result.ToList(); It calls Element 'Id' does not

Element 'Id' does not match any field or property of class

随声附和 提交于 2020-01-12 11:50:34
问题 I got the result from the collection in MongoDB, the structure is the same as below [DataContract] public class Father { [BsonId] [DataMember] public MongoDB.Bson.ObjectId _id { get; set; } [DataMember] public string Id { get; set; } [DataMember] public List<Child> childs { get; set; } } [DataContract] public class Child { [DataMember] public string Id { get; set; } [DataMember] public int Name { get; set; } } When I try this: List<Father> f = result.ToList(); It calls Element 'Id' does not

Understanding the changes in MongoDB new C# driver (Async and Await)

陌路散爱 提交于 2020-01-12 05:09:07
问题 The new C# driver is totally Async and in my understanding twists a little bit the old design patterns such as DAL in n-tier architecture. In my Mongo DALs I use to do: public T Insert(T entity){ _collection.Insert(entity); return entity; } This way I can get the persisted ObjectId . Today, everything is Async such as InsertOneAsync . How would Insert method will now return the entity when the InsertOneAsync will be done? Can you show an example? 回答1: It's helpful to understand the basics of

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

情到浓时终转凉″ 提交于 2020-01-09 12:17:13
问题 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 回答1: 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

Getting the first document of a cursor in MongoDb + C#

感情迁移 提交于 2020-01-06 15:17:43
问题 The whole problem is like so: Having a collection of documents in MongoDb, find the first one according to a query and an order. Since FindOne in MongoDb does not accept an order, the way to do so is to return a Cursor with a Limit of one. It is done in C# like so: var query = Query<Doc>.EQ(e => e.Deleted, false); var sortBy = SortBy<Doc>.Ascending(e => e.Date); var cur = colletion.FindAs<Doc>(query).SetSortOrder(sortBy).SetLimit(1); Then, somehow, I need to take the found document out of the

Using both $in and $elemMatch using the C# Driver

岁酱吖の 提交于 2020-01-06 12:53:54
问题 I'm having a hard time translating a working MongoDB query to the C# driver's untyped equivalent. The query: { "Field" : { "$elemMatch" : { "$in" : ["Hamster"]}} } What I have: Query.ElemMatch("Field", Query.In("", new BsonArray(new[] { "Hamster" }))); Which generates: { "Field" : { "$elemMatch" : { "" : { "$in" : ["Hamster"] }}} } That's pretty close but i can't figure out how to remove the name from the $in query. 回答1: It seems like it should be possible, but the way the helper methods are

How Do I Add Data To Mongo Db Synchronously?

若如初见. 提交于 2020-01-05 07:59:28
问题 How do i add data to MOngo Db synchronously ? Is it a good idea to use asynchronously method to add user data in server ? I have user registration form, when user click on create button it should add user data to Mongo db collection. But in Mongo db site they have explained asynchronous methods to insert data using c# driver. But i want to insert it synchronously. 1. User click on create button 2. Insert data to Mongo DB 3. Show success message to user. here in step 2, if it is asynchronous,

Serialize Dictionary<long, VALUE> to BSON documents

≡放荡痞女 提交于 2020-01-05 02:50:38
问题 I want to serialize Dictionary<long, VALUE> to following JSON in MongoDB. { "213" : {}, "63624" : {}, ... } I don't want other DictionaryRepresentation except DictionaryRepresentation.Document.Document I am using MongoDB C# Driver (v2.0.1.27), and it is not smart to convert the long type key into string , which causes an exception. Thank you 回答1: You can do this with the existing serializers but it requires a small amount of configuration. Assume the following class: public class C { public