Help with near(can't find special index: 2d for: { geoLoc: { $near: [ 40.75412, -72.98157000000001 ] } })

340 views
Skip to first unread message

FabioEbner

unread,
May 4, 2012, 10:37:50 PM5/4/12
to mongod...@googlegroups.com
Can, someone help me?? i'm trying to use .near  but i always get this error:

can't find special index: 2d for: { geoLoc: { $near: [ 40.75412, -72.98157000000001 ] } }

i start the mongo with MongoDir\bin\mongod --dbpath dbDir 

i use this method to add the citys:

public void addVarias() throws Exception{
City c1 = new City("teste1'", new double[] {40.724220,-73.961470});
City c2 = new City("teste2'", new double[] {40.734220,-73.971470});
City c3 = new City("teste3'", new double[] {40.754220,-73.981470});
City c4 = new City("teste4'", new double[] {40.754220,-72.981470});
City c5 = new City("teste5'", new double[] {41.754220,-72.981470});
ds = ConexaoDao.getInstance().getDatastore();
ds.save(c1);
ds.save(c2);
ds.save(c3);
ds.save(c4);
ds.save(c5);
ds.ensureIndexes();
}
And this to get

ds = ConexaoDao.getInstance().getDatastore();
List<City> cityList = ds.find(City.class).field("geoLoc").near(40.754120,-72.981570, 100/111.12).asList();


and this is my City Object:
@Entity("city")
public class City {

@Id
private ObjectId cdCity;
private String nmCity;
private double[] geoLoc;
public City() {
// TODO Auto-generated constructor stub
}
public City(String nmCity, double[] geoLoc) {
this.nmCity = nmCity;
this.geoLoc = geoLoc;
}

//gets and setters
}

Nat

unread,
May 4, 2012, 11:50:19 PM5/4/12
to mongod...@googlegroups.com
try declaring @Indexed annotation as follows:

@Entity("city")
public class City {

@Id
private ObjectId cdCity;
private String nmCity;
@Indexed(IndexDirection.GEO2D)

private double[] geoLoc;
public City() {
// TODO Auto-generated constructor stub
}
public City(String nmCity, double[] geoLoc) {
this.nmCity = nmCity;
this.geoLoc = geoLoc;
}

//gets and setters
}

Fabio Ebner

unread,
May 4, 2012, 11:51:43 PM5/4/12
to mongod...@googlegroups.com
Nice.. this work..

tks

2012/5/5 Nat <nat....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/PP1ioNaTDO4J.

To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Fabio Ebner

unread,
May 5, 2012, 8:22:18 AM5/5/12
to mongod...@googlegroups.com
everytime i save one object i need to call ensureindex? i can put on auto? :D





2012/5/5 Nat <nat....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.

Nat Luengnaruemitchai

unread,
May 6, 2012, 10:58:37 AM5/6/12
to mongod...@googlegroups.com
Nope. Just once is enough. You can do it during the program initialization.

Fabio Ebner

unread,
May 6, 2012, 3:02:58 PM5/6/12
to mongod...@googlegroups.com

Só, i only need run just after connect? But if i save some object, he will make the index auto?

Enviado do celular

Sam Millman

unread,
May 6, 2012, 3:53:03 PM5/6/12
to mongod...@googlegroups.com
Yes

Fabio Ebner

unread,
May 10, 2012, 4:04:40 PM5/10/12
to mongod...@googlegroups.com
Hey man, takes a doub my please, i create my Object with ObjectId, i have some Id that refer this data?? just like in SGDB, to not travel all the object only one pointer

tks

2012/5/6 Sam Millman <sam.m...@gmail.com>
Reply all
Reply to author
Forward
0 new messages