Phillipp
Phillipp

Reputation: 1445

Mongoose without models or schemas

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.

Upvotes: 3

Views: 12950

Answers (3)

Avinash
Avinash

Reputation: 2205

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);

Upvotes: 6

Jay Kukadiya
Jay Kukadiya

Reputation: 686

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

Upvotes: -1

arboreal84
arboreal84

Reputation: 2154

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.

Upvotes: 10

Related Questions