mongoDB中mongoose的位置 [英] Location in mongoose, mongoDB
本文介绍了mongoDB中mongoose的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我尝试将位置存储在mongodb中时,它都不会显示,所以我想我做错了什么. 我找不到任何有关如何在猫鼬中存储位置的文档,所以我只想在这里询问.
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 ] },
这篇关于mongoDB中mongoose的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文