本文介绍了如何使用2d地理索引在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而且在我忘记之前,最好将其设置为索引(2d)

If this helps, my Mongoose version is 3.6.20 and MongoDB 2.4.7And 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:[{类型:[数字],索引:"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'
      }
});

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

10-12 11:19