如何用2d geo索引正确定义Mongoose模式中数组中的对象

时间:2022-05-25 18:43:25

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 ;-)

我目前在为下面的文档创建模式时遇到了问题。服务器的响应总是返回“trk”字段值作为[Object]。不知怎的,我不知道这是怎么回事,因为我至少尝试了所有对我有意义的方法;

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)

如果这有帮助,我的Mongoose版本是3.6.20和MongoDB 2.4.7,在我忘记之前,还可以将它设置为索引(2d)

Original data:

原始数据:

{
    "_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
    }]
    }
}

Mongoose Schema:

猫鼬模式:

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

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

Chrome网络选项卡的响应始终是这样的(这仅仅是错误的trk部分):

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

I already tried different Schema definitions for "trk":

我已经尝试过“trk”的不同模式定义:

  1. trk: Schema.Types.Mixed
  2. 载重汽车:Schema.Types.Mixed
  3. trk: [Schema.Types.Mixed]
  4. 载重汽车(Schema.Types.Mixed):
  5. trk:[ { type:[Number], index: "2d" }]
  6. trk:[{type:[Number], index: "2d"}]

Hope you can help me ;-)

希望你能帮助我;

3 个解决方案

#1


150  

You can declare trk by the following ways : - either

你可以用以下方法申报trk: -任何一种

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

or

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

trk:{类型:数组,“默认”:[]}

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 
  }
});

or you can set the Array of object by

也可以设置对象数组by

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'
      }
});

#2


51  

I had a similar issue with mongoose :

我和猫鼬有过类似的问题:

fields: 
    [ '[object Object]',
     '[object Object]',
     '[object Object]',
     '[object Object]' ] }

In fact, I was using "type" as a property name in my schema :

事实上,我在模式中使用“type”作为属性名:

fields: [
    {
      name: String,
      type: {
        type: String
      },
      registrationEnabled: Boolean,
      checkinEnabled: Boolean
    }
  ]

To avoid that behavior, you have to change the parameter to :

为了避免这种行为,您必须将参数更改为:

fields: [
    {
      name: String,
      type: {
        type: { type: String }
      },
      registrationEnabled: Boolean,
      checkinEnabled: Boolean
    }
  ]

#3


1  

Thanks for the replies.

谢谢你的回复。

I tried the first approach, but nothing changed. Then, I tried to log the results. I just drilled down level by level, until I finally got to where the data was being displayed.

我尝试了第一种方法,但没有改变。然后,我尝试记录结果。我一层一层地往下钻,直到我终于找到显示数据的地方。

After a while I found the problem: When I was sending the response, I was converting it to a string via .toString().

过了一会儿,我发现了问题:当我发送响应时,我通过. tostring()将它转换为字符串。

I fixed that and now it works brilliantly. Sorry for the false alarm.

我修好了,现在效果很好。请原谅我的误报。

#1


150  

You can declare trk by the following ways : - either

你可以用以下方法申报trk: -任何一种

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

or

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

trk:{类型:数组,“默认”:[]}

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 
  }
});

or you can set the Array of object by

也可以设置对象数组by

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'
      }
});

#2


51  

I had a similar issue with mongoose :

我和猫鼬有过类似的问题:

fields: 
    [ '[object Object]',
     '[object Object]',
     '[object Object]',
     '[object Object]' ] }

In fact, I was using "type" as a property name in my schema :

事实上,我在模式中使用“type”作为属性名:

fields: [
    {
      name: String,
      type: {
        type: String
      },
      registrationEnabled: Boolean,
      checkinEnabled: Boolean
    }
  ]

To avoid that behavior, you have to change the parameter to :

为了避免这种行为,您必须将参数更改为:

fields: [
    {
      name: String,
      type: {
        type: { type: String }
      },
      registrationEnabled: Boolean,
      checkinEnabled: Boolean
    }
  ]

#3


1  

Thanks for the replies.

谢谢你的回复。

I tried the first approach, but nothing changed. Then, I tried to log the results. I just drilled down level by level, until I finally got to where the data was being displayed.

我尝试了第一种方法,但没有改变。然后,我尝试记录结果。我一层一层地往下钻,直到我终于找到显示数据的地方。

After a while I found the problem: When I was sending the response, I was converting it to a string via .toString().

过了一会儿,我发现了问题:当我发送响应时,我通过. tostring()将它转换为字符串。

I fixed that and now it works brilliantly. Sorry for the false alarm.

我修好了,现在效果很好。请原谅我的误报。