0
votes

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?

[]
2
I see you added "locations" to the model definition, 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 with mongoose.connect() is the database it's looking in. Using the mongo shell, the default namespace is "test". So check where your data is. - Neil Lunn
This is unlikely because of my other queries to this database work successfully. Сould you tell me whether my mongoose schema is right? I should rename my collection in MongoDB if it names "points"? - Artur Timoxin
I said "collection" and not just "database", even though both are a possibility. There are basically only four possible causes, and you've basically ruled out three of them. Bottom line is the collection cannot be called "locations" where you are looking, and actually has a different name. Show the steps in your question used to query from a mongo shell if you think otherwise. - Neil Lunn
@NeilLunn Thank you for helping me solve my problem, the collection is called a "points" and I created the "location" scheme, this caused a problem with the database request. Have a nice day! :) - Artur Timoxin

2 Answers

0
votes

Oh, collection name in my database "points", but I named my schema "locations". This was a problem for the database query.

const Points = mongoose.model("point", pointSchema, "points");
-1
votes

You need to specify an empty object in the find call:

Location.find({}).exec(...);