syv
syv

Reputation: 3608

Mongo Indexing - Nodejs

Im using the following schema and code to create collection in Mongo along with the Indexer and insert data. Please note that the collection is getting created dynamically based on the categoryName.

var employeeSchema = new mongoose.Schema({

      categoryId              : {type: String, required: true },

      loc                     : {type: {lon: Number,  lat: Number}, index: '2d'},

     // createdBy             : {type: String, required: true },
      createDate              : {type: Date, default: Date.now}
});

exports.register = function   (objEmployee , callback)
{
            var emp = db.model(objEmployee.categoryName, employeeSchema );

            var objSchema = new emp(objEmployee);          
            objSchema.save(function (err) {
            if (err) return callback(err);
            console.info ('Data inserted successfully.');
            return callback(null);
            });

};

Im able to insert data but when I run the query based on the radious, when I run I get the following error.

Sat Sep 29 20:21:24 uncaught exception: error: {
    "$err" : "can't find special index: 2d for: { loc: { $within: { $center: [ [ 50.9393925139, -114.0 ], 2.0 ] } } }",
    "code" : 13038

Anything going wrong with in my code ?

Upvotes: 0

Views: 324

Answers (1)

zemirco
zemirco

Reputation: 16395

I think your schema definition for loc is wrong. It should be

loc: {
  lon: Number,
  lat: Number
}

And after your schema definition add the index

employeeSchema.index({
  loc: "2d"
});

Upvotes: 3

Related Questions