Mongoose: StrictModeError when using $near with $geometry and geo points in array

Created on 14 Jul 2017  路  8Comments  路  Source: Automattic/mongoose

An error is thrown whenever I query for documents that contain geo points near a given point.

Reproduction:

const mongoose = require('mongoose');
const bluebird = require('bluebird');

mongoose.Promise = bluebird;

const uri = 'mongodb://localhost:27017/mongoose_test';
const db = mongoose.createConnection(uri, { db: { safe: true } });

const storySchema = new mongoose.Schema({
  gallery: [{
    src: String,
    location: {
      type: { type: String, enum: ['Point'] },
      coordinates: { type: [Number], default: void 0 } // eslint-disable-line
    },
    timestamp: Date
  }]
});

storySchema.index({ 'gallery.location': '2dsphere' });

const Story = mongoose.model('Story', storySchema);

const coordinates = [51.53377166666667, -0.1197471666666667];

Story.update({
  'gallery.location': {
    $near: {
      $geometry: {
         type: 'Point',
         coordinates
      },
      $maxDistance: 500
    }
  }
}, {
  $push: {
    gallery: {
      $each: [{
        src: 'test',
        location: {
          type: 'Point',
          coordinates,
        },
        timestamp: Date.now()
      }],
      $sort: { timestamp: 1 }
    }
  }
}, { upsert: true })
.then(resolve => console.log(resolve))
.catch(error => console.error(error));

Error is:

{
  message: 'Path "location" is not in schema, strict mode is `true`, and upsert is `true`.',
  name: 'StrictModeError',
  path: 'location'
}

node v8.1.4, mongodb v3.4.2, mongoose v4.11.2

confirmed-bug

All 8 comments

Also, setting strict: false in update options doesn't change the behaviour.

Thanks for the detailed repro, will fix asap :+1:

hi, i am still getting this error:

I even updated to the npm to latest mongoose but still seeing the StricModeError. even though i have defined it in the model.

any other threads i can go and study ?

thanks

@gissystem can you provide code samples please? This particular issue is fixed (we have tests in place to prove that much) but you might be running into a similar issue.

thanks for the message :)

so the flow is:

android application calls a function at the server side (MEAN Stack) which is then using mongoose to insert data in MLAB.

the function called is :

function findAndInsert(presence, callbackMethod) {
    var response = {};
    XXpresense.getXXpresenseModel().findOneAndUpdate({"stringID":presense.stringID, "date":presence.date }, presence, { new: true, upsert: true }, function (err, foundData) {
        // since stringID must be unique, we must check if FoundData length is 0. This means the stringID being input by the
        // admin is UNIQUE and we can insert this data in the table
        if(err) {
            console.error("error in inserting/updating presenceController.js ", err);
            response = {success:false, message:err};
            callbackMethod(response);
            return;
        }

        response={success:false, message:"Unsuccessful inserted/updated presenceController.js"};
        if(foundData != null && foundData != undefined) {
            response={success:true, message:"Successfully inserted/updated presenceController.js"};
        }
        callbackMethod(response);
    });
}

the error message i get from heroku log is:

error in inserting/updateing presenceController.js { StrictModeError: Path "stringID" is not in schema, strict mode is 'true', and upsert is 'true'.

at MongooseError.StrictModeError (/app/node_modules/mongoose/lib/error/strict.js:22:11)
at cast (/app/node_modules/mongoose/lib/cast.js:199:17)
at Query.cast (/app/node_modules/mongoose/lib/query.js:3204:12)
at Query._castConditions (/app/node_modules/mongoose/lib/query.js:1203:10)
at Query._findOneAndUpdate (/app/node_modules/mongoose/lib/query.js:2058:8)
at /app/node_modules/kareem/index.js:250:8
at /app/node_modules/kareem/index.js:23:7
at _combinedTickCallback (internal/process/next_tick.js:73:7)
 at process._tickCallback (internal/process/next_tick.js:104:9)
 message: 'Path "stringID" is not in schema, strict mode is `true`, and upsert is `true`.',
name: 'StrictModeError',
path: 'stringID' }

=========================

is there anything else you need from my end?

thank you so much for helping out :)

really appreciate it

BR,

What does your schema look like?

@vkarpov - you are genuis !
there was a spelling mistake in the schema..

it was stringId instead of stringID mentioned there :) this one small mistake costed me 7 days!

Lol glad you found it! It happens, once in high school I spent a month trying to figure out a bug that was caused by me using i, ii, and iii for iteration and typing ii when I meant iii 馃

Was this page helpful?
0 / 5 - 0 ratings