Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose未返回完整文档_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js Mongoose未返回完整文档

Node.js Mongoose未返回完整文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我使用mongolab作为我的数据库主机。 我创建了位置模式并确保索引“2dsphere” 文档示例: { "_id": { "$oid": "54d6347ce4b04aad9bbdc8ac" }, "name": "Vacation", "address": { "city": "Ashkelon", "street": "Afridat" }, "location": { "ty

我使用mongolab作为我的数据库主机。 我创建了位置模式并确保索引“2dsphere” 文档示例:

{
    "_id": {
        "$oid": "54d6347ce4b04aad9bbdc8ac"
    },
    "name": "Vacation",
    "address": {
        "city": "Ashkelon",
        "street": "Afridat"
    },
    "location": {
        "type": "Point",
        "coordinates": [
            34.552955,
            31.671384
        ]
    }
}
当使用Mongoose查询集合时,一切都很好,但我无法检索位置字段。 使用MongoShell时,我会得到完整的文档(带有位置)

此查询仅返回以下字段:名称、地址和_id

更新:

模式:

var locationSchema = mongoose.Schema({
    name: String,
    address: {city: String, street: String},
    location: {
        type: [Number],  // [<longitude>, <latitude>]
        index: '2dsphere'      // create the geospatial index
    }
});
var locationSchema=mongoose.Schema({
名称:String,
地址:{城市:字符串,街道:字符串},
地点:{
类型:[编号],//[,]
索引:“2dsphere”//创建地理空间索引
}
});

架构中的
位置
字段需要与文档中的结构匹配。您当前的模式将其定义为传统坐标对,但您的文档使用的是GeoJSON
,因此请将您的模式改为如下所示:

var locationSchema = mongoose.Schema({
    name: String,
    address: {city: String, street: String},
    location: {
        type: { type: String },
        coordinates: [Number]
    }
});    
locationSchema.index({location: '2dsphere'});

请注意,您需要单独定义
位置
的索引,因为它在架构中包含自己的字段。

您可以发布架构定义吗?我更新了我的问题谢谢先生。返回阅读有关遗留坐标和新GeoJSON的更多信息。
var locationSchema = mongoose.Schema({
    name: String,
    address: {city: String, street: String},
    location: {
        type: { type: String },
        coordinates: [Number]
    }
});    
locationSchema.index({location: '2dsphere'});