Compound GeoSpatial Index in MongoDB is not working as intended - mongodb

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.

Related

MongoDB - how to optimise find query with regex search, with sort

I need to execute the following query:
db.S12_RU.find({"venue.raw":a,"title":/b|c|d|e/}).sort({"year":-1}).skip(X).limit(Y);
where X and Y are numbers.
The number of documents in my collection is:
208915369
Currently, this sort of query takes about 6 minutes to execute.
I have the following indexes:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"venue.raw" : 1
},
"name" : "venue.raw_1"
},
{
"v" : 2,
"key" : {
"venue.raw" : 1,
"title" : 1,
"year" : -1
},
"name" : "venue.raw_1_title_1_year_-1"
}
]
A standard document looks like this:
{ "_id" : ObjectId("5fc25fc091e3146fb10484af"), "id" : "1967181478", "title" : "Quality of Life of Swedish Women with Fibromyalgia Syndrome, Rheumatoid Arthritis or Systemic Lupus Erythematosus", "authors" : [ { "name" : "Carol S. Burckhardt", "id" : "2052326732" }, { "name" : "Birgitha Archenholtz", "id" : "2800742121" }, { "name" : "Kaisa Mannerkorpi", "id" : "240289002" }, { "name" : "Anders Bjelle", "id" : "2419758571" } ], "venue" : { "raw" : "Journal of Musculoskeletal Pain", "id" : "49327845" }, "year" : 1993, "n_citation" : 31, "page_start" : "199", "page_end" : "207", "doc_type" : "Journal", "publisher" : "Taylor & Francis", "volume" : "1", "issue" : "", "doi" : "10.1300/J094v01n03_20" }
Is there any way to make this query execute in a few seconds?

I Want add if data in element array not exists

I have this data in database.
{
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b"),
"name" : "Jhones Crows",
"hobbies" : [
{
"name" : "swim",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b")
},
{
"name" : "run",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7c")
}
]
}
And I try to add data into hobbies if data in hobbies not exist. I try this :
db.getCollection('milo').update(
{'_id' : ObjectId("5a6ef287370ff5dc3d6fda7b"), 'hobbies.name' : 'sport'},
{ $addToSet : { 'hobbies' : {
'name' : 'sport',
}}
},
{upsert : true}
)
And I want data result like this :
{
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b"),
"name" : "Jhones Crows",
"hobbies" : [
{
"name" : "swim",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b")
},
{
"name" : "run",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7c")
},
{
"name" : "sport",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7a")
}
]
}
so suppose the value of sport is not in hoobies.name. will add a new name object in the hobbies. but if there is not change anything

MongoDB $lookup on array of object

I have 2 collections structured as below. I have tried $lookup to get the result but I am not getting any result because of my local and foreign fields are in array of object.
Below is my structure:
{
"_id" : ObjectId("5795a3531d3f3afc19caefef"),
"name" : "category1",
"updatedAt" : "1469431592786",
"resources" : [
{
"_id" : ObjectId("5791be003fa3bedc15d3adde"),
"title" : "resource1",
"availability" : false
},
{
"_id" : ObjectId("5795a3771d3f3afc19caeff0"),
"title" : "resource2",
"availability" : true
}
]
}
Above "categories" schema have resources array of object. this resource _id is stored in bookings collection in following way:
"booking":
{
"_id" : ObjectId("57960aa8000ca7a46b7ef683"),
"name" : "1469491200000",
"__v" : 0,
"schedule" : [
{
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep",
"_id" : ObjectId("57960aa8f9f9951c1fc923b1")
},
{
"resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "mr3",
"_id" : ObjectId("57960aa8f9f9951c1fc923b2")
},
{
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep23",
"_id" : ObjectId("57960aa8f9f9951c1fc923b3")
}
]
}
Now I want to get all the schedule of booking collection with their resource information.I want to fetch resources from categories table on the basis of booking schedule.
Desired output:
[
{
"name" : "1469491200000",
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"resourceTitle":"title",
"availability":false,
"bookings": [
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep",
"_id" : ObjectId("57960aa8f9f9951c1fc923b1")
},
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep23",
"_id" : ObjectId("57960aa8f9f9951c1fc923b3")
}
]
},
{
"name" : "1469491200000",
"resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
"resourceTitle":"mr3",
"availability":false,
"bookings": [
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "mr3",
"_id" : ObjectId("57960aa8f9f9951c1fc923b2")
}
]
}
]
Help me to get this desired result.
Thanks.

how filter inside array in mongodb

I want to filter only kumar "to" items"_id" : ObjectId("5048d2e5fbdac48208000042") message from my query and expected result
{
"_id" : ObjectId("5191502a2f1b3ca33e000016"),
"message" : "<p>sssdasd<br></p>",
"subject" : "test message to nag",
"date" : ISODate("2013-05-13T20:42:19.349Z")
}
from below collections
{
"__v" : 0,
"_id" : ObjectId("5191502a2f1b3ca33e000016"),
"conversation" : [
{
"date" : ISODate("2013-05-13T20:42:19.349Z"),
"message" : "<p>sssdasd<br></p>",
"_id" : ObjectId("5191502a2f1b3ca33e000017"),
"to" : {
"_id" : ObjectId("5048d2e5fbdac48208000042"),
"name" : "Kumar"
},
"from" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
}
},
{
"message" : "<p>reply</p>",
"date" : ISODate("2013-05-13T21:05:33.453Z"),
"_id" : ObjectId("5191559c7d2c386741000018"),
"to" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
},
"from" : {
"_id" : ObjectId("5048d2e5fbdac48208000042"),
"name" : "Kumar"
}
},
{
"message" : "<p>reply2<br></p>",
"date" : ISODate("2013-05-13T21:05:55.006Z"),
"_id" : ObjectId("519155b1ca98b66641000014"),
"to" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
},
"from" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
}
}
],
"from" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
},
"sent" : ISODate("2013-05-13T20:42:19.349Z"),
"subject" : "test message to nag",
"to" : {
"_id" : ObjectId("5048d2e5fbdac48208000042"),
"name" : "Kumar"
}
}
I was try with below query
db.messages.find({'conversation.to._id':ObjectId("5048d2e5fbdac48208000042")},{'subject':-1, 'conversation.message': 1,'conversation.to':1}).pretty();
But I am not get the expected result
I have used aggregate framework I am able to get the expected result.
db.message.aggregate(
{ $unwind : "$conversation" },
{ $match: {
"conversation.to.name" : "Anand"
}
},
{
$sort: {
"conversation.date" : -1
}
},
{
$group: {
_id:"$_id",
msgSubject: {$first:"$subject"},
msgDate: {$first:"$conversation.date"},
latestmsg: {$first:"$conversation.message"}
}
}
);
Please share if any different than above

DoctrineMongo near query

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