3
votes

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

3 Answers

7
votes

Use the node.js mongodb driver directly rather than mongoose.

http://mongodb.github.io/node-mongodb-native/2.2/

mongoose is an Object Rational Mapper for mongodb. If you don't want or need an ORM, don't use it. Use a mongo driver directly.

Personally I think mongoose produces very suboptimal queries, and that mongo queries are very easy to reason about making mongoose very redundant.

3
votes

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);
0
votes
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();
      })
});