标签: mongodb-.net-driver

查询近与内

使用MongoDB我正在查询距离纬度为25英里的家庭.

我第一次尝试使用near命令,如下所示:

var near = Query.Near("Coordinates", coordinates.Latitude, coordinates.Longitude, find.GetRadiansAway(), false);
var query = Collection().Find(near);
var listings = query.ToList();
Run Code Online (Sandbox Code Playgroud)

near附近的问题是它只返回100个列表,而我想返回坐标25英里内的所有列表.

我的下一次尝试是在内部使用:

var within = Query.WithinCircle("Coordinates", coordinates.Latitude, coordinates.Longitude, find.GetRadiansAway(), false);
var query = Collection().Find(within);
var listings = query.ToList();
Run Code Online (Sandbox Code Playgroud)

内部返回25英里范围内的所有列表,这很好,但是它并没有根据它们与中心坐标的距离来排序.

所以我的问题是,我如何充分利用这两个世界?如何获取25英里范围内的所有列表并按中心坐标的接近程度排序?

geospatial mongodb mongodb-.net-driver

5
推荐指数
1
解决办法
1228
查看次数

使用Mongo C#驱动程序持久化派生对象

我有以下类层次结构

[BsonKnownTypes(typeof(MoveCommand))]
public abstract class Command : ICommand
{
    public abstract string Name
    {
        get;
    }

    public abstract ICommandResult Execute();
}
    public class MoveCommand : Command
{
    public MoveCommand()
    {
        this.Id = ObjectId.GenerateNewId().ToString();
    }

    [BsonId]
    public string Id { get; set; }

    public override string Name
    {
        get { return "Move Command"; }
    }

