mongodb-.net-driver

Unable to determine the serialization information for *expression* using .Date

岁酱吖の 提交于 2019-12-11 06:43:50
问题 I'm trying to read from a mongoDB collection with a Date filter (only the Date, time should be ignored): var filterDefinition = Builders<SomeClass>.Filter.Eq(p => p.SomeDateTimeProperty.Date, DateTime.Now.Date); using (var cursor = await _someCollection.FindAsync(filterDefinition)) { ... } SomeClass and the SomeDateTimeProperty property look like this: [BsonIgnoreExtraElements] [Serializable] public class ReportConfiguration { ... public DateTime SomeDateTimeProperty { get; set; } ... } The

MongoDB: Case insensitive and accent insensitive

半腔热情 提交于 2019-12-11 06:07:33
问题 I am looking for string "JESÚS" but only returns the document with the specified string, I need the search to ignore the accents and capital letters. I am using C# and mongodb driver. I have two documents saved in my mongodb: _id:5d265f3129ea36365c7ca587 TRABAJADOR:"JESUS HERNANDEZ DIAZ" _id:5d265f01db86a83148404711 TRABAJADOR:"JESÚS HERNÁNDEZ DÍAZ" In visual c# with mongo driver: var filter = Builders<BsonDocument>.Filter.Regex("TRABAJADOR", new BsonRegularExpression(string.Format(".*{0}.*",

Serialize get-only properties on MongoDb

送分小仙女□ 提交于 2019-12-11 05:42:23
问题 With C# 6 I can write: public class Person { public Guid Id { get; } public string Name { get; } public Person(Guid id, string name) { Id = id; Name = name; } } Unfortunately a class like this is not serialized correctly by MongoDb driver, properties are not serialized. MongoDb only serialize by default properties with getter and setter. I known that you can manually change the class mapping and tell serializer to include get-only properties but I was looking for a generic way to avoid

How to $lookup with pipeline & let parameters in C# (MongoDB.Driver 2.7.2)

℡╲_俬逩灬. 提交于 2019-12-11 05:13:47
问题 I need to run the following query in MongoDB in my C# code. I use MongoDB.Driver 2.7.2 and MongoDB 4.0. I would like to use $lookup rather than $project / $unwind in order to prevent naming each one of the collection's possible fields. db.getCollection('Collection1').aggregate([ { "$match" : { "Id" : 1 } }, { "$lookup": { "from": "Collection2", "let": { collection1Id : "$Id" }, "pipeline": [ { $match: { $expr: { $and: [ { $eq : [ "$Collection1Id", "$$collection2Id" ] }, { $in : [ "$_id" , [1,

Ignoring Properties inside Composite Property with BsonIgnore

好久不见. 提交于 2019-12-11 04:48:33
问题 I uses the below to code to ignore some property inside a class using BsonIgnore . But it is ignoring the total object. public class User { public string Username { get; set; } public string Password { get; set; } [BsonIgnore,JsonProperty(PropertyName = "CreateDate")] public ICollection<Role> Roles { get; set; } } public class Role { public int RoleId {get; set;} public string RoleName { get; set; } public DateTime CreateDate { get; set;} } I have 2 question. How to ignore only certain

Multiple Logical Conditions in MongoDB Select

假装没事ソ 提交于 2019-12-11 04:36:03
问题 I need to Query a Collection based on Multiple Condition My Collection { "_id" : ObjectId("575f4e2efd14481598fc0ebf"), "Emp_ID" : "100", "LastUpdate" : ISODate("2016-06-13T18:30:00.000Z") }, { "_id" : ObjectId("575f4e2efd14481598fc0ebf"), "Emp_ID" : "101", "LastUpdate" : ISODate("2016-06-14T06:33:00.000Z") }, ... () Now I need to Check the document is exist or not based on my below condition My C# Code: private static IMongoClient _client; private static IMongoDatabase _database; _client =

MongoDB C# Upsert with Guid

杀马特。学长 韩版系。学妹 提交于 2019-12-11 04:28:39
问题 When attempting to perform an upsert operation in Mongo, I'd like to have it generate a GUID for the ID instead of an Object ID. In this case, I'm checking to make sure an object with specific properties doesn't already exist and actually throwing an exception if the update occurs. Here's a stub of the class definition: public class Event { [BsonId(IdGenerator = typeof(GuidGenerator) )] [BsonRepresentation(BsonType.String)] [BsonIgnoreIfDefault] public Guid Id { get; set; } // ... more

Mongo C# driver update a specific element in a nested array

*爱你&永不变心* 提交于 2019-12-11 04:03:15
问题 How do I modify in Mongo (C# driver) a single element in a nested property (array) without retrieving the whole document? public class Element { public int Value {get; set;} public string Name {get; set;} } public class Document { public Element [] Elements {get; set;} } In example I want to find the element with name "Car" and sets its value to 4 in a single query. 回答1: You need $ positional operator where you can specify document-level condition and array-level condition to find single

Ambiguous discriminator 'myType' when 'myType' is generated dynamically at run time

烂漫一生 提交于 2019-12-11 03:43:18
问题 I have an application which creates new types dynamically at run time, creates objects of that type and inserts them into a MongoDB database collection of type object . Using the shell I can see that the object is inserted correctly and the _t value is the correct name of the dynamically created class. I am trying to retrieve objects from my collection using AsQueryable while applying a LINQ query to filter the results to only objects of a specific type. This works fine: _collection

Mongodb C# driver - can't use admin authentication to access other databases

被刻印的时光 ゝ 提交于 2019-12-11 03:42:03
问题 When you login as admin user (user of admin database) in MongoDB console, you can then have full access to any database on the same server without need to login to each of them. But, if I use the official C# driver and provide connection string to admin database, then when I am trying to access any other database, I get authentication error. It's happening in MongoServerInstance.AcquireConnection() where it's trying to re-authenticate the connection and checks admin database credentials