question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mongoose query won't return any result while same query in MongoDB does

See original GitHub issue

I’m trying to build an application using MEAN but now I’m stuck when trying to find linestrings intersecting on another one given its name.

For instance, given the following image, poly1 and poly2 should have intersections while poly3 does not.

fc

Let’s suppose poly1 has the following coordinates and the following JSON:

{ 
  "_id" : ObjectId("57ab2107505ab11b1bd8422e"), 
  "name" : "poly1", 
  "updated_at" : ISODate("2016-08-10T12:41:43.789+0000"), 
  "created_at" : ISODate("2016-08-10T12:41:43.780+0000"), 
  "geo" : {
      "coordinates" : [ [14.59, 24.847], [28.477, 15.961] ], 
      "type" : "LineString"
  }, 
  "__v" : NumberInt(0)
}

When I run the query on MongoChef I find both poly1 and poly2 and I do not find poly3 like I want:

{
    geo :{
        $geoIntersects:{
            $geometry :{
                type: "LineString" ,
                coordinates: [ [14.59, 24.847], [28.477, 15.961] ]
            }
        }
    }
}

While, when I run the query on Mongoose given a Polyline Id/name it does not work

//Given
var linestringById = Linestrings.find({name : lineName});
var linestrings    = Linestrings.find({});    

//Works
query = linestrings.where({ geo : { $geoIntersects : 
                    { $geometry : 
                       { type : 'LineString', 
                         coordinates : [ [27.528, 25.006], [14.063, 15.591] ]
                        } 
                     } } });

//Does not work
query = linestrings.where({ geo : { $geoIntersects : 
                   { $geometry : 
                     { type : 'LineString', coordinates : linestringById }}}});

This is the Schema for LineStrings:

var mongoose = require('mongoose');
var Schema   = mongoose.Schema;

// Creates a LineString Schema.
var linestrings = new Schema({
    name: {type: String, required : true},
    geo : {
        type : {type: String, default: "LineString"},
                coordinates : Array
    },
    created_at: {type: Date, default: Date.now},
    updated_at: {type: Date, default: Date.now}
});

// Sets the created_at parameter equal to the current time
linestrings.pre('save', function(next){
    now = new Date();
    this.updated_at = now;
    if(!this.created_at) {
        this.created_at = now
    }
    next();
});

linestrings.index({geo : '2dsphere'});
module.exports = mongoose.model('linestrings', linestrings);

This is how I call the query from the front-end QueryController.js

/** Looks for LineStrings intersecting a given linestring **/
vm.polyIntersect = function () {

   //Taking name from a form
   vm.queryBody = {
                    name : vm.formData.poly1
   };

   // Post the queryBody 
   $http.post('/find-poly-intersection', vm.queryBody)
             .success(function(queryResults) {
                console.log(queryResults);
             })
            .error(function(queryResults) {
                console.log('Error: no results found '+queryResults));
            });
};

This is my Route.js:

/** Requiring Factories **/
var LinestringFactory = require('./factories/linestring.factory.js');

module.exports = function(app) {
  // Retrieves JSON records for all linestrings intersecting a given one
    app.post('/find-poly-intersection', function(req, res) {
        LinestringFactory.findIntersections(req).then( function (linestrings) {
            return res.json(linestrings);
        }, function (error) {
            return res.json(error);
        })
    });
}

This is my LineString.factory.js:

var Linestrings  = require('../models/linestring-model.js');

exports.findIntersections = findIntersections;

