猫鼬和 Mongodb Returns 除了坐标
Mongoose and Mongodb Returns All but Coordinates
我正在尝试检索完整的文档,并且查询 returns 我需要的所有内容,但文档中的坐标除外。
这是我数据库中的示例文档:
示例文档
{
"_id": {
"$oid": "5733e359dcba0f6d5aa88cee"
},
"name": "Starcups",
"address": "906 Buford Hwy, Cumming, GA 30041",
"rating": 3,
"facilities": [
"Hot drinks",
"Food",
"Premium wifi"
],
"coords": {
"type": "Point",
"coordinates": [
-84.132809,
34.180035
]
},
"openingTimes": [
{
"days": "Monday - Friday",
"opening": "7:00am",
"closing": "7:00pm",
"closed": false
},
{
"days": "Saturday",
"opening": "8:00am",
"closing": "5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"reviews": [
{
"author": "Kenny Hall",
"rating": 5,
"timestamp": "13 April 2016",
"reviewText": "What a great place. I can't say enough good things about it."
},
{
"author": "Charlie Chaplin",
"rating": 3,
"timestamp": "16 June 2015",
"reviewText": "It was okay. Coffee wasn't great, but the wifi was fast."
}
]
}
如您所见,文档中有一个名为 coords
的字段。除此字段外,将返回整个文档。
这是我的控制器逻辑:
控制器逻辑
/* GET a location by the id */
module.exports.locationsReadOne = function(req, res) {
if (req.params && req.params.locationid) {
Loc.findById(req.params.locationid).exec(function(err, location) {
console.log(location);
if (!location) {
sendJsonResponse(res, 404, { "message" : "locationid not found" });
return;
} else if (err) {
sendJsonResponse(res, 404, err);
return;
}
sendJsonResponse(res, 200, location);
});
} else {
sendJsonResponse(res, 404, { "message" : "No locationid in request"});
}
};
知道为什么它不返回 coords
部分吗?如果重要的话,我会包括我的模式:
架构
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
author: String,
rating: {
type: Number,
required: true,
min: 0,
max: 5
},
reviewText: String,
createdOn: {
type: Date,
"default": Date.now
}
});
var openingTimeSchema = new mongoose.Schema({
days: {
type: String,
required: true
},
opening: String,
closing: String,
closed: {
type: Boolean,
required: true
}
});
var locationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
address: String,
rating: {
type: Number,
"default": 0,
min: 0,
max: 5
},
facilities: [String],
// Always store coordinates longitude, latitude order
coords: {
type: [Number],
index: '2dsphere'
},
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});
mongoose.model('Location', locationSchema);
编辑 1(添加额外的错误)
与 mongoose.set('debug', true);
我注意到以下几点:
Mongoose: locations.findOne({ _id: ObjectID("84y54uhfsomeid949") }) {fields: undefined}
我没有使用 findOne
函数,所以我不确定那是什么。我的理论是,我用来学习如何创建模式和将测试数据插入数据库的材料是旧的或过时的,而且我的文档被搞砸了。 (见上文示例文档)
编辑 2(将文档的坐标更改为此:)
"coords": [
-84.132809,
34.180035
],
我上面代码中的 "controller logic" 是针对 app_api
的。在我的 app_server
控制器中,我获取返回的数据并渲染出一个页面。在我将文档中的格式更改为上面编辑的格式后,这是 app_server/controllers/locations.js
的前后:
之前
/* GET 'Location info' page */
module.exports.locationInfo = function(req, res) {
var requestOptions, path;
path = "/api/locations/" + req.params.locationid;
requestOptions = {
url : apiOptions.server + path,
method : "GET",
json : {}
};
request(requestOptions, function(err, response, body) {
var data = body;
data.coords = {
lng : body.coordinates[0],
lat : body.coordinates[1]
};
renderDetailPage(req, res, data);
});
};
之后
/* GET 'Location info' page */
module.exports.locationInfo = function(req, res) {
var requestOptions, path;
path = "/api/locations/" + req.params.locationid;
requestOptions = {
url : apiOptions.server + path,
method : "GET",
json : {}
};
request(requestOptions, function(err, response, body) {
var data = body;
data.coords = {
lng : body.coords[0],
lat : body.coords[1]
};
renderDetailPage(req, res, data);
});
};
还有
之前(文档)
"coords": {
"type": "Point",
"coordinates": [
-84.132809,
34.180035
]
},
在(文件)
之后
"coords": [
-84.132809,
34.180035
],
我正在尝试检索完整的文档,并且查询 returns 我需要的所有内容,但文档中的坐标除外。
这是我数据库中的示例文档:
示例文档
{
"_id": {
"$oid": "5733e359dcba0f6d5aa88cee"
},
"name": "Starcups",
"address": "906 Buford Hwy, Cumming, GA 30041",
"rating": 3,
"facilities": [
"Hot drinks",
"Food",
"Premium wifi"
],
"coords": {
"type": "Point",
"coordinates": [
-84.132809,
34.180035
]
},
"openingTimes": [
{
"days": "Monday - Friday",
"opening": "7:00am",
"closing": "7:00pm",
"closed": false
},
{
"days": "Saturday",
"opening": "8:00am",
"closing": "5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"reviews": [
{
"author": "Kenny Hall",
"rating": 5,
"timestamp": "13 April 2016",
"reviewText": "What a great place. I can't say enough good things about it."
},
{
"author": "Charlie Chaplin",
"rating": 3,
"timestamp": "16 June 2015",
"reviewText": "It was okay. Coffee wasn't great, but the wifi was fast."
}
]
}
如您所见,文档中有一个名为 coords
的字段。除此字段外,将返回整个文档。
这是我的控制器逻辑:
控制器逻辑
/* GET a location by the id */
module.exports.locationsReadOne = function(req, res) {
if (req.params && req.params.locationid) {
Loc.findById(req.params.locationid).exec(function(err, location) {
console.log(location);
if (!location) {
sendJsonResponse(res, 404, { "message" : "locationid not found" });
return;
} else if (err) {
sendJsonResponse(res, 404, err);
return;
}
sendJsonResponse(res, 200, location);
});
} else {
sendJsonResponse(res, 404, { "message" : "No locationid in request"});
}
};
知道为什么它不返回 coords
部分吗?如果重要的话,我会包括我的模式:
架构
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
author: String,
rating: {
type: Number,
required: true,
min: 0,
max: 5
},
reviewText: String,
createdOn: {
type: Date,
"default": Date.now
}
});
var openingTimeSchema = new mongoose.Schema({
days: {
type: String,
required: true
},
opening: String,
closing: String,
closed: {
type: Boolean,
required: true
}
});
var locationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
address: String,
rating: {
type: Number,
"default": 0,
min: 0,
max: 5
},
facilities: [String],
// Always store coordinates longitude, latitude order
coords: {
type: [Number],
index: '2dsphere'
},
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});
mongoose.model('Location', locationSchema);
编辑 1(添加额外的错误)
与 mongoose.set('debug', true);
我注意到以下几点:
Mongoose: locations.findOne({ _id: ObjectID("84y54uhfsomeid949") }) {fields: undefined}
我没有使用 findOne
函数,所以我不确定那是什么。我的理论是,我用来学习如何创建模式和将测试数据插入数据库的材料是旧的或过时的,而且我的文档被搞砸了。 (见上文示例文档)
编辑 2(将文档的坐标更改为此:)
"coords": [
-84.132809,
34.180035
],
我上面代码中的 "controller logic" 是针对 app_api
的。在我的 app_server
控制器中,我获取返回的数据并渲染出一个页面。在我将文档中的格式更改为上面编辑的格式后,这是 app_server/controllers/locations.js
的前后:
之前
/* GET 'Location info' page */
module.exports.locationInfo = function(req, res) {
var requestOptions, path;
path = "/api/locations/" + req.params.locationid;
requestOptions = {
url : apiOptions.server + path,
method : "GET",
json : {}
};
request(requestOptions, function(err, response, body) {
var data = body;
data.coords = {
lng : body.coordinates[0],
lat : body.coordinates[1]
};
renderDetailPage(req, res, data);
});
};
之后
/* GET 'Location info' page */
module.exports.locationInfo = function(req, res) {
var requestOptions, path;
path = "/api/locations/" + req.params.locationid;
requestOptions = {
url : apiOptions.server + path,
method : "GET",
json : {}
};
request(requestOptions, function(err, response, body) {
var data = body;
data.coords = {
lng : body.coords[0],
lat : body.coords[1]
};
renderDetailPage(req, res, data);
});
};
还有
之前(文档)
"coords": {
"type": "Point",
"coordinates": [
-84.132809,
34.180035
]
},
在(文件)
之后"coords": [
-84.132809,
34.180035
],