How to make projections of selected attributes in mongoid? - ruby-on-rails

I have Coordinate model with many markers embedded_in User model. How to extract attributes without _ids so that on output display only each lng and lat?
structure:
{ "_id" : ObjectId( "4e9f418f1e7bf20fbc000009" ),
"name" : "test",
"coordinates" : [
{ "lng" : 16.86783310009764,
"lat" : 52.38353842845282,
"_id" : ObjectId( "4e9f418f1e7bf20fbc00000a" ) },
{ "lng" : 16.85787674023436,
"lat" : 52.40972501601293,
"_id" : ObjectId( "4e9f418f1e7bf20fbc00000b" ) },
{ "lng" : 16.92276474072264,
"lat" : 52.40071858320756,
"_id" : ObjectId( "4e9f418f1e7bf20fbc00000c" ) },
{ "lng" : 16.90182205273436,
"lat" : 52.38270020105396,
"_id" : ObjectId( "4e9f418f1e7bf20fbc00000d" ) },
{ "lng" : 16.96705337597655,
"lat" : 52.410661698108,
"_id" : ObjectId( "4e9f418f1e7bf20fbc00000e" ) },
{ "lng" : 16.89495559765624,
"lat" : 52.42773236584494,
"_id" : ObjectId( "4e9f418f1e7bf20fbc00000f" ) } ] }
e.g.
= debug #user.coordinates.to_json
gives:
--- ! '[{"_id":"4e9f418f1e7bf20fbc00000a","lat":52.383538428452816,"lng":16.86783310009764},{"_id":"4e9f418f1e7bf20fbc00000b","lat":52.40972501601293,"lng":16.85787674023436},{"_id":"4e9f418f1e7bf20fbc00000c","lat":52.40071858320756,"lng":16.92276474072264},{"_id":"4e9f418f1e7bf20fbc00000d","lat":52.382700201053964,"lng":16.90182205273436},{"_id":"4e9f418f1e7bf20fbc00000e","lat":52.410661698108,"lng":16.967053375976548},{"_id":"4e9f418f1e7bf20fbc00000f","lat":52.42773236584494,"lng":16.894955597656235}]'
expected:
--- ! '[{"lat":52.383538428452816,"lng":16.86783310009764},{"lat":52.40972501601293,"lng":16.85787674023436},{"lat":52.40071858320756,"lng":16.92276474072264},{"lat":52.382700201053964,"lng":16.90182205273436},{"lat":52.410661698108,"lng":16.967053375976548},{"lat":52.42773236584494,"lng":16.894955597656235}]'

Try using except
#user.coordinates.to_json(:except => '_id')

the accepted answer returns data you wished. I would suggest though to use without against mongoid query so you actually do not fetch the data from DB, which is ideal.
#user.coordinates.without('_id').to_json(:except => '_id')

Related

Get multiple strings from JSON

So the JSON variable let json = JSON(nearbyChargingSites.jsonString!) contains the current data.:
{
"timestamp" : 1626902257093,
"superchargers" : [
{
"location" : {
"lat" : 63.325319,
"long" : 10.305137
},
"total_stalls" : 19,
"distance_miles" : 10.064082000000001,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 15,
"name" : "Leinstrand, Norway - Klett"
},
{
"location" : {
"lat" : 63.466445999999998,
"long" : 10.91766
},
"total_stalls" : 16,
"distance_miles" : 11.838984999999999,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 16,
"name" : "Stjørdal, Norway"
},
{
"location" : {
"lat" : 63.734355000000001,
"long" : 11.281487
},
"total_stalls" : 12,
"distance_miles" : 31.206503999999999,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 11,
"name" : "Levanger, Norway"
},
{
"location" : {
"lat" : 62.832030000000003,
"long" : 10.009639999999999
},
"total_stalls" : 20,
"distance_miles" : 44.117753,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 17,
"name" : "Berkåk, Norway"
}
],
"congestion_sync_time_utc_secs" : 1626902199,
"destination_charging" : [
{
"distance_miles" : 23.366278999999999,
"name" : "Bårdshaug Herregård",
"location" : {
"lat" : 63.299208,
"long" : 9.8448650000000004
},
"type" : "destination"
},
{
"distance_miles" : 38.390034,
"name" : "Fosen Fjordhotel",
"location" : {
"lat" : 63.959356999999997,
"long" : 10.223908
},
"type" : "destination"
},
{
"distance_miles" : 46.220022999999998,
"name" : "Falksenteret",
"location" : {
"lat" : 63.293301999999997,
"long" : 9.0834460000000004
},
"type" : "destination"
},
{
"distance_miles" : 54.981445000000001,
"name" : "Væktarstua",
"location" : {
"lat" : 62.908683000000003,
"long" : 11.893306000000001
},
"type" : "destination"
}
]
}
I use SwiftyJSON and tries to get the superchargers latitude, longitude and name, like this:
let jsonName = json["superchargers"]["name"]
let jsonLat = json["superchargers"]["location"]["lat"]
let jsonLong = json["superchargers"]["location"]["long"]
When trying to print any of those, all of them return nil.
Any ideas what I am doing wrong, and how to do this?
The reason I want to do this is because I want to add them as annotation to a MKMapView.
The first error is that the JSON initializer you are using will create a single JSON string object, it will not parse the string as JSON data:
Instead of:
let json = JSON(nearbyChargingSites.jsonString!)
you need to use:
let json = JSON(data: dataFromJSONString)
Second you need to iterate over the superchargers array to collect all the values
Try something like:
if let dataFromString = nearbyChargingSites.jsonString!.data(using: .utf8, allowLossyConversion: false) {
let json = try! JSON(data: dataFromString,options: .allowFragments)
for supercharger in json["superchargers"].arrayValue {
let jsonName = supercharger["name"].stringValue
let jsonLat = supercharger["location"]["lat"].doubleValue
let jsonLong = supercharger["location"]["long"].doubleValue
}
}
Please note that the above code does not perform error handling and will crash if values are missing from JSON.

