Я хочу добавить координаты из этого идентификатора '5bf31f8c23d1fd21a2fb01e0' к каждому объекту в файле json, потому что мне нужно вычислить расстояние между адресами / координатами.
И это должно быть сделано динамически.Не статично, как показано в первом разделе кода.И я даже не знаю, возможно ли это получить в функции $ addFields.
Я новичок в nosql (mongo), поэтому, пожалуйста, наберитесь терпения.
db.getCollection('lodging').aggregate(
[
{
$match:
{
'_id': {$not: {$eq: ObjectId('5bf31f8c23d1fd21a2fb01e0').valueOf()}}
}
},
{
$addFields:
{
'coords':
{
'lat': -80.733179, // get lat from this id 5bf31f8c23d1fd21a2fb01e0
'long': 4.164745 // get long from this id 5bf31f8c23d1fd21a2fb01e0
}
}
},
{
$project:
{
'_id': 1,
'host': 1,
'lodging': 1,
'coords': 1
}
}
]
)
Вывод JSON:
{
"_id" : "5bf31f8c00168cb98a5fe77c",
"host" : {
"name" : "Sanders",
"surname" : "Rosales",
"gender" : "male",
"age" : 47,
"picture" : "https://api.adorable.io/avatars/285/SandersRosales.png",
"languages" : [
"spanish",
"polish"
],
"contact" : {
"company" : "GLOBOIL",
"email" : "sandersrosales@globoil.com",
"phone" : "+1 (820) 513-3649"
},
"reviews" : [
{
"reviewer" : "Elena Sloan",
"date" : "2017-06-23T04:35:52 -02:00",
"helpful" : 10,
"kind" : 9,
"responsive" : 3
},
{
"reviewer" : "Aurora Ortiz",
"date" : "2014-04-14T04:55:26 -02:00",
"helpful" : 3,
"kind" : 6,
"responsive" : 7
},
{
"reviewer" : "Jaime Holcomb",
"date" : "2018-06-07T07:47:59 -02:00",
"helpful" : 9,
"kind" : 4,
"responsive" : 9
},
{
"reviewer" : "Kim Mack",
"date" : "2016-10-06T03:52:52 -02:00",
"helpful" : 9,
"kind" : 5,
"responsive" : 8
},
{
"reviewer" : "Jenny Mcdaniel",
"date" : "2017-08-28T07:01:16 -02:00",
"helpful" : 5,
"kind" : 7,
"responsive" : 4
}
]
},
"lodging" : {
"price" : "$66",
"address" : {
"street" : "596 Canarsie Road",
"city" : "Weeksville",
"state" : "Delaware",
"coordinates" : [
-59.170234,
-57.874213
]
},
"amenities" : {
"freeWifi" : true,
"petsAllowed" : true,
"balcony" : true
},
"reviews" : [
{
"reviewer" : "Anderson Hernandez",
"date" : "2018-06-07T01:51:57 -02:00",
"cleanliness" : 8,
"location" : 10,
"food" : 1
},
{
"reviewer" : "Hooper Herman",
"date" : "2017-02-06T08:41:37 -01:00",
"cleanliness" : 2,
"location" : 5,
"food" : 2
},
{
"reviewer" : "Liza Cervantes",
"date" : "2015-05-20T03:45:31 -02:00",
"cleanliness" : 7,
"location" : 9,
"food" : 7
},
{
"reviewer" : "Bartlett Tyson",
"date" : "2018-11-11T05:47:34 -01:00",
"cleanliness" : 3,
"location" : 6,
"food" : 5
},
{
"reviewer" : "Abbott Barron",
"date" : "2016-10-25T02:20:33 -02:00",
"cleanliness" : 1,
"location" : 8,
"food" : 5
},
{
"reviewer" : "Consuelo Goodman",
"date" : "2015-12-01T08:57:35 -01:00",
"cleanliness" : 10,
"location" : 9,
"food" : 7
}
]
},
"coords" : {
"lat" : -80.733179,
"long" : 4.164745
} }
JSON-файл (один объект)
/* 2 */
{
"_id" : "5bf31f8cdd62fe9f8cc87761",
"index" : 3,
"guid" : "021be162-c209-44dc-9664-1f3622b23296",
"isActive" : true,
"registered" : "2009-08-12T06:53:53 -02:00",
"host" : {
"name" : "Traci",
"surname" : "Coffey",
"gender" : "female",
"age" : 55,
"picture" : "https://api.adorable.io/avatars/285/TraciCoffey.png",
"languages" : [
"spanish",
"slovenian",
"polish"
],
"contact" : {
"company" : "ZIDOX",
"email" : "tracicoffey@zidox.com",
"phone" : "+1 (842) 462-3547"
},
"reviews" : [
{
"reviewer" : "Billie Horton",
"date" : "2017-03-08T09:00:16 -01:00",
"helpful" : 5,
"kind" : 7,
"responsive" : 3
},
{
"reviewer" : "Faulkner Puckett",
"date" : "2017-10-15T11:52:05 -02:00",
"helpful" : 5,
"kind" : 9,
"responsive" : 6
},
{
"reviewer" : "Christa Potter",
"date" : "2018-10-30T12:53:02 -01:00",
"helpful" : 8,
"kind" : 6,
"responsive" : 3
}
]
},
"lodging" : {
"price" : "$167",
"address" : {
"street" : "908 Duryea Court",
"city" : "Derwood",
"state" : "District Of Columbia",
"coordinates" : [
9.583392,
126.149643
]
},
"amenities" : {
"freeWifi" : true,
"petsAllowed" : true
},
"reviews" : [
{
"reviewer" : "Josephine Thomas",
"date" : "2018-03-05T03:03:53 -01:00",
"cleanliness" : 2,
"location" : 1,
"food" : 6
}
]
}
}