0

I have the following MongoDB document strucutre:

{
"_id" : ObjectId("570b43bae4b0a33a2d7"),
"id" : "1175",
"data" : [ 
    {
        "name" : "tim",
        "email" : "tim@gmail.com",
        "id" : "1173",
        "x1" : 12.951

    }, 
    {
        "name" : "alex",
        "email" : "alex@gmail.com",
        "id" : "1096",
        "x1" : 12.926

    }, ]}

I want to filter out "data" array so that values of "x1" greater than 12.93 are selected. I want the following result after querying:

{
"_id" : ObjectId("570b43bae4b0a33a2d7"),
"id" : "1175",
"data" : [ 
    {
        "name" : "tim",
        "email" : "tim@gmail.com",
        "id" : "1173",
        "latitude" : 12.951


    }]} 

I ran the following query in mongo shell as instructed in MongoDB official manual

db.getCollection('mydatabase').aggregate(
         [{  $project:  {  data : { $filter: {   input: "$data", as : "item", cond:{   $gte: [ "$$item.x1" ,12.93]  } } } } }] )

I am getting the following error:

assert: command failed: { "errmsg" : "exception: invalid operator '$filter'", "code" : 15999, "ok" : 0 }

Why am I getting this error? How to resolve this error?

styvane
  • 59,869
  • 19
  • 150
  • 156
Rakeh Sahu
  • 47
  • 3
  • 11
  • 1
    The reason you are getting this error is because you are using old MongoDB server version. [`$filter`](https://docs.mongodb.org/manual/reference/operator/aggregation/filter/) is new in 3.2 also this is a duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/q/3985214/3100115) – styvane Apr 17 '16 at 06:44
  • @user3100115- Should I use it or not? Any alternate way to query my requirement ?? – Rakeh Sahu Apr 17 '16 at 06:47
  • Did you see the link above in my comment? – styvane Apr 17 '16 at 06:52
  • @user3100115 Yes, I saw the link.I guess i am not doing anything different from the suggested solution still I am getting this error. By the way, i am using robomongo 0.9.0 RC-7 – Rakeh Sahu Apr 17 '16 at 07:04

0 Answers0