- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 MongoDb $GeoWithin 查询开发 API GET 方法。如果我检查带有断点的代码,我会看到我的查询正在返回一个结果。但是当我的应用程序应该返回响应时,我在 Postman 中收到错误 (500) 响应。
我似乎无法弄清楚出了什么问题。我看到“无法将‘MongoDB.Bson.BsonArray’类型的对象转换为‘MongoDB.Bson.BsonBoolean’类型。”在堆栈跟踪中,但我没有发现我的模型/MongoDb 文档有任何问题。
免责声明:我当前的代码非常困惑,但我只是想在重构它之前让它正常工作。
我的模型
public class Bag3DMember
{
[BsonId]
[DataMember]
[BsonElement("_id")]
public string _Id { get; set; }
[DataMember]
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("geometry")]
public Geometry Geometry { get; set; }
[BsonElement("geometry_name")]
public string GeometryName { get; set; }
[BsonElement("properties")]
public Properties Properties { get; set; }
}
public class Geometry
{
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("coordinates")]
public BsonArray Coordinates { get; set; }
}
public class Properties
{
[BsonElement("gid")]
public int Gid { get; set; }
[BsonElement("identificatie")]
public string Identificatie { get; set; }
[BsonElement("aanduidingrecordinactief")]
public bool AanduidingrecordInactief { get; set; }
[BsonElement("aanduidingrecordcorrectie")]
public int AanduidingrecordCorrectie { get; set; }
[BsonElement("officieel")]
public bool Officieel { get; set; }
[BsonElement("inonderzoek")]
public bool InOnderzoek { get; set; }
[BsonElement("documentnummer")]
public string DocumentNummer { get; set; }
[BsonElement("documentdatum")]
public string DocumentDatum { get; set; }
[BsonElement("bouwjaar")]
public string Bouwjaar { get; set; }
[BsonElement("begindatumtijdvakgeldigheid")]
public string BeginDatumTijdVakGeldigheid { get; set; }
[BsonElement("einddatumtijdvakgeldigheid")]
public string EindDatumTijdVakGeldigheid { get; set; }
[BsonElement("gemeentecode")]
public string GemeenteCode { get; set; }
[BsonElement("ground-000")]
public decimal? Ground000 { get; set; }
[BsonElement("ground-010")]
public decimal? Ground010 { get; set; }
[BsonElement("ground-020")]
public decimal? Ground020 { get; set; }
[BsonElement("ground-030")]
public decimal? Ground030 { get; set; }
[BsonElement("ground-040")]
public decimal? Ground040 { get; set; }
[BsonElement("ground-050")]
public decimal? Ground050 { get; set; }
[BsonElement("roof-025")]
public decimal? Roof025 { get; set; }
[BsonElement("roof-050")]
public decimal? Roof050 { get; set; }
[BsonElement("roof-075")]
public decimal? Roof075 { get; set; }
[BsonElement("roof-090")]
public decimal? Roof090 { get; set; }
[BsonElement("roof-095")]
public decimal? Roof095 { get; set; }
[BsonElement("roof-099")]
public decimal? Roof099 { get; set; }
[BsonElement("rmse-025")]
public decimal? Rmse025 { get; set; }
[BsonElement("rmse-050")]
public decimal? Rmse050 { get; set; }
[BsonElement("rmse-075")]
public decimal? Rmse075 { get; set; }
[BsonElement("rmse-090")]
public decimal? Rmse090 { get; set; }
[BsonElement("rmse-095")]
public decimal? Rmse095 { get; set; }
[BsonElement("rmse-099")]
public decimal? Rmse099 { get; set; }
[BsonElement("roof_flat")]
public bool RoofFlat { get; set; }
[BsonElement("nr_ground_pts")]
public int NrGroundPts { get; set; }
[BsonElement("nr_roof_pts")]
public int NrRoofPts { get; set; }
[BsonElement("ahn_file_date")]
public string AhnFileDate { get; set; }
[BsonElement("ahn_version")]
public int AhnVersion { get; set; }
[BsonElement("height_valid")]
public bool HeightValid { get; set; }
[BsonElement("tile_id")]
public string TileId { get; set; }
[BsonElement("bbox")]
public BsonArray[] BoundingBox { get; set; }
}
我的 Controller 方法:
[HttpGet]
public ActionResult<List<Bag3DMember>> Get(string coordinates, double radius)
{
if(coordinates == null || radius == 0)
{
return StatusCode(400, "Paramaters: 'coordinates' and/or 'radius' are not found.");
}
var splittedCoordinates = coordinates.Split(',');
if(splittedCoordinates.Length != 2)
{
return StatusCode(406, "Coordinatesformat not accepted.");
}
var formattedCoordinates = Array.ConvertAll(splittedCoordinates, s => double.Parse(s, CultureInfo.InvariantCulture));
FieldDefinition<Bag3DMember> field = "bbox";
var results = _bag3DService.GetBySpatialQuery(field, formattedCoordinates[0], formattedCoordinates[1], radius);
var jsonResults = Json(results);
return Json(results);
}
数据库服务方法
public List<Bag3DMember> GetBySpatialQuery(FieldDefinition<Bag3DMember> field, double x, double y, double radius)
{
//var filter = Builders<Bag3DMember>.Filter.GeoWithinBox(field, (x - (radius/2)), (y - (radius / 2)), (x + (radius / 2)), (y + (radius / 2)));
BsonArray lowerLeftDoc = new BsonArray(new[] { 0, 0 });
BsonArray upperRightDoc = new BsonArray(new[] { 10000000, 10000000 });
BsonArray boundingBox = new BsonArray(new[] { lowerLeftDoc, upperRightDoc });
BsonDocument locBox = new BsonDocument { { "$box", boundingBox } };
BsonDocument locDoc = new BsonDocument { { "$geoWithin", locBox } };
BsonDocument queryDoc = new BsonDocument { { "properties.bbox", locDoc } };
var results = _Bag3DMembers.Find(new QueryDocument(queryDoc)).ToList();
return results;
}
因此在 Visual Studio 中它成功返回了查询。如您所见,它从数据库返回一个文档:
JSON 对象:
{
"$type": "System.Collections.Generic.List<EnveoApi.Bag3DMember>",
"$values": [
{
"$type": "EnveoApi.Bag3DMember",
"_Id": "pand3d.8575483",
"Type": "Feature",
"Geometry": {
"$type": "EnveoApi.Geometry",
"Type": "Polygon",
"Coordinates": {
"$type": "MongoDB.Bson.BsonArray",
"$values": [
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
108665.593,
447232.925,
0
]
},
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
108667.648,
447229.102,
0
]
},
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
108676.807,
447234.217,
0
]
},
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
108674.334,
447238.579,
0
]
},
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
108665.593,
447232.925,
0
]
}
]
}
]
}
},
"GeometryName": "geovlak",
"Properties": {
"$type": "EnveoApi.Properties",
"Gid": 8575483,
"Identificatie": "0513100011121832",
"AanduidingrecordInactief": "false",
"AanduidingrecordCorrectie": 0,
"Officieel": "false",
"InOnderzoek": "false",
"DocumentNummer": "BAGAV1776",
"DocumentDatum": "2018-08-15Z",
"Bouwjaar": "1900-01-01Z",
"BeginDatumTijdVakGeldigheid": "2018-08-14T22:00:00Z",
"EindDatumTijdVakGeldigheid": null,
"GemeenteCode": "0513",
"Ground000": -0.44,
"Ground010": -0.42,
"Ground020": -0.41,
"Ground030": -0.41,
"Ground040": -0.4,
"Ground050": -0.39,
"Roof025": 2.72,
"Roof050": 2.81,
"Roof075": 3.03,
"Roof090": 4.76,
"Roof095": 7.04,
"Roof099": 9.72,
"Rmse025": 0.87,
"Rmse050": 0.62,
"Rmse075": 0.62,
"Rmse090": 0.62,
"Rmse095": 0.62,
"Rmse099": 0.62,
"RoofFlat": "false",
"NrGroundPts": 12,
"NrRoofPts": 1247,
"AhnFileDate": "2014-02-25T23:00:00Z",
"AhnVersion": 3,
"HeightValid": "true",
"TileId": "38an2",
"BoundingBox": {
"$type": "MongoDB.Bson.BsonArray[]",
"$values": [
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
108665.593,
447229.102
]
},
{
"$type": "MongoDB.Bson.BsonArray",
"$values": [
108676.807,
447238.579
]
}
]
}
}
}
]
}
我在 Visual Studio 2019 中没有收到任何错误。我只在 Postman 中收到错误(堆栈跟踪):
System.InvalidCastException: Unable to cast object of type 'MongoDB.Bson.BsonArray' to type 'MongoDB.Bson.BsonBoolean'.
at get_AsBoolean(Object )
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnWrite(WriteStackFrame& current, Utf8JsonWriter writer)
at System.Text.Json.JsonPropertyInfo.Write(WriteStack& state, Utf8JsonWriter writer)
at System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteObject(JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultFilters>g__Awaited|27_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:44348
User-Agent: PostmanRuntime/7.19.0
Postman-Token: c23588a0-0e6b-46f8-834c-93dbfc6134eb
编辑 1:
如果我注释掉“几何”中所有嵌套的“属性”和“坐标”,似乎它会起作用。
显然,“坐标”给了我一个问题。但是“属性”中也有一些文件给我带来了问题。但我仍然不明白为什么它试图将 BsonArray (坐标是)转换为 bool 值?
此外,我现在只返回结果,而不是 Json(results)。但这没有任何区别。
编辑 2:如果我让它动态化,“坐标”就会起作用。但我认为这是不好的做法,对吗?
public class Geometry
{
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("coordinates")]
public dynamic Coordinates { get; set; }
}
最佳答案
我遇到了这个问题,我的解决方法是将我的集合通用更改为,一切正常,任何模型结构都可以工作。示例:DB.GetCollection("集合名称");
关于c# - 如何修复 : Unable to cast object of type 'MongoDB.Bson.BsonArray' to type 'MongoDB.Bson.BsonBoolean' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58579516/
我已经在 kubernetes 中部署了一个 3 pod mongodb statefulset,并且我正在尝试使用新的 mongodb+srv 连接字符串 (mongodb 3.6) 连接到具有 S
我已经创建了 MongoDB Atlas 帐户,并尝试连接。但出现以下错误。 MongoDB 连接错误 MongoNetworkError: 首次连接时无法连接到服务器 [cluster0-shard
我正在使用 Node-WebKit 创建桌面应用程序。该应用程序基本上是创建文档(员工日常工作的详细信息),任何注册用户都可以对这些文档发表评论。我正在创建的文档将被分成几个部分。用户将对特定部分发表
我正在尝试使用官方网站上的安装程序在我的本地机器上安装 mongo DB。但是我不断收到这条消息,有人可以帮忙吗? 我试过提供的解决方案 here但没有帮助。 最佳答案 我建议执行以下操作: 按 Wi
我对 MongoDB 和 MongoDB Compass 非常陌生。 我的客户集合中有大约 1000 条记录。如何通过 MongoDB 指南针一次删除所有记录。 非常感谢, 最佳答案 您可以使用 Mo
当我尝试在我的 Ubuntu 机器中安装 mongodb 时,apt-get 会显示以下选项 mongodb mongodb-clients mongodb-dev mongodb-server 谁能
如何将 Robomongo(或任何其他 mongodb 客户端)连接到由本地 Meteor 应用程序创建的 mongodb 实例? 最佳答案 确保 Meteor 正在本地主机上运行。打开终端窗口并运行
我需要在 MongoDB 中生成一个简单的频率表。假设我在名为 books 的集合中有以下文档。 { "_id": 1, genre: [ "Fantasy", "Crime"
我如何在 mongos mapreduce 中指定一个条件,就像我们在 mongos group 函数中所做的那样。 我的数据是这样的 {lid:1000, age:23}, {lid:3000, a
我的 mongodb 数据库文档中有几个 ID。我需要通过脚本在这些 ID 上创建索引,这样我就不必一次又一次地运行 ensureIndex 命令。 db.getCollection("element
在我的数据库中,每个包含项目的文档中都有一个嵌套的元素数组,格式如下: elements:[ { "elem_id": 12, items: [ {"i_id": 1
我正在构建一个应用程序,其中用户可以位于不同的时区,并且我运行的查询对他们的时区很敏感。 我遇到的问题是 MongoDB 似乎在查询时忽略了时区! 这是日期字段“2019-09-29T23:52:13
我正在研究使用 mongodb 进行分片,我有以下结构: 1 个 Mongod 到我的 ConfigServer,在 ReplicaSet 中只有 1 个成员 2 个分片,每个分片在 ReplicaS
我正在尝试获取一个 mongoDB 对象,例如 Friend1 包含另一个 mongoDB 对象 Friend2,该对象又包含第一个对象 Friend1本质上使它成为一个循环对象引用。 要么这样,要么
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
Mongo 版本 5.0.2。 Ubuntu 20.0 我在本地主机中启用了 MongoDB 连接的安全性。 我正在尝试通过以下命令使用身份验证详细信息连接我的本地主机 MongoDBmongo ad
我即将将分片的 MongoDB 环境从 2.0.7 升级到 2.2.9,最终我想升级到 2.4.9,但显然我需要通过 2.2 来完成。 2.2 的发行说明声明配置服务器应该首先升级其二进制文件,然后是
目前,我无法在我的虚拟 Ubuntu 机器上远程连接 mongodb 服务器。我无法使用在我的 Windows PC 上运行的 Robomongo 客户端连接,该 PC 也运行 vm。 这是两台电脑的
我创建了一个免费的 mongodb 集群。我创建了一个用户,设置了与 mongodb compass 的连接,复制了连接字符串,然后打开了我的 mongodb compass。将复制的字符串粘贴到那里
我使用 java 代码创建了 mongo 数据库集合索引 dbCollection.createIndex("accountNumber"); 当我看到索引使用 db.accounts.getInde
我是一名优秀的程序员,十分优秀!