如何在聚合管道中将数组包含查询与geoNear运算符结合在一起?

时间:2018-11-26 19:12:39

标签: mongodb mongoose mongodb-query aggregation-framework

这是我的用例:

模式:

const pointSchema = new Schema({
    type: {
        type: String,
        enum: ["Point"]
    },
    coordinates: {
        type: [Number]
    }
});

var Item = newSchema({
    stores: [{ type: Schema.Types.ObjectId, ref: "Store" }],
})

var Store = new Schema({
    location: {
        type: pointSchema
    }
}

我要查找所有在地理空间点附近一定半径内包含至少一个商店的商品。

我正在使用以下代码进行操作:

Store.aggregate([
    {
      $geoNear: {
        near: {
          type: "Point",
          coordinates: [Number(lng), Number(lat)]
        },
        maxDistance: Number(radius),
        distanceField: "distance",
        spherical: true
      }
    }
  ]);

我能够获得商店,然后对商品进行查询:

Item.find({ stores: { $in: stores }})

但是无法按到近点的距离对商品进行排序,而且我也无法将距离字段从商店复制到商品的商店字段中。

0 个答案:

没有答案
相关问题