skip to Main Content

I am trying to fetch data from MongoDB by a field name – pubdate, but data is not showing neither I am getting any error!

I have field in the collection – _id, article, headline, pubdate all are String type except _id which is Objectid.

When I tried this query in Mongo query browser like – compass and studio 3t I got data –

{ pubdate: { '$gte': '2022-12-01', '$lte': '2022-12-31' } }

I am using postman to fetch data, in raw option sending POST request in JSON form.

{"fdate":"2022-12-31","tdate":"2022-12-31"}

const express = require("express");
const app = express();
const port = 3005;
const mongoose = require("mongoose");

// Connect to MongoDB using Mongoose
const url =
  "mongodb://localhost:2701/db";
mongoose.connect(url, { useUnifiedTopology: true, useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));

// Define the Article schema
const articleSchema = new mongoose.Schema({
  headline: String,
  fulltext: String,
  pubdate: String,
  article_type: String,
});
const Article = mongoose.model("collectioname", articleSchema);

// Route for retrieving articles based on fromdate and todate
app.post("/articles2", (req, res) => {
  let _fDate = req.body.fdate;
  let _tDate = req.body.tdate;

  Article.find({
    pubdate: { $gte: _fDate, $lte: _tDate },
    
  }).exec((err, articles) => {
    if (err) {
      console.error(err);
      res.status(500).send("Error retrieving articles");
      return;
    }
    res.send(articles);
  });
});

// Start the server
app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

How Will I pass the value in Postman to get the record, I am clueless as of now?

I tried map function too but no output

let _articles = articles.map((x) => x.articleid); res.send(_articles);

2

Answers


  1. Can you try logging what articles return in exec callback?
    One possible issue could be that variables _fDate and _tDate are not seen as Dates by mongoose.So try wrapping them with new Date?

    Article.find({
    pubdate: { $gte: new Date(_fDate), $lte: new Date(_tDate) },
    

    })

    Login or Signup to reply.
  2. Your issue is caused by Mongoose, by default, lowercasing and pluralizing the name that you pass when creating the model to determine the name of the collection it will use for the model.

    In other words, when you use mongoose.model('User', UserSchema), Mongoose will take the name (User) and lowercase and pluralize it, so the collection becomes users.

    If you want Mongoose to use an existing collection, or if you want to have full control of the collection name, you need to use the collection option in your schema:

    const articleSchema = new mongoose.Schema({
      headline: String,
      fulltext: String,
      pubdate: String,
      article_type: String,
    }, {
      collection : 'article_fulltext' // the collection to use for this schema
    });
    const Article = mongoose.model("Article", articleSchema);
    

    Without this option, and assume that you used article_fulltext as the model name, Mongoose would have used a collection named article_fulltexts.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search