Mongoose without models or schemas

Viewed 11132

how can I use mongoose without being forced to create models and schemas? I basically just have JS objects and know in which collection and document each of them has to go. I want to completely bypass the model and schema thing because they all have different structures.

3 Answers

Try this:

const Mongoose = require("mongoose"),
  Types = Mongoose.Schema.Types;

const modelName = "Employee";

//Employee Model without any fixed schema
const EmployeeSchema = new Mongoose.Schema({},
  {strict:false }
);

module.exports = Mongoose.model(modelName, EmployeeSchema);
dbo.collection("PackageCollection").aggregate([
        {"$lookup":
        {
            "from":"AgentCollection",
            "localField":"AgentId",   //field name of packageCollection
            "foreignField":"IdAsString",   //field name of AgentCollection I Stored object id as string
            "as":"Agent"
        }
    }
    ]).toArray(function(err, result) {
        res.send(result)
        db.close();
      })
});
Related