How does Query.prototype.intersects() work in Mongoose ?
Last Updated :
17 Mar, 2021
The Query.prototype.intersects() function is used to declare an intersects query for geometry(). This function is widely used with geometry function.
Syntax:
Query.prototype.intersects()
Parameters: This function has one arg parameter of the Object type.
Return Value: This function returns Query Object.
Installing mongoose:
npm install mongoose
After installing the mongoose module, you can check your mongoose version in the command prompt using the command.
npm mongoose --version
After that, you can just create a folder and add a file for example, index.js as shown below.
Database: The sample database used here is shown below.
Project Structure: The project structure will look like this:
Example 1:
javascript
const mongoose = require( 'mongoose' );
useNewUrlParser: true ,
useCreateIndex: true ,
useUnifiedTopology: true
});
const User = mongoose.model( 'User' , {
name: { type: String },
age: { type: Number }
});
const query = User.find();
query.where( 'path' ).intersects().geometry({
type: 'LineString'
, coordinates: [[180.0, 11.0], [180, 9.0]]
})
console.log(query._conditions)
|
Run index.js file using below command:
node index.js
Output:
{ path: { '$geoIntersects': { '$geometry': [Object] } } }
Example 2:
javascript
const express = require( 'express' );
const mongoose = require( 'mongoose' );
const app = express()
useNewUrlParser: true ,
useCreateIndex: true ,
useUnifiedTopology: true
});
const User = mongoose.model( 'User' , {
name: { type: String },
age: { type: Number }
});
const query = User.find();
query.where( 'path' ).intersects().geometry({
type: 'LineString'
, coordinates: [[181.0, 12.0], [181, 10.0]]
})
console.log(query._conditions)
app.listen(3000, function (error ) {
if (error) console.log(error)
console.log( "Server listening on PORT 3000" )
});
|
Run index.js file using below command:
node index.js
Output:
Server listening on PORT 3000
{ path: { '$geoIntersects': { '$geometry': [Object] } } }
Reference: https://mongoosejs.com/docs/api/query.html#query_Query-intersects
Share your thoughts in the comments
Please Login to comment...