Tag: mongodb

如何在数组.NET驱动程序中的项的属性上创建MongoDB MultiKey索引

我有一个包含项目的MongoDB集合“foos”,每个项目都有一个“bars”数组。 也就是说,“foo”具有以下架构: { “id”: UUID “name”: string … “bars”: [ “id”: UUID “key”: string … ] } 我需要使用MongoDB C#.NET Mongo驱动程序在name和bar.key上创建索引。 我假设我可以使用Linq Select函数执行此操作,如下所示: Indexes.Add(Context.Collection().Indexes.CreateOne( Builders.IndexKeys .Descending(x => x.Bars.Select(y => y.Key)))); 但是,这会导致InvalidOperationException: System.InvalidOperationException:’无法确定x => x.Bars.Select(y => y.Id)的序列化信息。 关于MultiKey索引的Mongo文档显示了如何使用简单的点表示法创建这样的索引,即 db.foos.createIndex( { “name”: 1, “bars.key”: 1 } ) 但是,MongoDB驱动程序文档似乎表明我正在使用Linq函数是正确的。 如何使用MongoDB .NET驱动程序在我的集合上创建多键索引,最好使用Linq函数?

更新mongodb文档中的特定字段

我使用C#驱动程序在小项目中使用MongoDb,现在我坚持更新文档。 试图弄清楚如何更新字段AVG (int) 这是我的代码: IMongoCollection studentCollection = db.GetCollection(“studentV1”); Student updatedStudent = new Student() { AVG = 100, FirstName = “Shmulik” }); studentCollection.UpdateOne( o=>o.FirstName == student.FirstName, ****); 有一种简单而干净的方法来更新特定字段,如方法ReplaceOne(updatedStudent) ?

C#MongoDB驱动程序忽略超时选项

我们正在使用Mongo DB的C#驱动程序(1.9.1)。 如果DB不可访问,我们有一些需要运行的回退逻辑,但是默认超时太长。 我们试图改变它,但我们放置的值被忽略了。 对于测试,我们使用的是无响应机器的IP。 我们尝试在连接字符串中设置超时: 或者通过代码: var client = new MongoClient(new MongoClientSettings { Server = new MongoServerAddress(“xxx.xxx.xxx.xxx”), SocketTimeout = new TimeSpan(0, 0, 0, 2), WaitQueueTimeout = new TimeSpan(0, 0, 0, 2), ConnectTimeout = new TimeSpan(0, 0, 0, 2) }); 两次请求在平均约20秒后超时。 我们设置超时选项的方式可能有什么问题。

在聚合框架中使用Facets C#with Multiple Facet,Unwind和sortByCount

如何在C#中表示此管道? —–Id —–Name —–ProductAttributes (object array) | —–ProductAttributeType —–ProductAttributeValues (string array) 我的collections: { “_id” : ObjectId(“5b41a5e225cd892c14628b78”), “Name” : “Esmalte Super Pérola Fashion Glamour”, “ProductAttributes” : [ { “ProductAttributeType” : “Coleção”, “Values” : [ “Fashion” ] }, { “ProductAttributeType” : “Tamanho”, “Values” : [ “8 ml” ] }, { “ProductAttributeType” : “Tom”, “Values” : [ “Vermelho”, […]

MongoDB中存储的javaScript的C#调用不是比使用MongoDB C#驱动程序查询API更高效,更好吗?

所有: 以下是有关我的开发环境的信息: MongoDB 3.0.0 MongoDB C#驱动程序版本1.7.0.4714 Microsoft Visual Studio Professional 2013 .NET Framework 4.0 使用MongoDB C#驱动程序版本1.7.0.4714 API,我可以使用IMongoQuery和Query对象构建查询, 但是,当查询涉及MongoDB集合之间的大量“准连接”时,我发现很难使用MongoDB C#驱动程序版本1.7.0.4714 API实现查询。 此外,如果您将MongoDB世界中的大量数据作为POCO对象列入C#world,我相信它会降低性能。 所以,我在考虑采用使用C#调用MongoDB中存储的JavaScript的实现方法是否合适。 var sysJs = DBConnection.database.GetCollection(“system.js”); sysJs.Remove(Query.EQ(“_id”, “getUsers”)); var code = File.ReadAllText(HttpContext.Current.Server.MapPath(“~/Hos/quasiStoredProcedures JS/getUsers.js”)); var codeDocument = new BsonDocument(“value”, new BsonJavaScript(code)); codeDocument.Add(new BsonElement(“_id”, “getUsers”)); sysJs.Insert(codeDocument); BsonValue getUsers = DBConnection.database.Eval(“getUsers”); BsonValue bv3 = DBConnection.database.Eval(getUsers.AsBsonJavaScript.Code, null , loggedInUser.CompanyID […]

