Exploring the Bounds of Mongodb's $within Query

I'm currently working on a geospatial query in mongodb using the $within operator. I have a collection entry with a location field containing:

location: {
  bounds: {
    south_west: { lat: XX.XXXXXX, lng: XX.XXXXX },
    north_east: { lat: XX.XXXXXX, lng: XX.XXXXX }
  },
  center: {
    lat: XX.XXXXXX,
    lng: XX.XXXXXX
  }
}

In my query, I need to retrieve all places that have a location.center within the bounds of a google.maps object. This is what I have so far:

query = {}

var southWest = map.getBounds().getSouthWest();
southWest = [southWest.lat(), southWest.lng()];

var northEast = map.getBounds().getNorthEast();
northEast = [northEast.lat(), northEast.lng()];

query["location.center"] = { $within: { $box: [southWest, northEast] } }

However, I keep encountering this error message:

Exception from Meteor.flush: Error: Unrecognized key in selector: $within
at Error (<anonymous>)
at Function.LocalCollection._exprForConstraint (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:559:11)
at Function.LocalCollection._exprForOperatorTest (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:468:36)
at Function.LocalCollection._exprForKeypathPredicate (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:387:34)
at Function.LocalCollection._exprForSelector (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:307:36)
at Function.LocalCollection._compileSelector (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:284:24)
at new LocalCollection.Cursor (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:71:39)
at LocalCollection.find (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:57:10)
at _.extend.find (http://localhost:3000/packages/mongo-livedata/collection.js?3ef9efcb8726ddf54f58384b2d8f226aaec8fd53:155:34)
at Template.feed.feed_items (http://localhost:3000/client/modules/js/feed.js?5ed30ddbd861944b636dbe03c13cc80421258a5b:129:17) 

I'm not sure how to proceed from here. When I use JSON.stringify(query), I noticed that the query appears like this:

{"location.center":{"$within":{"$box":[[39.93623526127148,-75.63687337060549],[39.984129247435064,-75.57438862939455]]}}}

Is there a way to prevent the quotes around the $within and $box selectors? It seems that's where the exception is coming from.

Answer №1

The issue lies with the quotation marks in JSON, but rest assured, it is a valid format. The actual problem is that $within functionality has not been implemented yet in minimongo. Back in October 2012, Matt Debergalis highlighted this concern in his comment on the incomplete state of Minimongo:

Recently, there has been some discussion on meteor-talk about the various improvements and expansions that should be made to minimongo.

[...]

  • Inclusion of $near, $within, and $maxDistance capabilities.

If you try running this on the server where you have genuine access to MongoDB, you might have better luck. However, I'm unsure how that would work if the client store lacks the same functionalities.

You can find the source code for Minimongo here. Perhaps you could take a shot at implementing this selector yourself and submit a pull request? :)

Answer №2

While this query might appear dated, it could still be valuable for those who stumble across it again. Take note of the following code snippet as it demonstrates how to utilize minimongo for querying:

locations.query(
    { "location.lng": {
        "$greaterThan": bottomLeftLng,
        "$lessThan": topRightLng  
      },
      "location.lat": {
        "$greaterThan": bottomLeftLat,
        "$lessThan": topRightLat
      }
   }
)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Image not showing up when using drawImage() from canvas rendering context 2D

Need help with drawImage() method in JavaScript <head> </head> <body> <script type = "text/javascript"> var body, canvas, img, cxt; body = document.getElementsByTagName("body" ...

Setting headers in Node.js after they have already been sent to the client is not allowed

I'm currently enrolled in a node.js course on Udemy which seems to be outdated. I've encountered some errors that I'm struggling to resolve. Here's what I've tried so far: using next(); adding return res inside all if statements ...

What are the differences between using the open prop and conditionally rendering a Material-UI Modal component?

Is there a difference in using the open prop or conditionally rendering Material-UI's Modal component and its built components? The closing transition may be lost, but are there any performance advantages when dealing with multiple Modals? Example wi ...

Enhancing features with jQuery's .animate() function

Can anyone help me figure out why the div on the right is not pushing itself away from the one on the left when I hover over it? I want it to move on mouseenter and return to its original position on mouseleave. The changing background colors are just ther ...

To add additional nested data to a JSON object in JavaScript, you can use the push method or update

Looking to enhance the nested object data within my existing object The current structure of the JSON object array resembles this: var orderDetails = [{ "utilityType": "Electric", "firstName": "ROBERT", "lastName": "GUERRERO", "utilityList": [{ ...

Function being called by Intersection Observer at an inappropriate moment

After running the page, the intersection observer behaves exactly as desired. However, upon reloading the page, I am automatically taken back to the top of the page (which is expected). Strangely though, when the viewport interacts with the target elemen ...

Proper syntax for SVG props in JSX

I have developed a small React component that primarily consists of an SVG being returned. My goal is to pass a fill color to the React component and have the SVG use this color. When calling the SVG component, I do so like this: <Icon fillColour="#f ...

Strategies for tracking distinct property values in Firestore

Is it possible to count the occurrences of unique values in Firestore? For instance, if there are 1000 documents with dates and only 50 different dates repeated, how can I get a list of each date along with its frequency? Context: These documents represe ...

Tips for formatting a lengthy SQL query in a Node.js application

Currently, I am facing a challenge with a massive MySQL select query in my node.js application. This query spans over 100 lines and utilizes backticks ` for its fields, making me uncertain if ES6's multi-line string feature can be used. Are there any ...

Can we determine if a user's operating system has disabled animations?

In my frontend project with React, I am incorporating an animation within a component. However, I want to cater to users who have disabled animations in their settings by replacing the animated content with a static image. Is there a method to detect if ...

Harmonizing various client viewpoints in a ThreeJS scene featuring a unified mesh structure

I am fairly new to ThreeJS and I am curious to know if it is possible to achieve the following, and if so, how can it be done: Two web browser clients on separate machines want to load the same html-based Scene code, but view it from different perspective ...

Utilize the inverse mapping method along with conditional statements inside a mapping function

When looping through the categories using categories.map(), I am trying to reverse the elements of the categories and also check for category.isFeatured before creating a link. However, I am unable to use an if statement in this scenario. const Header = ...

Problems with navigation, not functioning properly due to issues with Pulled functions

I am still getting the hang of things and struggling with the terminology, so please bear with me as I try to explain my issue. Currently, I am working on a project in react native where I have two files - Header.js and footer.js. I have successfully impo ...

The issue of AngularJS failing to bind object properties to the template or HTML element

Just dipping my toes into angularJS, following Todd Motto's tutorials, and I'm having trouble displaying object properties on the page. function AddCurrentJobs($scope){ $scope.jobinfo = [{ title: 'Building Shed', description: ...

Commencing CSS Animation Post Full Page Loading

I am looking for a solution using WordPress. In my specific case, I want the CSS Animations to take effect only after the page has completely loaded. For example: click here This is my HTML: <div class="svg-one"> <svg xmlns="ht ...

Installing a package from a private repository using a different package name with npm

I'm looking to incorporate a module from a private GitHub repository into my project. To achieve this, I will execute the command npm install git+https://[API-KEY]:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b737c6e607 ...

Add owl carousel to your npm project in any way you see fit

After struggling for a while, I finally wanted to implement owl-carousel, but couldn't figure out how to connect it using npm and webpack. The official NPM website states: Add jQuery via the "webpack.ProvidePlugin" to your webpack configuration: ...

Tips on using Ajax to post in HTML

Here is the code I have been working on: <script type="text/javascript"> var xmlDoc; var xmlhttp; function loadRates() { xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = readRates; xmlhttp.open("GE ...

What is the process for implementing a click event and accessing the DOM within an iframe using react-frame-component?

I am working on using the react-frame-component to create an iframe. I am trying to bind a click event on the iframe and retrieve the DOM element with the id of "abc" inside the iframe. Can anyone guide me on how to achieve this? The code snippet provided ...

Using es6 map to deconstruct an array inside an object and returning it

I am looking to optimize my code by returning a deconstructed array that only contains individual elements instead of nested arrays. const data = [ { title: 'amsterdam', components: [ { id: 1, name: 'yanick&a ...