I created mongoose schema for find document in MongoDB but it doesn't work, return always an empty array
In MongoDB, I have a collection with such documents
{
"_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"
}
]
}
Created mongoose Schema for get documents from collection
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;
And express router to process the request
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;
Why my route return this?
[]
"locations"to themodeldefinition, which probably means you read an existing answer that suggested it. However that would actually be the default that mongoose uses given the"location"for the "model name", since it pluralizes that name. If you get no results, then that is either still not the existing collection name, OR you're pointing to the wrong database or server. Note the name on the end of the connection URI used withmongoose.connect()is the database it's looking in. Using themongoshell, the default namespace is "test". So check where your data is. - Neil Lunn"locations"where you are looking, and actually has a different name. Show the steps in your question used to query from amongoshell if you think otherwise. - Neil Lunn