位于mongoose,mongoDB

Lau*_*yts 6 database mongoose mongodb node.js

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

我首先创建我的模型:

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

});
Run Code Online (Sandbox Code Playgroud)

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

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

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

Lau*_*yts 9

我自己修好了.

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

loc :  { type: {type:String}, coordinates: [Number]},
Run Code Online (Sandbox Code Playgroud)

在我的下面,它使它成为一个2dsphere索引.

eventSchema.index({loc: '2dsphere'});
Run Code Online (Sandbox Code Playgroud)

并向其添加数据:

loc: { type: "Point", coordinates: [ longitude, latitude ] },
Run Code Online (Sandbox Code Playgroud)