Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

geolocation - MongoDB: Not getting correct result using $geoWithin operator

I am using $geoWithin for circle, and i am not getting expected result. There are two collections,one for users and second for items. I am setting radius and coordinates for item. so i have to find a users within that coordinates and radius.

User collection

{
    "_id" : NumberLong(25287),
    "_class" : "com.samepinch.domain.user.User",
    "name":"XYZ",
    "location" : [
        74.866247,
        31.63336
    ]
}

Item collection

{
    "_id" : NumberLong(46603),
    "itemName" : "Chandigarh",
    "categoryName" : "TRAVELLING",
    "location" : {
        "_id" : null,
        "longitude" : 77.15319738236303,
        "latitude" : 28.434568229025803,
        "radius" : 29153.88048637637
    }
}

as according to item coordinates and user coordinates,there is approx 500 km distance between two places.

Query

db.users.find({ location: { $geoWithin: { $center: [ [77.15319738236303,28.434568229025803], 29153.88048637637/3963.2] } } } ).count()

According to $geoWithin,user should not show,but it is showing. If i am doing something wrong,please help me.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to query related to $geoWithin or $centerSphere in future in your project then specify your field structure like this only:-

"location" : {
        "lng" : 77.15319738236303,
        "lat" : 28.434568229025803
    },
"redius" : 120

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...