mongodbscalasalatnosql

MongoDB query does not find documents in collection


My query does not find documents in my MongoDB collection

This is example object in the database (notice that position.x and position.y are Longs in Salat)

{
  "_id": ObjectId("50e85039e4b0f225b98b8b34"),
  "worldSlug": "test",
  "position": {
    "x": {
      "floatApprox": 3
    },
    "y": {
      "floatApprox": 3
    }
  },
  "type": "village",
  "owner": "mati",
  "health": {
    "got": 500,
    "total": 500
  }
}

This is my query

{
  "worldSlug": "test",
  "position": {
    "x": {
     "$gt": -31,
     "$lt": 29
    },
    "y": {
      "$gt": -27,
      "$lt": 33
    }
  }
}

Solution

  • You need to use dot notation to query on embedded fields. Use a query object like this instead:

    {
      "worldSlug": "test",
      "position.x": {
         "$gt": -31,
         "$lt": 29
      },
      "position.y": {
          "$gt": -27,
          "$lt": 33
      }
    }