Tag: mongodb .net driver

如何使用MongoDB的官方C#驱动程序检索所有嵌入的文档值?

鉴于以下类和示例文档,如何从Question集合中检索AnswerChoice文档,其中AnswerChoice中的_id是’4d6d336ae0f84c23bc1fae00’使用官方C#驱动程序。 谢谢。 public class Question { [BsonId] public ObjectId QuestionId {get;set;} public string Question {get;set;} public List AnswerChoices {get;set;} } public class AnswerChoice { [BsonId] public ObjectId AnswerChoiceId {get;set;} public string Answer {get;set;} public int Order {get;set;} } //示例文档 { “_id”: “4d6d3369e0f84c23bc1facf7”, “Question”: “Question 1”, “AnswerChoices”: [ { “_id”: “4d6d3369e0f84c23bc1facf2”, “Answer”: “Answer Choice A”, “Order”: […]

MongoDB的push和root的C#等价物是什么?

我有一群人。 我试图为每个名字找到最老的人。 我可以使用mongoDB控制台命令实现该结果 db.People.aggregate([ { ‘$sort’: { ‘Name’: 1, ‘Age’: -1 } }, { ‘$group’: { ‘_id’: ‘$Name’, ‘docs’: { ‘$push’: ‘$$ROOT’ }, } }, { ‘$project’: { ‘top_one’: { ‘$slice’: [‘$docs’, 1] } } } ]) 对于C#驱动程序,这相当于什么? 我特别遇到麻烦 ‘docs’: { ‘$push’: ‘$$ROOT’ }, 这是我当前的C#查询: collection.Aggregate(aggArgs) .SortByDescending(x => x.Age) .Group(x => x.Name, x => new […]

mongoDB重命名嵌入字段

我们如何使用C#和mongoDB重命名嵌入字段? 文档Person的一个例子是: { Id: 1, LastName: “Smith”, FirstName: “John”, Orders: { Id: 1, Name: “Trousers” // I want to rename **Name** into **Something** } } 使用mongoDB语法,它就像是 db.Users.update({}, {$rename:{“Orders.Name”:”Orders.Something”}},true, true) 谢谢。

如何在使用mongo csharp插入后获取rcently插入文档的_id?

我能够使用以下代码成功插入新文档,但我无法获取新插入文档的_id。 插入后,user为null。 谢谢! MongoServer server = MongoServer.Create(); MongoDatabase test = server.GetDatabase(“Test”); MongoCollection users = test.GetCollection(“Users”); BsonDocument user = new BsonDocument(); user.Add(“FirstName”, “John”); user.Add(“LastName”, “Michael”); user.Add(“Address”, “123 Main Street”); user.Add(“City”, “Newport Beach”); user.Add(“State”, “CA”); user.Add(“ZipCode”, “92660”); user.Add(“Email”, “John.Michael@myemail.com”); user.Add(“CreatedDate”, DateTime.Now); user.Add(“IPAddress”, “10.1.1.1”); user = users.Save(user); string idSTring = user[“_id”].ToString().Replace(“\””, “”);

使用MongoDB C#驱动程序在Aggregation Framework中使用allowDiskUse

我想允许DiskUse:true。 但是,我找不到任何解释allowDiskUse启用MongoDB C#驱动程序的示例。 如何在MongoDB C#驱动程序中启用allowDiskUse? 我的示例代码就是这样 var pipeline = new[] { match, project, group, limit, sort, allow }; List result = db .GetCollection(“TwitterStatus”) .Aggregate(pipeline).ResultDocuments.Select(x => new User { Influence = Convert.ToDouble(x[“Influence”]), User = new SMBUser((BsonDocument)x[“User”]) }).ToList();

无法通过C#客户端连接到MongoDB(MongoLabs)

