DoctrineMongo near query - mongodb

I have symfony 2 + doctrineMongo and i'm trying to use the geospatial query ->near but seems that this query don't work. Always return an empty array.
I followed this guide: http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/geospatial-queries.html
And i have this query in my repository:
$this->createQueryBuilder()
->field('coordinates')->near($longitude, $latitude)
->getQuery()
->execute();
There is a bug? How i can fix it?
My places findAll:
db.Place.find();
{ "_id" : ObjectId("4e4b82df3eee4f7e2c000000"), "coordinates" : { "latitude" : 23.1, "longitude" : 23.23 }, "name" : "Opium Mar" }
{ "_id" : ObjectId("4e5769f43eee4fc002000000"), "name" : "Sutton club", "coordinates" : { "latitude" : 2, "longitude" : 1 } }
{ "_id" : ObjectId("4e5cf2173eee4fc202000008"), "name" : "Scorpia", "coordinates" : { "latitude" : 23, "longitude" : 22 } }
And this is my index:
db.system.indexes.find();
{ "name" : "_id_", "ns" : "kzemos.User", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.Place", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.Party", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.Friend", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.UserParty", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.Invite", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.Photo", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.Group", "key" : { "_id" : 1 }, "v" : 0 }
{ "name" : "_id_", "ns" : "kzemos.places", "key" : { "_id" : 1 }, "v" : 0 }
{ "_id" : ObjectId("4e5deaced3c5c27e84059447"), "ns" : "kzemos.places", "key" : { "loc" : "2d" }, "name" : "loc_", "bits" : 26 }
{ "_id" : ObjectId("4e5dead9d3c5c27e84059448"), "ns" : "kzemos.places", "key" : { "coordinates" : "2d" }, "name" : "coordinates_", "bits" : 26 }`
When i use the near query in mongo shell i get this error:
db.Place.find( { coordinates : { $near : [50,50] } } )
error: {
"$err" : "can't find special index: 2d for: { coordinates: { $near: [ 50.0, 50.0 ] } }",
"code" : 13038
}
Thank you!

You need to have a Geospartial index on you collection like it is explained here http://www.mongodb.org/display/DOCS/Geospatial+Indexing

Related

mongodb findOne not found document in base

in my collection i have this document:
{
"_id" : ObjectId("5eecb84a9e41ff609fd6389a"),
"uid" : NumberLong(619942065802969109),
"banmute" : 0,
"expire" : ISODate("2023-03-15T13:06:18.694Z"),
"fid" : "3cac4490b6ca491e838d4e5317e5b87e",
"id" : null,
"nick" : "Flawe",
"nicks_ld" : "",
"old_nicks" : "",
"reason" : ""
}
Indexes is:
/* 1 */
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "fsl.index_profile"
},
{
"v" : 2,
"unique" : true,
"key" : {
"uid" : 1
},
"name" : "uid_1",
"ns" : "fsl.index_profile",
"background" : true
}
]
On direct request i have null answer:
db.getCollection('index_profile').findOne({uid: 619942065802969109})
result: ->
null
But if i request $gte i found it:
db.getCollection('index_profile').find({uid: {$gte: 619942065802969109}}).limit(1)
result: ->
/* 1 */
{
"_id" : ObjectId("5eecb84a9e41ff609fd6389a"),
"uid" : NumberLong(619942065802969109),
"banmute" : 0,
"expire" : ISODate("2023-03-15T13:06:18.694Z"),
"fid" : "3cac4490b6ca491e838d4e5317e5b87e",
"id" : null,
"nick" : "Flawe",
"nicks_ld" : "",
"old_nicks" : "",
"reason" : ""
}
I tried deleting the cache, rebooting the server, deleting indexes, assigned different new indexes
I am in despair, help solve this problem
have you tried:
db.getCollection('index_profile').findOne({uid: NumberLong(619942065802969109)})

MongoDB dataset : pairs not reducing or problem with script

I'm new to programming and mongoDB and learning as I go, I'm attempting a mapreduce on a dataset using mongoDB. So far I've converted the csv to json and imported it into a mongoDB using compass.
In compass the data now looks like this :
_id :5bc4e11789f799178470be53
slug :"bitcoin"
symbol :"BTC"
name :"Bitcoin"
date :"2013-04-28"
ranknow :"1"
open :"135.3"
high :"135.98"
low :"132.1"
close :"134.21"
volume :"0"
market :"1500520000"
close_ratio :"0.5438"
spread :"3.88"
I've added each value as indices as follows, is this the right process so I can run a mapreduce against the data ?
db.testmyCrypto.getIndices()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "id",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"slug" : 1
},
"name" : "slug_1",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"symbol" : 2
},
"name" : "symbol_2",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"name" : 3
},
"name" : "name_3",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"data" : 4
},
"name" : "data_4",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"ranknow" : 4
},
"name" : "ranknow_4",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"ranknow" : 5
},
"name" : "ranknow_5",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"open" : 6
},
"name" : "open_6",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"high" : 7
},
"name" : "high_7",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"low" : 8
},
"name" : "low_8",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"volume" : 9
},
"name" : "volume_9",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"market" : 10
},
"name" : "market_10",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"close_ratio" : 11
},
"name" : "close_ratio_11",
"ns" : "myCrypto.testmyCrypto"
},
{
"v" : 2,
"key" : {
"spread" : 13
},
"name" : "spread_13",
"ns" : "myCrypto.testmyCrypto"
}
]
I've scraped the above and now im doing the following from the link to the map-reduce. Is this the correct output, someone ?
> db.testmyCrypto.mapReduce(function() { emit( this.slug, this.symbol ); }, function(key, values) { return Array.sum( values ) },
... {
... query: { date:"2013-04-28" },
... out: "Date 04-28"
... }
... )
{
"result" : "Date 04-28",
"timeMillis" : 837,
"counts" : {
"input" : 0,
"emit" : 0,
"reduce" : 0,
"output" : 0
},
"ok" : 1
}
I've added the "key value pairs" but I don't seem to be able to get anything from the data.
> db.testmyCrypto.mapReduce(function() { emit( this.slug, this.symbol, this.name, this.date, this.ranknow, this.open, this.high, this.low, this.close, this.volume, this.market, this.close_ratio, this.spread ); }, function(key, values) { return Array.sum( values ) }, { query: { slug:"bitcoin" }, out: "Date 04-28" } )
{
"result" : "Date 04-28",
"timeMillis" : 816,
"counts" : {
"input" : 0,
"emit" : 0,
"reduce" : 0,
"output" : 0
},
"ok" : 1 }
>
if you trying to sum some values then they need to numeric (when you importing data to mongo try to set type for values)
db.collectionName.mapReduce(
function() {
emit(
this.slug,
this.open
)
},
function(keySlug, valueOpen) {
return Array.sum(valueOpen)
},
{
query: { date:"2013-04-28" },
out: "Date 04-28"
}
)
this query will return you sum of open values for each slug filtered by date.
ps. you can do same thing with aggregation.
if you have any question let me know.

Issue When I set shard key

I have a collection and I set shard it.
I have errors while I add a shard key!!!
mongos> sh.shardCollection('IBSng.connection_log', {login_time:1})
But I was shown this error:
Even I set compound shard with logout_time field with login_time, But the result shown to me this error.
{
"proposedKey" : {
"login_time" : 1
},
"curIndexes" : [
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"user_id" : 1
},
"name" : "user_id_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"ras_id" : 1
},
"name" : "ras_id_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"retry_count" : 1
},
"name" : "retry_count_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"credit_used" : 1
},
"name" : "credit_used_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"details.mac" : 1
},
"name" : "details.mac_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"username" : 1
},
"name" : "username_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"type_details.in_bytes" : 1,
"type_details.out_bytes" : 1
},
"name" : "type_details.in_bytes_1_type_details.out_bytes_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"details.kill_reason" : 1
},
"name" : "details.kill_reason_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"details.terminate_cause" : 1
},
"name" : "details.terminate_cause_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"login_time" : -1,
"logout_time" : -1
},
"name" : "login_time_-1_logout_time_-1",
"ns" : "IBSng.connection_log",
"sparse" : false
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
}
I Waiting for your answers.
Add an index for {login_time:1}, this is different to that compound one you have done.

Compound GeoSpatial Index in MongoDB is not working as intended

the collection nodesWays has the following indexes:
> db.nodesWays.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "h595.nodesWays"
},
{
"v" : 1,
"key" : {
"amenity" : 1,
"geo" : "2dsphere"
},
"name" : "amenity_1_geo_2dsphere",
"ns" : "h595.nodesWays",
"2dsphereIndexVersion" : 2
},
{
"v" : 1,
"key" : {
"geo" : "2dsphere"
},
"name" : "geo_2dsphere",
"ns" : "h595.nodesWays",
"2dsphereIndexVersion" : 2
}
]
Now the following two queries should return the same result, but they don't.
I want the nearest 10 restaurants to the specified point.
The first query is working how it should be, the second is not working like intended.
The only difference between these two queries is that the first one uses the geo_2dsphere-Index
and the second query the amenity_1_geo_2dsphere-Index.
> db.nodesWays.find(
{
geo:{
$nearSphere:{
$geometry:{
type: "Point", coordinates: [9.7399777,52.3715156]
}
}
}, "amenity":"restaurant",
name: {$exists: true}
}, {id:1, name:1}).hint( "geo_2dsphere" ).limit(10)
{ "_id" : ObjectId("53884860e552e471be2b7192"), "id" : "321256694", "name" : "Masa" }
{ "_id" : ObjectId("53884860e552e471be2b7495"), "id" : "323101271", "name" : "Bavarium" }
{ "_id" : ObjectId("53884862e552e471be2ba605"), "id" : "442496282", "name" : "Naxos" }
{ "_id" : ObjectId("53884860e552e471be2b7488"), "id" : "323101189", "name" : "Block House" }
{ "_id" : ObjectId("53884878e552e471be2d1a41"), "id" : "2453236451", "name" : "Maestro" }
{ "_id" : ObjectId("53884870e552e471be2c8aab"), "id" : "1992166428", "name" : "Weinstube Leonardo Ristorante" }
{ "_id" : ObjectId("53884869e552e471be2c168b"), "id" : "1440320284", "name" : "Altdeutsche küche" }
{ "_id" : ObjectId("53884861e552e471be2b88f7"), "id" : "353119010", "name" : "Mövenpick" }
{ "_id" : ObjectId("5388485de552e471be2b2c86"), "id" : "265546900", "name" : "Miles" }
{ "_id" : ObjectId("53884863e552e471be2bb5d3"), "id" : "532304135", "name" : "Globetrotter" }
> db.nodesWays.find(
{
geo:{
$nearSphere:{
$geometry:{
type: "Point", coordinates: [9.7399777,52.3715156]
}
}
}, "amenity":"restaurant",
name: {$exists: true}
}, {id:1, name:1}).hint( "amenity_1_geo_2dsphere" ).limit(10)
{ "_id" : ObjectId("53884875e552e471be2cf4a8"), "id" : "2110027373", "name" : "Schloßhof Salder" }
{ "_id" : ObjectId("5388485be552e471be2aff19"), "id" : "129985174", "name" : "Balkan Paradies" }
{ "_id" : ObjectId("5388485be552e471be2afeb4"), "id" : "129951134", "name" : "Asia Dragon" }
{ "_id" : ObjectId("53884863e552e471be2ba811"), "id" : "450130115", "name" : "Kings Palast" }
{ "_id" : ObjectId("53884863e552e471be2ba823"), "id" : "450130135", "name" : "Restaurant Montenegro" }
{ "_id" : ObjectId("53884877e552e471be2d053a"), "id" : "2298722569", "name" : "Pizzaria Da-Lucia" }
{ "_id" : ObjectId("53884869e552e471be2c152e"), "id" : "1420101752", "name" : "Napoli" }
{ "_id" : ObjectId("5388485be552e471be2b0028"), "id" : "136710095", "name" : "Europa" }
{ "_id" : ObjectId("53884862e552e471be2ba5bc"), "id" : "442136241", "name" : "Syrtaki" }
{ "_id" : ObjectId("53884863e552e471be2ba763"), "id" : "447972565", "name" : "Pamukkale" }
My goal with the second index is to:
select all restaurants
then use the nearSphere-Operator to sort them in regards to the distance from the specified point
Auf Wiedersehen
I think you should try to put the geolocation first in the index.

MongoDB : geospatial index not getting added

I have a mongo collection where each document has a 'loc' array as follows:
> db.trucks.findOne()
{
"_id" : ObjectId("52afe2a9e8de3f311ec675ee"),
"objectid" : "427856",
"fooditems" : "Cupcakes",
"facilitytype" : "Truck",
"loc" : [
37.7901490737255,
-122.398658184604
],
"priorpermit" : "0",
"location" : {
"latitude" : "37.7901490874965",
"needs_recoding" : false,
"longitude" : "-122.398658184594"
},
"lot" : "055",
"cnn" : "101000",
"status" : "REQUESTED",
"schedule" : "http://bsm.sfdpw.org/PermitsTracker/reports/report.aspx?title=schedule&report=rptSchedule&params=permit=13MFF-0068&ExportPDF=1&Filename=13MFF-0068_schedule.pdf",
"locationdescription" : "01ST ST: STEVENSON ST to JESSIE ST (21 - 56)",
"latitude" : "37.7901490737255",
"blocklot" : "3708055",
"address" : "50 01ST ST",
"received" : "Mar 14 2013 3:34PM",
"applicant" : "Cupkates Bakery, LLC",
"longitude" : "-122.398658184604",
"expirationdate" : "2013-03-15T00:00:00",
"permit" : "13MFF-0068",
"y" : "2115738.283",
"x" : "6013063.33",
"block" : "3708"
}
When I try to index on 'loc', it doesn't get added:
> db.trucks.ensureIndex( { loc : "2d" } )
> db.trucks.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "food.trucks",
"name" : "_id_"
}
]
What am I doing wrong?
Shouldn't be db.trucks.ensureIndex( { loc : "2d" } )? You are creating indexes on some other collection.