如何使用二维地理索引在 Mongoose 模式中正确定义数组中的对象 [英] How to define object in array in Mongoose schema correctly with 2d geo index

查看:12
本文介绍了如何使用二维地理索引在 Mongoose 模式中正确定义数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在为下面的文档创建架构时遇到问题.来自服务器的响应总是将trk"字段值作为 [Object] 返回.不知何故,我不知道这应该如何工作,因为我至少尝试了所有对我有意义的方法;-)

I'm currently having problems in creating a schema for the document below. The response from the server always returns the "trk" field values as [Object]. Somehow I have no idea how this should work, as I tried at least all approaches which made sense to me ;-)

如果这有帮助,我的 Mongoose 版本是 3.6.20 和 MongoDB 2.4.7在我忘记之前,最好也将其设置为 Index (2d)

If this helps, my Mongoose version is 3.6.20 and MongoDB 2.4.7 And before I forget, it would be nice to also set it as Index (2d)

原始数据:

{
    "_id": ObjectId("51ec4ac3eb7f7c701b000000"),
    "gpx": {
        "metadata": {
            "desc": "Nürburgring VLN-Variante",
            "country": "de",
            "isActive": true
        },
    "trk": [
    {
        "lat": 50.3299594,
        "lng": 6.9393006
    },
    {
        "lat": 50.3295046,
        "lng": 6.9390688
    },
    {
        "lat": 50.3293714,
        "lng": 6.9389939
    },
    {
        "lat": 50.3293284,
        "lng": 6.9389634
    }]
    }
}

猫鼬架构:

var TrackSchema = Schema({
            _id: Schema.ObjectId,
            gpx: {
                metadata: {
                    desc: String,
                    country: String,
                    isActive: Boolean
                },
                trk: [{lat:Number, lng:Number}]
            }
        }, { collection: "tracks" });

Chrome 中网络选项卡的响应始终如下所示(仅 trk 部分是错误的):

The response from the Network tab in Chrome always looks like this (that's only the trk-part which is wrong) :

{ trk: 
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],

我已经为trk"尝试了不同的架构定义:

I already tried different Schema definitions for "trk":

  1. trk:Schema.Types.Mixed
  2. trk: [Schema.Types.Mixed]
  3. trk:[ { type:[Number], index: "2d" }]

希望你能帮助我;-)

推荐答案

你可以通过以下方式声明trk:-要么

You can declare trk by the following ways : - either

trk : [{
    lat : String,
    lng : String
     }]

trk : { type : Array , "default" : [] }

在插入过程中的第二种情况下,创建对象并将其推入数组,如

In the second case during insertion make the object and push it into the array like

db.update({'Searching criteria goes here'},
{
 $push : {
    trk :  {
             "lat": 50.3293714,
             "lng": 6.9389939
           } //inserted data is the object to be inserted 
  }
});

或者你可以通过

db.update ({'seraching criteria goes here ' },
{
 $set : {
          trk : [ {
                     "lat": 50.3293714,
                     "lng": 6.9389939
                  },
                  {
                     "lat": 50.3293284,
                     "lng": 6.9389634
                  }
               ]//'inserted Array containing the list of object'
      }
});

这篇关于如何使用二维地理索引在 Mongoose 模式中正确定义数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