mongodb-.net-driver

Mongodb collection as dynamic

安稳与你 提交于 2021-01-28 14:20:38
问题 I have an application that has two similar but different objects and I want to store those objects in the same collection. What is the best way to do this? And how can I query this collection? Today my collections is represented by: public IMongoCollection<Post> Posts { get { return _database.GetCollection<Post>("posts"); } } And I have this class: public class Post { public string Id { get; set; } public string Message { get; set; } } public class NewTypePost { public string Id { get; set; }

MongoDB NodeJS Native Driver(mongodb) vs Mongo Shell Performance

一世执手 提交于 2021-01-28 11:49:31
问题 I have 10000 records in MongoDB in table1. Data is as below: "_id" : ObjectId("5d5e500cb89312272cfe51fc"), "cities" : [ { "cityid" : "5d5d2205cdd42d1cf0a92b33", "value" : "XYZ" }, { "cityid" : "5d5d2214cdd42d1cf0a92b34", "value" : "Rowcliffe" }, ], Query is as below: { $unwind: "$cities" }, { "$addFields": { "cities.cityid": { "$toObjectId": "$cities.cityid" } } }, { $lookup: { from: "cities", localField: "cities.cityid", foreignField: "_id", as: "docs" } }, So, here i lookup cityid in

Updating an object property in a nested collection with another property of this object in MongoDB with c# driver

只愿长相守 提交于 2021-01-28 10:00:12
问题 I need to update property in nested collection. All examples describes about setting concrete value, but not about value from another field. MongoDB version: 4.2 C# driver version: 2.10.4 Product example: { _id: "1", Title: "Some title", Price: 20, OriginalPrice: 10, ... other props ... Variants: [ { Price: 21, OriginalPrice: 11 ... other props ... }, { Price: 22, OriginalPrice: 13 ... other props ... }, ] } I know how to update Price var pipeline = PipelineDefinition<Product, Product>.Create

Use LINQ in C# to find MondoDB records when values in a list field match a criteria value from a list

对着背影说爱祢 提交于 2021-01-28 08:47:03
问题 I want to use LINQ to return all records in a MongoDB collection where the field in the record is a list of strings and any string in the list matches any string value in a list of strings used as the search criteria: Mongo Record in Collection ("Item"): { "_id": ..., "StringList": [ "string1", "string2", "string3" ], ... } Search Criteria: var criteria = new List<string> { "string2", "string4" }; My Code: var foundItems = iMongoDataProvider.FindAll<Item>() .Where(x =>x.StringList.ContainsAny

Convert Newtosoft JObject directly to BsonDocument

家住魔仙堡 提交于 2021-01-27 19:45:12
问题 Try to convert JObject to BsonDocument using this example https://www.newtonsoft.com/json/help/html/WriteJTokenToBson.htm ( BsonWriter obsolete, so I use BsonDataWriter ) var jObject = JObject.Parse("{\"name\":\"value\"}"); using var writer = new BsonDataWriter(new MemoryStream()); jObject.WriteTo(writer); var bsonData = writer.ToBsonDocument(); Console.WriteLine(bsonData.ToJson()); output: { "CloseOutput" : true, "AutoCompleteOnClose" : true, "Formatting" : 0, "DateFormatHandling" : 0,

Convert Newtosoft JObject directly to BsonDocument

大城市里の小女人 提交于 2021-01-27 19:20:46
问题 Try to convert JObject to BsonDocument using this example https://www.newtonsoft.com/json/help/html/WriteJTokenToBson.htm ( BsonWriter obsolete, so I use BsonDataWriter ) var jObject = JObject.Parse("{\"name\":\"value\"}"); using var writer = new BsonDataWriter(new MemoryStream()); jObject.WriteTo(writer); var bsonData = writer.ToBsonDocument(); Console.WriteLine(bsonData.ToJson()); output: { "CloseOutput" : true, "AutoCompleteOnClose" : true, "Formatting" : 0, "DateFormatHandling" : 0,

Best way to create a text index on a MongoDB collection via C# driver

丶灬走出姿态 提交于 2021-01-27 07:55:03
问题 Using v1.9.0 of the C# driver (latest at time of writing) with MongoDB 2.6.0 What is the best way currently to create a text index on a collection, via the C# driver? From what I could tell, it's not possible via MongoCollection.CreateIndex ? So currently creating it using MongoDatabase.Eval like so: Database.Eval(new EvalArgs { Code = "function(){db.dummycollection.ensureIndex({\"$**\" : \"text\"},{name:\"TextIndex\"});}" Am I missing something / is there a better way? 回答1: This should work:

Best way to create a text index on a MongoDB collection via C# driver

只愿长相守 提交于 2021-01-27 07:52:49
问题 Using v1.9.0 of the C# driver (latest at time of writing) with MongoDB 2.6.0 What is the best way currently to create a text index on a collection, via the C# driver? From what I could tell, it's not possible via MongoCollection.CreateIndex ? So currently creating it using MongoDatabase.Eval like so: Database.Eval(new EvalArgs { Code = "function(){db.dummycollection.ensureIndex({\"$**\" : \"text\"},{name:\"TextIndex\"});}" Am I missing something / is there a better way? 回答1: This should work:

How to convert string into ObjectId

天涯浪子 提交于 2020-12-02 07:10:06
问题 I am getting data from MongoDB and binding to a WPF datagrid. My code selects multiple rows, retrieves IDs and updates the selected records: var server = MongoServer.Create(this.connectionString); var db = server.GetDatabase(DATABASE); var viewTrue = db.GetCollection(RISKALERT_TBL); var count = viewTrue.Count(); foreach (RiskSettings row in grdRiskAlerts.SelectedItems) { viewTrue.Update(Query.EQ("ID",row.ID), Update.Set("View", "False")); LoadandBindData(); } But it does not update the record

How to convert string into ObjectId

一曲冷凌霜 提交于 2020-12-02 07:09:05
问题 I am getting data from MongoDB and binding to a WPF datagrid. My code selects multiple rows, retrieves IDs and updates the selected records: var server = MongoServer.Create(this.connectionString); var db = server.GetDatabase(DATABASE); var viewTrue = db.GetCollection(RISKALERT_TBL); var count = viewTrue.Count(); foreach (RiskSettings row in grdRiskAlerts.SelectedItems) { viewTrue.Update(Query.EQ("ID",row.ID), Update.Set("View", "False")); LoadandBindData(); } But it does not update the record