    public override ICommandResult Execute()
    {
        return new CommandResult { Status = ExecutionStatus.InProgress };
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我像这样保存命​​令:

 Command c = new MoveCommand();
MongoDataBaseInstance.GetCollection<Command>("Commands").Save(c);
Run Code Online (Sandbox Code Playgroud)

然后查询数据库,我没有看到派生属性持久化.

{"_ id":"4df43312c4c2ac12a8f987e4","_ t":"MoveCommand"} …

mongodb mongodb-.net-driver

5
推荐指数
1
解决办法
3933
查看次数

管理类结构中的更改以与mongodb集合保持一致

我们正在使用mongodb和c#.我们正试图找到一种方法来保持我们的集合无缝一致.现在,如果开发人员对类结构进行了任何更改(添加字段或更改数据类型或更改嵌套类中的属性),他/她必须手动更改mongo集合.

随着我们项目的不断发展以及从事该项目的开发人员不断增加,这很痛苦.想知道某人是否已经找到了解决这个问题的方法.

  • 研究
    1. 我发现了类似的问题.但是,找不到解决方案.
    2. 找到了查找所有属性的方法查找属性 ; 但是,数据类型和嵌套文档成为一个问题.

mongodb mongodb-.net-driver

5
推荐指数
1
解决办法
802
查看次数

Mongodb,linq司机.如何使用变量或语句构造Contains

我正在使用Mongo LINQ驱动程序用于C#,效果很好.

排序很多属性,但这是一个我无法解决的问题,它可能很简单.

var identifierList = new []{"10", "20", "30"};
var newList = list.Where(x => identifierList.Contains(x.Identifier));

This is NOT supported ... 
Run Code Online (Sandbox Code Playgroud)

所以我可以这样做:

 var newList = list.Where(x => x.Identifier == "10" || x.Identifier == "20" || x.Identifier == "30");
Run Code Online (Sandbox Code Playgroud)

但由于列表是可变的......我该如何构建上述内容?还是有更好的选择吗?

list类型是IQueryable<MyCustomClass>

有关信息......这用作许多属性的过滤器.在SQL中我可以有一个父 - >子关系.但由于我不能作为主ID的父亲,我需要把所有的ID都拿出去,然后像这样构建它.

希望这是有道理的.如果需要,我会解释更多.

.net c# linq mongodb mongodb-.net-driver

5
推荐指数
1
解决办法
7411
查看次数

MongoDB将数组添加到BsonDocument

我正在尝试使用C#驱动程序在mongodb文档中添加字段.

我正在创建一个文档.

BsonDocument document = new BsonDocument();
Run Code Online (Sandbox Code Playgroud)

并添加

document.Add(name, value); // here name and value both are string
Run Code Online (Sandbox Code Playgroud)

但我不确定如何在这种情况下添加数组

喜欢 document.Add(name, values); // here values is List<string>

例如 document.Add(skills, [C++, Java, C#]);

请在这件事上给予我帮助

c# mongodb bson mongodb-.net-driver

5
推荐指数
1
解决办法
4067
查看次数

MongoDB,C#,QueryFailure标志不是master而slaveOk = false

我有一个网站已经运行了好几个月,现在,突然间,没有任何作用.该站点使用MVC 5完成,并使用MongoDB进行存储.

我最好的猜测是MongoDB主机正在搞乱,这导致了问题,但......

我该怎么做以避免将来出现这个问题?我必须改变一些设置吗?

这是错误和堆栈跟踪:

QueryFailure标志不是master而slaveOk = false(响应是{"$ err":"not master and slaveOk = false","code":13435}).

at MongoDB.Driver.MongoReplyMessage 1.ReadHeaderFrom(BsonBuffer buffer) at MongoDB.Driver.Internal.MongoReplyMessage1.ReadFrom(BsonBuffer buffer)MongoDB.Driver.Internal.MongoConnection.ReceiveMessage [TDocument](BsonBinaryReaderSettings readerSettings,IBsonSerializer serializer,IBsonSerializationOptions serializationOptions),位于MongoDB.Driver.Operations.QueryOperation 1.GetFirstBatch(IConnectionProvider connectionProvider) at MongoDB.Driver.Operations.QueryOperation1.Execute( System.Linq.Buffer 1..ctor(IEnumerable1上的IConnectionProvider connectionProvider)System.Linq.OrderedEnumerable 1.<GetEnumerator>d__0.MoveNext() at System.Linq.Enumerable.<TakeIterator>d__3a1.MoveNext(1..ctor(IEnumerable)在System.Linq.Enumerable.ToList [TSource]的System.Collections.Generic.List 1 collection)(IEnumerable 1 source) at ScaleModellingCentral.Web.UI.Controllers.HomeController.Read() at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2参数)在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary)2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult)在System.Web.Mvc的System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<> c__DisplayClass46.b__3f()上的System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()处. System.Web.Mvc.Async.AsyncControllerActionInvoker上的System.Web.Mvc.Async.AsyncControllerActionInvoker.<> c__DisplayClass21.<> c__DisplayClass2b.b__1c()中的Async.AsyncControllerActionInvoker.<> c__DisplayClass33.b__32(IAsyncResult asyncResult).<> c__DisplayClass21. …

asp.net-mvc mongodb-.net-driver

5
推荐指数
1
解决办法
1695
查看次数

Azure VM上与MongoDb的连接超时

将Azure Web App连接到Azure VM上托管的MongoDb时,我遇到了一些超时问题.

2015-12-19T15:57:47.330+0100 I NETWORK  Socket recv() errno:10060 A connection attempt 
 failed because the connected party did not properly respond after a period of time, 
 or established connection failed because connected host has failed to respond.
2015-12-19T15:57:47.343+0100 I NETWORK  SocketException: remote: 104.45.x.x:27017 error: 
 9001 socket exception [RECV_ERROR] server [104.45.x.x:27017]
2015-12-19T15:57:47.350+0100 I NETWORK  DBClientCursor::init call() failed
Run Code Online (Sandbox Code Playgroud)

目前mongodb配置在单个服务器上(仅适用于dev),并通过公共IP公开.网站使用azure域名(*.westeurope.cloudapp.azure.com)连接到它,没有虚拟网络.

通常一切都运行良好,但在几分钟不活动后,我得到超时异常.从我的PC上使用MongoDb shell时会发生同样的情况,所以我很确定这是mongodb方面的一个问题.

我错过了一些配置?

azure mongodb azure-virtual-machine azure-virtual-network mongodb-.net-driver

5
推荐指数
1
解决办法
2943
查看次数

在MongoDB C#驱动程序2.2中使用AsQueryable进行投影

我正在尝试使用MongoDB C#驱动程序2.2版.我正在尝试使用投影,因为我不想检索文档中的所有元素.我发现一种方法是使用项目运算符和find运算符,如下所示:

collection.Find(key => key.Index == 1).Project<MyClass>(Builders<MyClass>.Projection.Include(key => key.Name).Include(key => key.Index)). ToEnumerable ();
Run Code Online (Sandbox Code Playgroud)

但是,我有兴趣使用AsQueryable API以及where运算符,如下所示:

collection.AsQueryable().Where(key => key.Index == 1);
Run Code Online (Sandbox Code Playgroud)

在上述情况下是否可以使用投影?如果我使用选择运算符,它会与投影具有相同的效果吗?或者仍然会从数据库服务器获取所有元素,然后在应用程序服务器中选择指定的元素?

c# asqueryable mongodb mongodb-.net-driver

5
推荐指数
1
解决办法
1287
查看次数

C#Mongodb FindOneAndReplace或ReplaceOne无法正常工作

public async Task SomeWork()
        {
            Debug.WriteLine("SomeWork for User : " + Context.User.Identity.Name);
            var userProfile = await UserProfileCollection.Find(u => u._id == Context.User.Identity.Name).FirstOrDefaultAsync();

            userProfile.SuccessfullConnect = true; // currently false
            var up = await UserProfileCollection.FindOneAndReplaceAsync(u => u._id == userProfile._id, userProfile);
            Debug.WriteLine(ObjectDumper.Dump(userProfile));
            Debug.WriteLine(ObjectDumper.Dump(up));
            Debug.WriteLine("Saved SomeWork for User : " + Context.User.Identity.Name);
        }
Run Code Online (Sandbox Code Playgroud)

以下是调试输出。我的文档没有被替换。检查SuccesslConnect变量,它仍然为false。我在服务器上检查了文件也没有替换。有时它工作,有时不工作。

SomeWork for User : +919933221101
{TestSignalR_Server.Models.UserProfile}
  _id: "+919933221101"
  ...
  SuccessfullConnect: True
  ...

{TestSignalR_Server.Models.UserProfile}
  _id: "+919933221101"
  ...
  SuccessfullConnect: False
  ...

Saved SomeWork for User : +919933221101
Run Code Online (Sandbox Code Playgroud)

c# mongodb mongodb-.net-driver

5
推荐指数
1
解决办法
2962
查看次数

无法使用Azure中的另一个MVC应用程序将Azure机器中的SSL与MongoDB连接

我在连接使用SSL配置的MongoDB时遇到问题.我在Azure虚拟机中有MongoDB企业服务器,它具有以下配置.

net:
bindIp: 0.0.0.0
port: 27017
ssl:
    CAFile: 'C:\openssl-0.9.8h-1-bin\bin\rCA.pem'
    PEMKeyFile: 'C:\openssl-0.9.8h-1-bin\bin\rser.pem'
    allowConnectionsWithoutCertificates: false
    allowInvalidHostnames: true
    mode: requireSSL
storage:
    dbPath: 'C:\data\db'
Run Code Online (Sandbox Code Playgroud)

我有一个C#示例连接mongodb和作为字节数组传递的证书数据.

MongoClientSettings settings = new MongoClientSettings
        {
            Server = new MongoServerAddress("mongo_azure_host", 27017),
            UseSsl = true,
            RetryWrites = true
        };
        settings.VerifySslCertificate = false;
        var SslCertificateData = FilePathHelper.ReadFile(Server, mySslClientCertificate);
        var certificate = new X509Certificate2(SslCertificateData, "pwd");
            settings.SslSettings = new SslSettings()
            {
                ClientCertificates = new[] { certificate }
            };
        }
        MongoClient mongoClient = new MongoClient(settings);
        mongoClient.GetServer().Connect();
Run Code Online (Sandbox Code Playgroud)

如果样本在我的本地环境中,这可以正常工作.但是,如果我在Azure Web应用程序中发布相同内容并尝试连接,则会引发以下异常

system.componentmodel.win32exception:无法识别提供给包的凭据

ssl openssl azure mongodb mongodb-.net-driver

5
推荐指数
0
解决办法
72
查看次数