使用C#驱动程序从MongoDB Collection上的文本查询中检索相关性排序结果

我正在尝试文本查询集合并以文本匹配顺序检索结果。 文档很好地解释了如何在shell中执行此操作: db.articles.find( { status: “A”, $text: { $search: “coffee cake” } }, { score: { $meta: “textScore” } } ).sort( { date: 1, score: { $meta: “textScore” } } ) 但它需要将查找中的附加score字段投影到排序中。 在C#中,我有一个如下所示的函数: public IEnumerable TextSearch(MongoCollection coll, string text) { var cursor = coll.Find(Query.Text(text)) .SetSortOrder(SortBy.MetaTextScore(???)); foreach(var t in cursor) { // strip projected score from […]

如何将ISODate转换为DateTime?

在一个集合中,我有IsoDates存储如下: SubmitDateTime” : ISODate(“2015-03-02T07:39:05.463Z”) 现在我想使用以下属性将此属性映射到MyModel : public class MyModel { public DateTime SubmitDateTime { get; set; } } 然后将其映射如下: GetCollection(“collection_name”).Find(myQuery).ToListAsync(); 不幸的是我收到此错误: System.ArgumentOutOfRangeException BsonDateTime MillisecondsSinceEpoch的值-9223372036854775808超出了可以转换为.NET DateTime的范围 有谁知道如何解决这一问题? 谢谢。 PS我最初使用Mongo shell中的脚本将此格式的字符串表示forms“2015-03-30T10:50:01.813”转换为IsoDate : var cursor = db.collection_name.find() while (cursor.hasNext()) { var doc = cursor.next(); db.collection_name.update({_id : doc._id}, {$set : {SubmitDateTime : new Date(doc.SubmitDateTime)}}) } 也许可以在那里取得进步。

MongoDB C#驱动程序2.0:如何从MapReduceAsync获取结果

MongoDB C#驱动程序2.0:如何从MapReduceAsync获取结果 我正在使用MongoDB版本3,C#驱动程序2.0,并将获得MapReduceAsync方法的结果。 我有这个集合“用户”: { “_id” : 1, “firstName” : “Rich”, “age” : “18” } { “_id” : 2, “firstName” : “Rob”, “age” : “25” } { “_id” : 3, “firstName” : “Sarah”, “age” : “12” } VisualStudio中的代码: var map = new BsonJavaScript( @” var map = function() { emit(NumberInt(1), this.age); };”); var reduce = […]

使用MongoDB C#驱动程序进行分支

我正在尝试在MongoDb中实现一种称为bucketing的技术(或者在MongoDB研讨会中称之为),它使用Push和Slice来实现这一点。 这是为了实现类似于twitter / facebook的用户馈送系统。 基本上我有一个包含一系列项目(饲料项目)的文档。 我想在这个项目数达到用户的特定数量时创建一个新文档。 因此,如果最新的userFeed文档集合有50个项目,我想要创建一个新文档,并将新项目插入到新创建的文档的项目数组中。 这是我到目前为止的代码: var update = Builders .Update .CurrentDate(x => x.DateLastUpdated) .PushEach(x => x.Items, new List { feedItem }, 50); var result = await Collection.UpdateOneAsync(x => x.User.Id == userFeedToWriteTo, update, new UpdateOptions { IsUpsert = true } ).ConfigureAwait(false); … 但它似乎不会创建新文档,甚至不会将项插入现有文档的数组中。 我认为新文件的创建将由此处理 new UpdateOptions { IsUpsert = true } 但显然不是。 任何帮助将不胜感激

了解MongoDB Aggregate和GroupBy

我正在尝试在MongoDB中进行查询,先按ID进行分组,然后按降序排序。 我在这里有一个函数LINQ表达式: var list = this.GetPackages().ToList(); list = list.OrderByDescending(package => package.PackageVersion) .GroupBy(g => g.PackageId) .Select(packages => packages.First()).ToList(); return list; 但是我似乎无法想出相应的MongoDB表达式,也无法让$ project函数工作: db.packages.aggregate([ { $sort : { packageVersion : -1 } }, { $group: { _id: “$PackageId” } }, { $project: { PackageVersion: 1, Title: 1 } } ]) 我的结果如下: { “_id” : “e3afb1fe-dce7-476e-8372-cd8201abc131” } { […]