我在MongoDB中创建了用于查找文档的猫鼬模式,但是它不起作用,总是返回一个空数组
在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"
}
]
}
创建了猫鼬架构,用于从集合中获取文档
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;
然后表达路由器来处理请求
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;
为什么我的路线返回了这个?
[]
答案 0 :(得分:0)
您需要在find
调用中指定一个空对象:
Location.find({}).exec(...);
答案 1 :(得分:0)
哦,我的数据库中的集合名称是“ points”,但是我将架构命名为“ locations”。这是数据库查询的问题。
const Points = mongoose.model("point", pointSchema, "points");