How to define object in array in Mongoose schema correctly with 2d geo index

前端 未结 4 760
故里飘歌
故里飘歌 2020-12-04 06:35

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

4条回答
  •  执念已碎
    2020-12-04 06:54

    You can declare trk by the following ways : - either

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

    or

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

    or you can set the Array of object 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'
          }
    });
    

提交回复
热议问题