嵌套数组上的mongo $ near命令

时间:2018-03-12 12:05:00

标签: java mongodb

{

"merchantId" : "M168976258",
"catalogTypeId" : "catalogTypeProduct",
"items" : [
    {
            "name" : "Product 1",
        "location" : {
            "type" : "Point",
            "coordinates" : [
                0,
                0
            ]
        }
    },
    {

            "name" : "Product 2",
        "location" : {
            "type" : "Point",
            "coordinates" : [
                0,
                0
            ]
        }
    },
    {
            "name" : "Product 3",
        "location" : {
            "type" : "Point",
            "coordinates" : [
                0,
                0
            ]
        }
    }


    }

我能够在具有单一位置的文件上的Perfrom mongo附近 使用以下命令

db.abc.find({ 
location :{
  $near : {
  $geometry : {   
     index : "Point" ,
     coordinates : [19.1, 72.89]
    },
   $maxDistance : 10000
   }
}
})

但是我无法对有嵌套数组的文件执行任何人可以帮助我找出问题

通过运行此查询,我收到以下错误

db.catalogForAdminAndMerchant.find({ 
"items.location" :{
  $near : {
  $geometry : {   
     index : "Point" ,
     coordinates : [19.1, 72.89]
    },
 $maxDistance : 10000
}
 }
})  

o / p是

planner返回错误:无法找到$ geoNear查询的索引

但已创建索引

通过运行db.catalogForAdminAndMerchant.getIndexes()得到

/* 1 */
{
"v" : 1,
"key" : {
    "_id" : 1
},
"name" : "_id_",
"ns" : "catalog-db.catalogForAdminAndMerchant"
 },

  /* 2 */
  {
"v" : 1,
"key" : {
    "_fts" : "text",
    "_ftsx" : 1
},
"name" : "CatalogForAdminAndMerchant_TextIndex",
"ns" : "catalog-db.catalogForAdminAndMerchant",
"weights" : {
    "items.name" : 3
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
},

  /* 3 */
{
"v" : 2,
"key" : {
    "address.items[i].location" : "2dsphere"
},
"name" : "address.items[i].location_2dsphere",
"ns" : "catalog-db.catalogForAdminAndMerchant",
"2dsphereIndexVersion" : 3

}

0 个答案:

没有答案