0

I have several documents in my Articles collection. Every document has a location value and some extra data. The location value looks like this:

"loc" : {
    "type" : "Point",
    "coordinates" : [ 
        4, 
        54
    ]
}

I can build an index by executing the following command:

db.articles.ensureIndex({loc:"2dsphere"});

And I can query documents based on their location and a $maxDistance with the following query:

db.articles.find({ loc : { $near : {$geometry : {type : "Point" , coordinates : [4, 54] }, $maxDistance : 1000 } } });

This works perfectly!

However when I change the location of my "loc" object in my document, my query always returns zero results. My document should look like this (This is a minimized version):

{
    "articledata" {
        "content": {
            "contact": {
                "loc" : {
                    "type" : "Point",
                    "coordinates" : [
                        4.1,
                        54
                    ]
                }
            }
        }
    }
}

When I rebuild my index query:

db.articles.ensureIndex({"articledata.content.contact.loc":"2dsphere"});

and execute my query again after changing my 'loc' location in the document:

db.articles.find({ "articledata.content.contact.loc" : { $near : {$geometry : {type : "Point" , coordinates : [4, 54] }, $maxDistance : 10000 } } });

There are no results.

It's probably some stupid mistake but I really can't find the problem...

Is there anyone who can help me out? Thanks in advance!

Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
  • 1
    Does it work if you add a colon after "articledata"? – skieter Nov 12 '14 at 17:36
  • 1
    Following your steps, everything works fine for me. Your example documents aren't well-formed and I had to add in {'s and a : to get them to work, so perhaps we didn't use exactly the same documents? Check carefully and try again; if things still don't work, edit the question to include the exact, well-formed documents that you are using. – wdberkeley Nov 12 '14 at 18:29
  • I don't know why but now it somehow works. Like @skieter suggested, I moved the location of "loc" to "articledata" and rebuilt my index. This worked. I then moved it to my ideal location and it kept on working (after an index of course). I don't understand it but I'm happy it's fixed. Thanks for the replies! – Thomas Bormans Nov 13 '14 at 09:00

0 Answers0