我正在尝试从嵌套的mongodb文档中找到最接近的编码。我不断收到的错误如下:
我已经尽力了。我试图添加索引2d,但该索引也不起作用。
var point = GeoJson.Point(GeoJson.Geographic(38.8086, -85.1792));
var locationQuery = new FilterDefinitionBuilder<Book>().NearSphere(tag => tag.CitiesInBook[-1].Location, point,
5000);
var query = collection.Find(locationQuery).Limit(10);
var a = query.ToList();
计划者返回错误
无法找到$ geoNear查询的索引。”
答案 0 :(得分:0)
以下汇总查询适用于图书实体中的嵌入城市。需要注意的一点是,您应该使用正确的键名创建geo2dsphere索引,并在正确的结构中使用c#类,以便驱动程序进行序列化/反序列化。
#!/bin/bash
currentDateTime=$(date +"%Y%m%d.%k%M")
fileName="Statements-$currentDateTime"
touch ~/MyStatements/$fileName
echo $currentDateTime
echo $fileName
history -a "~/MyStatements/$fileName"
history -a newFile.text
以上查询是由以下MongoDB.Entities代码生成的:
db.Book.aggregate({
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [
48.857908,
2.295243
]
},
"distanceField": "SearchResult.DistanceKM",
"spherical": true,
"maxDistance": NumberInt("20000"),
"includeLocs": "SearchResult.Location"
}
})
答案 1 :(得分:-1)
这是使用MongoDB.Entities的解决方案。我使用的是书籍与城市之间的一对多关系,而不是将城市嵌入书籍实体中。
using MongoDB.Driver;
using MongoDB.Entities;
namespace StackOverflow
{
public class Program
{
public class Book : Entity
{
public string Name { get; set; }
public Many<City> Cities { get; set; }
public Book() => this.InitOneToMany(() => Cities);
}
public class City : Entity
{
public string Name { get; set; }
public Coordinates2D Location { get; set; }
public double DistanceKM { get; set; }
}
static void Main(string[] args)
{
new DB("test");
//create an index
DB.Index<City>()
.Key(c => c.Location, KeyType.Geo2DSphere)
.Option(o => o.Background = false)
.Create();
var paris = new City
{
Name = "paris",
Location = new Coordinates2D(48.8539241, 2.2913515)
};
paris.Save();
var versailles = new City
{
Name = "versailles",
Location = new Coordinates2D(48.796964, 2.137456)
};
versailles.Save();
var poissy = new City
{
Name = "poissy",
Location = new Coordinates2D(48.928860, 2.046889)
};
poissy.Save();
var scifi = new Book { Name = "sci-fi" };
scifi.Save();
scifi.Cities.Add(paris);
scifi.Cities.Add(versailles);
scifi.Cities.Add(poissy);
var horror = new Book { Name = "horror" };
horror.Save();
horror.Cities.Add(poissy);
var eiffelTower = new Coordinates2D(48.857908, 2.295243);
//find matching city IDs within 20kms of eiffel tower.
var cities = DB.GeoNear<City>(
NearCoordinates: eiffelTower,
DistanceField: c => c.DistanceKM,
MaxDistance: 20000);
//get books with matching cities
var books = DB.Entity<Book>()
.Cities.ParentsFluent<Book>(cities)
.ToList();
}
}
}
以上程序向mongodb发出以下聚合查询:
第一个查询:
{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [
48.857908,
2.295243
]
},
"distanceField": "DistanceKM",
"spherical": true,
"maxDistance": NumberInt("20000")
}
}, {
"$lookup": {
"from": "[Book~City(Cities)]",
"localField": "_id",
"foreignField": "ChildID",
"as": "Results"
}
}, {
"$replaceRoot": {
"newRoot": {
"$arrayElemAt": [
"$Results",
NumberInt("0")
]
}
}
}, {
"$lookup": {
"from": "Book",
"localField": "ParentID",
"foreignField": "_id",
"as": "Results"
}
}, {
"$replaceRoot": {
"newRoot": {
"$arrayElemAt": [
"$Results",
NumberInt("0")
]
}
}
}, {
"$group": {
"_id": "$_id",
"doc": {
"$first": "$$ROOT"
}
}
}, {
"$replaceRoot": {
"newRoot": "$doc"
}
}