问题背景: 我在MongoLabs(mLab – https://mlab.com/ )中设置了一个数据库并添加了一个非常简单的Collection。 我正在使用MongoDB驱动程序尝试通过C#3.2驱动程序连接和使用此集合。 问题: 我无法通过C#驱动程序连接到我的数据库,并且持续进行身份validation,如下所示: System.TimeoutException:使用CompositeServerSelector选择服务器30000ms后出现超时{Selectors = ReadPreferenceServerSelector {ReadPreference = {Mode = Primary,TagSets = []}},LatencyLimitingServerSelector {AllowedLatencyRange = 00:00:00.0150000}}。 群集状态的客户端视图是{ClusterId:“1”,ConnectionMode:“Automatic”,类型:“Unknown”,状态:“Disconnected”,服务器:[{ServerId:“{ClusterId:1,EndPoint:”Unspecified / ds048719。 mlab.com:48719“}”,EndPoint:“未指定/ ds048719.mlab.com:48719”,状态:“已断开连接”,类型:“未知”,HeartbeatException:“MongoDB.Driver.MongoConnectionException:打开时发生exception连接到服务器.—. MongoDB.Driver.MongoAuthenticationException:无法使用sasl协议机制SCRAM-SHA-1进行身份validation.—> MongoDB.Driver.MongoCommandException:命令saslStart失败:身份validation失败。 代码: 我尝试了许多尝试validation请求的不同方法。 目前我正在尝试简单地使用MongoClient类,如下所示: MongoClient client; var connectionString = “mongodb://userNameGoesHereRemovedForSO:passwordGoesHereRemovedForSO@ds048555.mlab.com:48719/db”; client = new MongoClient(connectionString); var database = client.GetDatabase(“testDB”); var collection = database.GetCollection(“test”); 任何有关如何成功克服此身份validation问题的帮助或示例都将受到高度赞赏。

使用MongoDB时如何按惯例应用BsonRepresentation属性

我正在尝试将[BsonRepresentation(BsonType.ObjectId)]应用于表示为字符串的所有id,而不得不用属性装饰我的所有id。 我尝试添加StringObjectIdIdGeneratorConvention但似乎没有对它进行排序。 有任何想法吗?

使用MongoDB的官方C#驱动程序进行按位枚举(标志)查询

当我尝试运行表单的LINQ查询时: MongoCollection collection; collection.AsQueryable().Where(entity => (entity.Flags & MyFlags.AFlag) != MyFlags.None); 我得到一个带有消息Unsupported where clause: ((Int32)((Int32)entity.Flags & 4) != 0).的ArgumentException Unsupported where clause: ((Int32)((Int32)entity.Flags & 4) != 0). 这是一个已知的错误/function吗? 有没有解决方法? 从文档中可以看出,MongoDB有一个按位更新,但不是按位查询。 为了进行比较,使用ServiceStack作为客户端,相同的查询在Redis上方顺利运行。 我确实找到了建议使用JavaScript的这两个链接( link1 , link2 ),这将使服务层的实现非常依赖于DB技术。

MongoDb SafeMode与WriteConcern进行比较

谁能说如何用WriteConcern替换过时的SafeMode ? 特别是我对SafeMode.True感兴趣? 谢谢。

在MongoDB C#中展开组聚合

我在使用新的C#2.0 MongoDB驱动程序和聚合管道时遇到了一些麻烦。 基本上,我试图返回对象上数组字段中最受欢迎的元素。 字段类型为: IList FavouritePlaceIds { get; set; } IList FavouritePlaceIds { get; set; } IList FavouritePlaceIds { get; set; } 。 我有以下MongoDB聚合,它按预期工作: db.users.aggregate([ { $unwind : “$FavouritePlaceIds” }, { $group: { “_id”: “$FavouritePlaceIds”, “count”: {$sum: 1}}}, { $sort : { “count”: -1 }} ]) 但是,现在的问题是尝试使用新的MongoDB驱动程序2.0将其转换为C#代码。 我一直在使用以下链接获取有关聚合管道的帮助: http : //mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/crud/reading/#unwind 到目前为止,我已经为我的聚合管道提供了以下内容: var pipeline = […]