Spring Data Neo4j corrupted json

I'm using neo4j with spring data, when I store an object with inside a relation I get as a result of the findAll a corrupted json. I never get this error when I query the objects one at time.
Even more strange the first one in the list is correct but the second has this error. The error is at the edge field. Any idea?
[{
"uuid" : "e5c90af5-6259-4ddf-ae1f-c0cff5a41296",
"name" : "test",
"createdBy" : {
"uuid" : "319535cc-288f-4a23-bc02-a3b01bf6e93f",
"createdAt" : "2017-03-10T02:06:55.925+0000",
"user" : {
"uuid" : "9e91032e-a54d-4297-8a6a-1506589b7529",
},
"edge" : { "id" : 6514
},
"graphId" : 664
}
},
{
"uuid" : "e5c90af5-6259-4ddf-ae1f-c0cff5a41296",
"name" : "test",
"createdBy" : {
"uuid" : "319535cc-288f-4a23-bc02-a3b01bf6e93f",
"createdAt" : "2017-03-10T02:06:55.925+0000",
"user" : {
"uuid" : "9e91032e-a54d-4297-8a6a-1506589b7529",
},
"edge" : { : 6514
},
"graphId" : 664
}
}]

How to boost the closest created_at field in Elasticsearch?

I want to sort my query results following some boost rules and in the same time i want them to be sorted as possible by creation date, if i add a created_at sort, it changes everything and my results are not relevant anymore. So i guess the only way to do that is to boost created_at field (the newest has the biggest bonus in calculating score for that boost) but i dont know how to implement it. This is my query:
query = {
"query" : {
"bool" : {
"must" : [
{
"range" : {
"deadline" : {
"gte" : "2016-05-30T11:39:10+02:00"
}
}
},
{
"terms" : {
"state" : [
"open"
]
}
},
{
"query_string" : {
"query" : "chant",
"default_operator" : "AND",
"analyzer" : "search_francais",
"fields" : [
"title^6",
"description",
"brand",
"category_name"
]
}
}
]
}
},
"filter" : {
"and" : [
{
"geo_distance" : {
"distance" : "40km",
"location" : {
"lat" : 48.855736,
"lon" : 2.32927300000006
}
}
}
]
},
"sort" : [
{
"_score" : "desc"
},
#{
# "created_at" : "desc" ==> i tried this but it doesnt change results
#}
]
}
Try adding your condition in should block.
i)If the created date should be closer to come value in the search query or you have any idea on how close the date should be, give a range query.
ii) If you are not sure of all those values, decay function can be used. In this case, query shall be changed to function query.
{
"query" : {
"bool" : {
"must" : [
{
"range" : {
"deadline" : {
"gte" : "2016-05-30T11:39:10+02:00"
}
}
},
{
"terms" : {
"state" : [
"open"
]
}
},
{
"query_string" : {
"query" : "chant",
"default_operator" : "AND",
"analyzer" : "search_francais",
"fields" : [
"title^6",
"description",
"brand",
"category_name"
]
}
}
],
"should": [
{"created_at" : "condition here .. "}
]
}
},
"filter" : {
"and" : [
{
"geo_distance" : {
"distance" : "40km",
"location" : {
"lat" : 48.855736,
"lon" : 2.32927300000006
}
}
}
]
}
}

