mongoose 中的位置,mongoDB [英] Location in mongoose, mongoDB

查看:24
本文介绍了mongoose 中的位置,mongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试在我的 mongodb 中存储一个位置时,它都不会显示,所以我想我做错了什么.我找不到任何关于如何在 mongoose 中存储位置的文档,所以我只想在这里提问.

Whenever I try to store a location in my mongodb it doesn't show, so I guess I'm doing something wrong. I can't find any documentation on how to store a location in mongoose so I'm just gonna ask it here.

我首先创建我的模型:

var eventSchema = mongoose.Schema({ 
    name : String,
    creator : String,
    location : { type: [Number], index: '2dsphere'},

});

然后我尝试将其添加到我的数据库中:

Then I try to add it to my database:

var newevent = new event({ 
        name: name,
        creator: tokenUser, 
        location : { type: "Point", coordinates: [longitude, latitude] },
});

当我查看我的数据库时,除了位置之外,所有内容都已存储...

When I look in my database everything is stored except the location ...

推荐答案

我自己修好了.

我在我的模型中做了这个:

I did this in my model:

loc :  { type: {type:String}, coordinates: [Number]},

在下面我做了一个 2dsphere 索引.

Underneath I made it a 2dsphere index.

eventSchema.index({loc: '2dsphere'});

并向其添加数据:

loc: { type: "Point", coordinates: [ longitude, latitude ] },

这篇关于mongoose 中的位置,mongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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