MongoDB query problem in express

I just started to learn express recently. In the practice of tapping mdn, I want to modify it a little bit. If I encounter a problem, I will first add the code:

//tagController.js:
const Tag = require("../models/tag");

exports.find_all = async (req, res, next) => {
        let result = await Tag.find({});
        debugger;
        res.send(result);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
};
///./models/tag

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;

var TagSchema = new Schema({
    context: String,
    tagId: ObjectId
    //tagId: {type: ObjectId, ref: "Tag", required: true},
});

TagSchema
    .virtual("url")
    .get(function () {
        return "/tag/" + this._id;
    });

module.exports = mongoose.model("Tag", TagSchema, "Tag");
//routes.js
const  express = require("express");
const  router = express.Router();
const tag_controller = require("../controllers/tagController");

router.get("/", tag_controller.find_all);
//Set up mongoose connection
var mongoose = require("mongoose");
var mongoDB = "mongodb://****************************/test?ssl=true&replicaSet=piggybank-shard-0&authSource=admin&retryWrites=true";
const options = {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
    autoIndex: false, // Don"t build indexes
    reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
    reconnectInterval: 500, // Reconnect every 500ms
    poolSize: 10, // Maintain up to 10 socket connections
    // If not connected, return errors immediately rather than waiting for reconnect
    bufferMaxEntries: 0,
    connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
    socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
    family: 4 // Use IPv4, skip trying IPv6

    // replicaSet: "piggybank-shard-0",
    // ssl: true,
    // authSource: admin,
    // retryWrites: true
};
mongoose.connect(mongoDB,options);
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));

Database connection should be normal. Data can be inserted into the table. If there is a problem with the query, I would like to ask where it may be wrong. Thank you:)

Apr.19,2022

I have tried your code, and the query is OK. You can take a look at my screenshot below:

my code is directly copy from your above code (no code will be posted), so I guess you can't query the data, either your so-called insert data may have failed (you can see if you inserted the data into the Tag), or you did not insert the data successfully.

Menu