Why my PassBook isn't valid or outdate?

I generate one passbook using this gem in rails and it seen that works but when I open the passbook .pkpass file I see this message:
It's in spanish but basically it says that this card isn't valid anymore.
Here is my JSON:
{
"formatVersion" : 1,
"passTypeIdentifier" : "{MY PASS ID HERE}",
"serialNumber" : "E5982H-I2",
"teamIdentifier" : "{MY TEAM ID HERE}",
"webServiceURL" : "https://example.com/passes/",
"authenticationToken" : "vxwxd7J8AlNNFPS8k0a0FfUFtq0ewzFdc",
"barcode" : {
"message" : "123456789",
"format" : "PKBarcodeFormatPDF417",
"messageEncoding" : "iso-8859-1"
},
"locations" : [
{
"longitude" : -122.3748889,
"latitude" : 37.6189722
},
{
"longitude" : -122.03118,
"latitude" : 37.33182
}
],
"organizationName" : "CROCANTICKETS SL",
"description" : "Paw Planet Coupon",
"logoText" : "Paw Planet",
"foregroundColor" : "rgb(255, 255, 255)",
"backgroundColor" : "#FF4B33",
"coupon" : {
"primaryFields" : [
{
"key" : "offer",
"label" : "Any premium dog food",
"value" : "20% off"
}
],
"auxiliaryFields" : [
{
"key" : "expires",
"label" : "EXPIRES",
"value" : "2016-04-24T10:00-05:00",
"isRelative" : true,
"dateStyle" : "PKDateStyleShort"
}
]
}
}
Any idea? Thanks!
According to the Expiration Keys in the Passbook Package Format Reference check the expirationDate and voided keys. Since you do not have those in your JSON, it might be added by the gem you are using.

Step-by-Step navigation by using GPS in iOS sdk [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want Step by Step navigation by using GPS with google Maps and how to achieve that one.
Any help really appreciated
Thank you
you need a google API Services and they provide full Route path when you give Startpoint and Destination point and call the api with the following
http://maps.google.com/maps/api/directions/json?sensor=true&origin=23.027439,72.523089&destination=23.029108,72.507570
here my startingpoint is origin=23.027439,72.523089 and destintaion is destination=23.029108,72.507570
so now google will provide following data
{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 23.0296906,
"lng" : 72.5234184
},
"southwest" : {
"lat" : 23.026354,
"lng" : 72.50715599999999
}
},
"copyrights" : "Map data ©2014 Google",
"legs" : [
{
"distance" : {
"text" : "2.2 km",
"value" : 2249
},
"duration" : {
"text" : "5 mins",
"value" : 325
},
"end_address" : "Sarkhej - Gandhinagar Highway, Satellite, Ahmedabad, Gujarat 380054, India",
"end_location" : {
"lat" : 23.0290924,
"lng" : 72.5076297
},
"start_address" : "38, Indus Park, Satellite, Ahmedabad, Gujarat 380015, India",
"start_location" : {
"lat" : 23.027439,
"lng" : 72.5230894
},
"steps" : [
{
"distance" : {
"text" : "68 m",
"value" : 68
},
"duration" : {
"text" : "1 min",
"value" : 24
},
"end_location" : {
"lat" : 23.0269233,
"lng" : 72.5227287
},
"html_instructions" : "Head \u003cb\u003esouthwest\u003c/b\u003e toward \u003cb\u003eSatellite Rd\u003c/b\u003e",
"polyline" : {
"points" : "oppkCitsyLXPJFh#\\VN"
},
"start_location" : {
"lat" : 23.027439,
"lng" : 72.5230894
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "85 m",
"value" : 85
},
"duration" : {
"text" : "1 min",
"value" : 30
},
"end_location" : {
"lat" : 23.0265039,
"lng" : 72.5234184
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eSatellite Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Hanuman Mandir (on the left)\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "gmpkCarsyLVa#`#u#Pc#FM"
},
"start_location" : {
"lat" : 23.0269233,
"lng" : 72.5227287
},
"travel_mode" : "DRIVING"
},
you have to understand the documentation for using google direction services for that here is the link https://developers.google.com/maps/documentation/javascript/directions.
here is a simple logic you can use after that.
what you have to do is to store this steps dictionaryArray in array and then try to fetch it one by one.
Check didUpdateToLocation method for current Lat Long and if your current location is near by the vlaue of step Dcitionay's endLocation point and if its near by then you can alert the message of html_instructions Value that which direction user has to go next and increase your index for steps array dictionary and same do it for the last index of steps.
if need any help feel free to ask.

Resources