Documents inserted without schema not being found with schema

Viewed 121

I have two new collections in MongoDB of data that I pulled from an old Firestore database that I'm moving to mongo. Since the total number between these two collections is roughly 20,000, I opted to paste the raw JSON into the insert document section in mongo, which worked like a charm and I didn't have to write a new insert route to do the same.

I then created a schema in Mongoose that matched the inserted documents, and tried to use the schema to pull back some data, and its always returning nothing.

An example of a ticket inserted via JSON:

{
    "title": "How to add and manage users for your company in QuickBooks Online",
    "priority": "1",
    "type": "Video",
    "course": "G205",
    "transcriptId": "07dom27Zz98jakvB1oh5",
    "status": "In Review",
    "tags": "",
    "url": "",
    "revisionNumber": 0,
    "directoryId": 19,
    "checkedOut": false
},

And my schema I made to match. The collection name in mongo is also called oldTickets, the plural of my schema name here:

const mongoose = require('mongoose');
var Schema = mongoose.Schema

const schema = new Schema({
    course:         { type: String },
    title:          { type: String },
    priority:       { type: String },
    type:           { type: String },
    course:         { type: String },
    transcriptId:   { type: String },
    status:         { type: String },
    tags:           { type: String },
    url:            { type: String },
    revisionNumber: { type: Number },
    directoryId:    { type: Number },
    checkedOut:     { type: Boolean },
    
});

module.exports = mongoose.model('oldTicket', schema);

And finally my model import and fetch call:

const OldTicket = require('./models/model_old_ticket');

/***************************************************************************
 * Get Old Tickets - Returns all old tickets, 10 at a time
****************************************************************************/ 
app.get('/getOldTickets/:offset', (req, res) => {

  checkConnection();

  OldTicket.find().skip(parseInt(req.params.offset)).limit(10).exec((err, data) => {

    if (err){ res.status(500).send({err: err}); }

    //If we got data, count the tickets & return the tickets & count
    if (data) {
      OldTicket.find().countDocuments().then(count => {
        return res.status(200).send({
            tickets: data,
            count: count
        })
    })

    }

  });

});

Why isn't this finding anything? Both the count and the tickets are 0. I've run into this issue before when manually creating a collection without a schema, and in those instances I would simply delete the collection, write a route to create a document, and then things would work fine. But with the large data size of these two collections, I'd rather not do that since everything should be working as is.

Edit: Example of document in MongoExample of document in Mongo

And the name of the collection I'm currently viewing:

Mongo collections

And I just now realized that for some reason there are now two collection names, oldTickets, which has data, and oldtickets, which is empty. I'm assuming my query is searching through the empty one? How can I get it to go to the one that actually has data?

1 Answers

can you attach the screenshot of your data with the collection? might be it's different.in mongoose, every collection name is complete with 's'. please verify your collection is created manually by you then it has to same as mongoose schema and also completed with 's'. example:

const mongoose = require("mongoose");

const schema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      index: true
    },
    filmId: {
      type: mongoose.Schema.Types.ObjectId,
      index: true
    },
    filmType: {
      type: String,
      index: true
    },
    birthday: {
      type: Date
    },
    age: {
      type: Number
    },
    terms: {
      type: Boolean
    }
  },
  {
    versionKey: false,
    timestamps: true,
  }
);

schema.index({ filmId: 1, user: 1 })

module.exports = mongoose.model("UserAgeVerification", schema);

see my database collection name should be complete with 's'

Related