/** Finds Linestrings Intersections **/
function findIntersections(req) {
    return new Promise( function (resolve, reject) {
        var lineName       = req.body.name;
        var linestringById = Linestrings.find({name : lineName});
        var linestrings    = Linestrings.find({});

        //Check if that certain linestring exists with Lodash
        if (_.isEmpty(linestringById) || _.isUndefined(linestringById) 
            || _.isNull(linestringById)){
            return reject('No Linestrings found for that Name');
        } else {

        query = linestrings.where({ geo : { $geoIntersects : { $geometry : { type : 'LineString', coordinates : linestringById }}}});

        query.exec(function (err, intersections) {
            if (err){
                return reject(err);
            }
            return resolve(intersections);
        });

    }, function (error) {
        return reject(error);
    })
}

console.log in QueryController gives me always Object {} for any linestring name. I’m making sure of inserting [lng, lat] coordinates

Have you any idea on why I can’t find any LineString intersecting by Id while I can find them using straight coordinates?

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
andream16commented, Aug 17, 2016

Thanks for your response.

Mongoose version - “mongoose”: “~4.1.0”

And Debug after that particular query is:

That’s what happens when I pass straight coordinates to the query

Query {
  _mongooseOptions: {},
  mongooseCollection: 
   NativeCollection {
     collection: { s: [Object] },
     opts: { bufferCommands: true, capped: false },
     name: 'linestrings',
     collectionName: 'linestrings',
     conn: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'MeanMapApp',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: true,
        db: [Object] },
     queue: [],
     buffer: false },
  model: 
   { [Function: model]
     hooks: Kareem { _pres: {}, _posts: {} },
     base: 
      Mongoose {
        connections: [Object],
        plugins: [],
        models: [Object],
        modelSchemas: [Object],
        options: [Object] },
     modelName: 'linestrings',
     model: [Function: model],
     db: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'MeanMapApp',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: true,
        db: [Object] },
     discriminators: undefined,
     schema: 
      Schema {
        paths: [Object],
        subpaths: {},
        virtuals: [Object],
        nested: [Object],
        inherits: {},
        callQueue: [Object],
        _indexes: [Object],
        methods: {},
        statics: {},
        tree: [Object],
        _requiredpaths: undefined,
        discriminatorMapping: undefined,
        _indexedpaths: undefined,
        s: [Object],
        options: [Object] },
     collection: 
      NativeCollection {
        collection: [Object],
        opts: [Object],
        name: 'linestrings',
        collectionName: 'linestrings',
        conn: [Object],
        queue: [],
        buffer: false } },
  schema: 
   Schema {
     paths: 
      { name: [Object],
        'geo.type': [Object],
        'geo.coordinates': [Object],
        created_at: [Object],
        updated_at: [Object],
        _id: [Object],
        __v: [Object] },
     subpaths: {},
     virtuals: { id: [Object] },
     nested: { geo: true },
     inherits: {},
     callQueue: [ [Object], [Object], [Object] ],
     _indexes: [ [Object] ],
     methods: {},
     statics: {},
     tree: 
      { name: [Object],
        geo: [Object],
        created_at: [Object],
        updated_at: [Object],
        _id: [Object],
        id: [Object],
        __v: [Function: Number] },
     _requiredpaths: undefined,
     discriminatorMapping: undefined,
     _indexedpaths: undefined,
     s: { hooks: [Object], queryHooks: [Object] },
     options: 
      { id: true,
        noVirtualId: false,
        _id: true,
        noId: false,
        validateBeforeSave: true,
        read: null,
        shardKey: null,
        autoIndex: null,
        minimize: true,
        discriminatorKey: '__t',
        versionKey: '__v',
        capped: false,
        bufferCommands: true,
        strict: true,
        pluralization: true } },
  op: 'find',
  options: {},
  _conditions: { geo: { '$geoIntersects': [Object] } },
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection: 
   NodeCollection {
     collection: 
      NativeCollection {
        collection: [Object],
        opts: [Object],
        name: 'linestrings',
        collectionName: 'linestrings',
        conn: [Object],
        queue: [],
        buffer: false },
     collectionName: 'linestrings' },
  _traceFunction: undefined,
  _castError: null,
  _count: [Function],
  _execUpdate: [Function],
  _find: [Function],
  _findOne: [Function],
  _findOneAndRemove: [Function],
  _findOneAndUpdate: [Function] }
Mongoose: linestrings.find({ geo: { '$geoIntersects': { '$geometry': { type: 'LineString', coordinates: [ [ 27.528, 25.006 ], [ 14.063, 15.591 ] ] } } } }) { fields: undefined }  
POST /find-poly-intersection 200 77.255 ms - 2
[ { geo: { type: 'LineString', coordinates: [ [Object], [Object] ] },
    created_at: Wed Aug 10 2016 14:41:43 GMT+0200 (CEST),
    updated_at: Wed Aug 10 2016 14:41:43 GMT+0200 (CEST),
    __v: 0,
    name: 'poly2',
    _id: 57b428f3170ec81013cf3d26 },
  { geo: { type: 'LineString', coordinates: [ [Object], [Object] ] },
    created_at: Wed Aug 10 2016 14:41:43 GMT+0200 (CEST),
    updated_at: Wed Aug 10 2016 14:41:43 GMT+0200 (CEST),
    __v: 0,
    name: 'poly1',
    _id: 57ab2107505ab11b1bd8422e } ]

When I pass linestringByID I do not get any output in the console.

3reactions
vkarpov15commented, Aug 16, 2016

Enable mongoose debug mode with require('mongoose').set('debug', true); to see what query mongoose is actually sending. Also, which version of mongoose are you using?

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongoose find doesn't return any doc, while compass does
I tried to query using mongodb's compass - it worked as expected. I even tried my code to find documents using id(worked), title(worked)...
Read more >
Unable to query for recent data - Drivers & ODMs - MongoDB
When querying for data say for a IP the MongoDB return older data or no data at all (In case if the client...
Read more >
Mongoose v6.8.2: Queries
All callbacks in Mongoose use the pattern: callback(error, result) . If an error occurs executing the query, the error parameter will contain an...
Read more >
How find() Works in Mongoose | www.thecodebarbarian.com
find () in Mongoose. Make no mistake, Model.find() does what you expect: find all documents that match a query. But there's some confusion...
Read more >
How To Create Queries in MongoDB - DigitalOcean
The $in operator allows you to write queries that will return documents with values matching one of multiple values held in an array....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found