【发布时间】:2019-09-17 03:45:18
【问题描述】:
我在 MongoDB 中为查找文档创建了 mongoose 模式,但它不起作用,总是返回一个空数组
在 MongoDB 中,我有一个包含此类文档
的集合{
"_id":"5cba6a9079beee20a817b532",
"name":"Soborniy, 151",
"imageURL":"http://localhost:3001/locations/zaporozhya_soborniy_151.jpg",
"lat":47.845174,
"lng":35.127215,
"mainImageURL":"http://localhost:3001/points/Zaporozhya_Soborniy_151.jpg",
"pointName":"Jays Zaporizhia: Soborniy 151",
"pointDescription":"This is our European flagship store. For this store we chose an area called Kreuzber, a multicultural melting pot of artists, hipsters and all things Berlin. In this vast 150m² open space, we showed our respect to the greats of German design by creating a shop interior in the style of iconic Braun designer Dieter Rams. Our seating area, bean cellar, shop counter are all a homage to his amazing craft and ingenuity. The store also has a large kitchen, which is a new challenge for our brand. We hired two young chefs, Tyler from America, and Paulo from Italy, to cook locally grown, fresh, simple and healthy breakfast and lunch menu. As for coffee, %Arabica Berlin is filled with our usual passion for serving the best beans with the upmost dedication. Our founder, Kenneth owns a coffee farm in Hawaii, and we trade green beans from all over the world. We even offer beans from Ninety Plus Coffee, who are creating barista champions in so many countries the world over. This store is filled with our passion for coffee, food, design, and seeing the world. Please visit us and let’s ”See the World Through Coffee” together",
"neighborhoodPoints":[
{
"_id":"5cba6e034ca43420a82f7313",
"name":"Soborniy, 172",
"imageURL":"http://localhost:3001/locations/zaporozhya_soborniy_172.jpg"
},
{
"_id":"5cba6ec54ca43420a82f7314",
"name":"Soborniy, 175",
"imageURL":"http://localhost:3001/locations/zaporozhya_soborniy_175.jpg"
}
]
}
创建 mongoose Schema 用于从集合中获取文档
var mongoose = require("mongoose");
const locationSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
imageURL: String,
lat: Number,
lng: Number,
mainImageURL: String,
pointName: String,
pointDescription: String,
neighborhoodPoints: [
{
_id: mongoose.Schema.Types.ObjectId,
name: String,
imageURL: String,
},
],
});
const Location = mongoose.model("location", locationSchema, "locations");
module.exports = Location;
和express router处理请求
const express = require("express");
const router = express.Router();
const Location = require("../models/location_schema");
router.get("/", (req, res) => {
Location.find().exec((err, locations) => {
if (err) return res.status(500).json(err);
res.status(200).json(locations);
});
});
module.exports = router;
为什么我的路线返回这个?
[]
【问题讨论】:
-
我看到您将
"locations"添加到model定义中,这可能意味着您阅读了建议它的现有答案。然而,这实际上是 mongoose 使用的默认值,因为它使用了“模型名称”的"location",因为它是该名称的复数形式。如果您没有得到任何结果,那么这仍然不是现有的集合名称,或者您指向错误的数据库或服务器。请注意,与mongoose.connect()一起使用的连接URI 末尾的名称是它正在查找的数据库。使用mongoshell,默认命名空间是“test”。因此,请检查您的数据在哪里。 -
这不太可能,因为我对该数据库的其他查询工作成功。你能告诉我我的猫鼬模式是否正确吗?如果它命名为“点”,我应该在 MongoDB 中重命名我的集合?
-
我说的是“收集”而不仅仅是“数据库”,尽管 两者 都是可能的。基本上只有四种种可能的原因,你基本排除了三种。底线是集合不能在您正在查找的位置称为
"locations",并且实际上具有不同的名称。如果您不这么认为,请在您的问题中显示用于从mongoshell 查询的步骤。 -
@NeilLunn 谢谢你帮我解决了我的问题,集合被称为“点”,我创建了“位置”方案,这导致了数据库请求的问题。祝你今天过得愉快! :)
标签: javascript node.js mongodb object mongoose