I have a RoR application which will read info from a MongoDB collection.
I have my model set as:-
class Vulnerability
include Mongoid::Document
field :id, type: String
field :description, type: String
field :type, type: String
end
The first record in the collection is formatted like this:-
> db.vulnerabilities.findOne()
{
"_id" : "NGINX:CVE-2009-3896",
"_index" : "bulletins",
"_type" : "bulletin",
"_score" : null,
"_source" : {
"lastseen" : "2016-09-26T17:22:32",
"references" : [ ],
"edition" : 1,
"description" : "Null pointer dereference vulnerability\nSeverity: major\nCVE-2009-3896\nNot vulnerable: 0.8.14+, 0.7.62+, 0.6.39+, 0.5.38+\nVulnerable: 0.1.0-0.8.13",
"reporter" : "Nginx",
"published" : "2009-11-24T12:30:00",
"type" : "nginx",
"title" : "Null pointer dereference vulnerability",
"bulletinFamily" : "software",
"affectedSoftware" : [
{
"name" : "nginx",
"version" : "0.8.13",
"operator" : "le"
}
],
"cvelist" : [
"CVE-2009-3896"
],
"modified" : "2009-11-24T12:30:00",
"id" : "NGINX:CVE-2009-3896",
"href" : "http://nginx.org/en/security_advisories.html",
"cvss" : {
"score" : 5,
"vector" : "AV:NETWORK/AC:LOW/Au:NONE/C:NONE/I:NONE/A:PARTIAL/"
}
},
"sort" : [
38985
]
}
I want to pull the ID, the type and the description. When trying to view the index view I get
NameError at /vulnerabilities uninitialized constant Bulletin
I believe the issue is with the type in the document as there is a key of _type but I want to get the type from the key _source, so in the example above it will display nginx not bulletin.
I use MongoID and PostgreSQL on Rails 4 and I have a notification system.
Each user has one notification document. I would like to be able to mark all as read in a controller.
Here is my notification schema :
{
"_id" : ObjectId("53e360156d6163513e000000"),
"unread_counter" : 1,
"user_id" : 1,
"rows" : [
{
"_id" : ObjectId("53e369166d61635328000000"),
"readed" : false,
"created_at" : ISODate("2014-08-07T11:55:02.936Z"),
"author" : {
"_id" : ObjectId("53e369166d61635328010000"),
"username" : "edouard",
"user_id" : 6
},
"subject" : {
"_id" : ObjectId("53e369166d61635328020000"),
"type" : "follower"
}
}
]
}
The problem is that elem_match just match one element in rows array.
Notification.where(:user_id => current_user.id)
.elem_match(rows: { readed: false })
.update_all("$set" => {"rows.$.readed" => true })
I have been inspired by this post : mongoid update elements within array
How can I match all "readed" : false row elements and then update them to true ?
What I'm trying to do is to sort mongoid geo_near(or within_circle) max_distance results by distance because I don't know why but it doesn't do that by default.
I have mongoid_geospatial, mongoid_spacial and rgeo in my Rails gem file. I know mongoid_spacial has this ability but I could not use it and it caused problems with gmaps4rails.
I'm trying on mongoid_geospatial(which uses mongoid libraries) with no success and I could not find any resource except this one https://stackoverflow.com/questions/18633636/mongodb-aggregate-geonear-maxdistance but I don't know how to convert mongo that to mongoid.
Any one had an experience on sorting geo_near or within_circle in mongoid? Any help will be greatly appreciated.
The code in my Controller
searchterm = session[:categoryid].to_s
radius = session[:distance].to_f / 10
#places = Provider.all.where(:category.to_s => /.*#{searchterm}.*/)
.geo_near([ session[:latitude].to_f,session[:longitude].to_f ]).max_distance(radius)
##places = Provider.all.where(:category.to_s => /.*#{searchterm}.*/)
#.within_circle(location: [[ session[:latitude].to_f,session[:longitude].to_f ], radius ])
#.sort(:servicescore.desc).sort(:pricescore.desc)
The code in my model
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
include Mongoid::Geospatial
include Mongoid::MultiParameterAttributes
include Mongoid::Slug
include Mongoid::Spacial::Document
include Gmaps4rails::ActsAsGmappable
acts_as_gmappable :lat => 'latitude', :lon => 'longitude', :process_geocoding => true,
:check_process => :prevent_geocoding,
:address => "business_address"
field :location, :type => Point
spatial_index :location
field :officialname
field :business_description
field :category#, :type => Array
field :business_type
field :tax_office
field :tax_number
field :pin
field :business_phone
field :web_site
field :business_address
field :latitude
field :longitude
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
EDIT ON QUESTION
I've tried almost anything related without success. Read mongo and mongoid geo_near documentation. It does comment on storing location [lon, lat] order. I've tried both [lon, lat] and [lat, long] both when saving the provider and for the center location. Tried spherical geo_near and within_circle too. Still without success.
Geo_near returns my results incorrectly ordered as seen on the map above and on the list below. There should be something I'm missing but can't find it.
The red parts are the "geo_near_distance" output results, the list is sorted according to those but they are wrong as seen on the map. The green calculation is jquery code I've been using that returns correct distances(though cannot use in controller).
There should be something I'm missing. Can anyone spot that?
here is my latest code:
Provider.rb
field :location, :type => Array # [lat,lng]
index({ location: "2d" }, { min: -180, max: 180 })
spatial_index :location
Providers_controller
center = [session[:longitude].to_f, session[:latitude].to_f]
radious = 100
searchterm = session[:categoryid].to_s
radius = session[:distance].to_f / 10
#places = Provider.all.where(:category.to_s => /.*#{searchterm}.*/)
.geo_near(center).max_distance(radious)#.spherical
#.near(location: center)
#.within_circle( location: [center,radius] )
#.sort(:geo_near_distance.desc)
#.geo_near([ session[:latitude].to_f,session[:longitude].to_f ]).max_distance(100).spherical
#.unit("km")
#.geo_near([ session[:latitude].to_f,session[:longitude].to_f ], max_distance: radius, unit: "km".to_sym, spherical: true).sort_by!{|r| r.geo[:distance] }
#.geo_near([ session[:latitude].to_f,session[:longitude].to_f ]).max_distance(radius)
#.sort(:geo_near_distance)
JQuery for the green part
var p1 = new LatLon(Geo.parseDMS($('#lat1_{{id}}').val()), Geo.parseDMS($('#lon1_{{id}}').val()));
var p2 = new LatLon(Geo.parseDMS($('#lat2_{{id}}').val()), Geo.parseDMS($('#lon2_{{id}}').val()));
var new_number = parseFloat(p1.distanceTo(p2)).toFixed(2);
$('#result-distance_{{id}}').html(new_number+' km');
Data for Provider
{ "_id" : ObjectId("5295ef0fdd5063ce1600002a"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [ "5295ef0fdd5063ce1600002a-1" ], "officialname" : "çeşme", "business_description" : "dsadsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dads", "tax_number" : "dss", "pin" : "", "business_address" : "Balçova/İzmir, Türkiye", "latitude" : 38.3692939, "longitude" : 27.093442, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [ 27.093441999999982, 38.3692939 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:09:36.214Z"), "created_at" : ISODate("2013-11-27T13:09:36.214Z") }
{ "_id" : ObjectId("5295f248dd5063ce16000030"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [ "5295f248dd5063ce16000030-1" ], "officialname" : "izmirdsads", "business_description" : "dsdsadsdsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dasds", "tax_number" : "dadsa", "pin" : "", "business_address" : "Çeşme, Türkiye", "latitude" : 38.32980999999999, "longitude" : 26.3149209, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [ 26.31492090000006, 38.32980999999999 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:23:20.423Z"), "created_at" : ISODate("2013-11-27T13:23:20.423Z") }
{ "_id" : ObjectId("5295f260dd5063ce16000032"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [ "5295f260dd5063ce16000032-1" ], "officialname" : "dsadsa", "business_description" : "dsadsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dsadsa", "tax_number" : "dsads", "pin" : "", "business_address" : "Çiğli, 35580 İzmir, Türkiye", "latitude" : 38.496303, "longitude" : 27.0603911, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [ 27.06039110000006, 38.496303 ], "gmaps" : true, "provider_image" : "gmaps1.jpg", "updated_at" : ISODate("2013-11-27T13:23:44.686Z"), "created_at" : ISODate("2013-11-27T13:23:44.686Z") }
{ "_id" : ObjectId("5295eee3dd5063ce16000026"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [ "5295eee3dd5063ce16000026-1" ], "officialname" : "dsdsa", "business_description" : "dadsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dsa", "tax_number" : "dads", "pin" : "", "business_address" : "Buca, İzmir, Türkiye", "latitude" : 38.38813400000001, "longitude" : 27.1753358, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [ 27.291124800000034, 38.3337361 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:08:51.544Z"), "created_at" : ISODate("2013-11-27T13:08:51.544Z") }
{ "_id" : ObjectId("5295eef5dd5063ce16000028"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [ "5295eef5dd5063ce16000028-1" ], "officialname" : "dsdsdsa", "business_description" : "dadsds", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dasds", "tax_number" : "dadsa", "pin" : "", "business_address" : "Bornova, 35100 İzmir, Türkiye", "latitude" : 38.466414, "longitude" : 27.2192191, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [ 27.219219100000032, 38.466414 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:09:10.122Z"), "created_at" : ISODate("2013-11-27T13:09:10.122Z") }
I used it few months ago. This is how I used it:
class Person
include Mongoid::Document
field :location, :type => Array # [lat,lng]
index( { location: Mongo::GEO2D }, { min: -180, max: 180 })
end
Then you need to generate indexes:
rake db:mongoid:create_indexes
Then you can make the query:
center = [ 1, 10 ]
radious = 100
Person.within_circle( location: [ center, radious ] )
Or use $near query:
Person.geo_near(center).max_distance(radious) # sorted by distance
All information is specified here:
http://mongoid.org/en/mongoid/docs/indexing.html
http://mongoid.org/en/origin/docs/selection.html
http://mongoid.org/en/mongoid/docs/querying.html#geo_near
Good luck!
I have a data strcuture like this
Courses
{
"name" : "Course1",
"student_id"
"subjects" :
[{
"level" : "1",
"short_name" : "Maths",
"topics" : [{
"name" : "Algebra 101",
"week" : "1",
"submission_week" : ISODate("2013-07-28T00:00:00Z"),
"correction_week" : ISODate("2013-07-28T00:00:00Z")
},
{
"name" : "Algebra 201",
"week" : "1",
"submission_week" : ISODate("2013-07-28T00:00:00Z"),
"correction_week" : ISODate("2013-07-28T00:00:00Z")
}
]},
{
"level" : "2",
"short_name" : "Chem"
}
]
}
Using Mongoid I am trying to retrieve all topics.
I have tried all sorts of queries but cannot seem get it.
e.g I don't understand why this doesn't work?
Topic.where( name: "Algebra 101", 'subject.short_name' =>"Maths", 'subject.course.name' =>"Course1")
Can I query like this?
My ruby code is
class Course
embeds_many :subjects
class Subject
embedded_in :course
embeds_many :topics
class Topic
embedded_in :subject
A far as I know it's only possible to make such a query on the top-most model (in this case Course); I have not seen a way to directly obtain an embedded model like that yet.
So this should work, but only gives you the Course:
Course.where(name: 'Course1', 'subjects.short_name' => 'Maths', 'subjects.topics.name' => "Algebra 101")
And this is the (unfortunately rather ugly) query that should give you what you want:
Course.where(name: 'Course1').subjects.where(short_name: 'Maths').topics.where(name: 'Algebra 101')
i have a rails app where i have some problems with indexes. I search locations by name.
First i thought its a problem with the addresses.coords but iam not sure about it.
The relevant parts of the search controller:
#practices = Practice.published
#practices = #practices.where(:"addresses.country" => params[:country].upcase) if params[:country].present?
if params[:location].present? && latlng = get_coordinates
#practices = #practices.near_sphere(:"addresses.coords" => latlng).max_distance(:"addresses.coords" => get_distance )
end
# now find doctors based on resulting practices
#doctors = Doctor.published.in("organization_relations.practice_id" => #practices.distinct(:_id))
The complete crash log:
Moped::Errors::OperationFailure (The operation: #<Moped::Protocol::Command
#length=255
#request_id=646
#response_to=0
#op_code=2004
#flags=[]
#full_collection_name="um-contacts.$cmd"
#skip=0
#limit=-1
#selector={:distinct=>"practices", :key=>"_id", :query=>{"deleted_at"=>nil, "published_at"=>{"$lte"=>2012-11-05 15:17:14 UTC}, "addresses.country"=>"DE", "addresses.coords"=>{"$nearSphere"=>[13.4060912, 52.519171], "$maxDistance"=>0.01569612305760477}}}
#fields=nil>
failed with error 13038: "exception: can't find special index: 2d for: { deleted_at: null, published_at: { $lte: new Date(1352128634313) }, addresses.country: \"DE\", addresses.coords: { $nearSphere: [ 13.4060912, 52.519171 ], $maxDistance: 0.01569612305760477 } }"
See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.):
app/controllers/search_controller.rb:16:in `index'
Thats the result of the indexes, not sure how to query them from the addresses which are embedded via has_many.
> db.practices.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "um-contacts.practices",
"name" : "_id_"
}
]
Help would be really appreciated!
Edit: Looks like the indexes for adresses.coords arent created,
db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.users", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.doctors", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.collaborations", "name" : "_i
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.practices", "name" : "_id_" }
but should be created within the practice class:
class Practice
...
embeds_many :addresses, cascade_callbacks: true, as: :addressable
...
field :name, type: String
field :kind, type: String
field :slug, type: String
index({"addresses.coords" => '2d'}, { min: -180, max: 180, background: true })
index({name: 1})
index({slug: 1}, { unique: true })
...
Anyone have an idea why its failing?
try to re-create your indexes. for mongoid:
rake db:mongoid:create_indexes