gpt4 book ai didi

node.js - mongoose中的位置,mongoDB

转载 作者:IT老高 更新时间:2023-10-28 13:25:50 25 4
gpt4 key购买 nike

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

我首先创建我的模型:

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

});

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

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

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

最佳答案

我自己修好了。

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

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

在下面我将它设为 2dsphere 索引。

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

并向其中添加数据:

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

关于node.js - mongoose中的位置,mongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com