querying a collection without passing schema in mongoose

mongodb, mongoose

Solution

From the Mongoose home page:

Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

Mongoose cannot infer from a collection of potentially unique documents a schema. MongoDB doesn't enforce schema upon the documents that are stored in a collection.

So, Mongoose adds a layer upon the NodeJS native driver (here) that many find more productive. It's not a requirement to use though with MongoDB when using Node.JS.

Mongoose needs two things fundamentally to work:

- Schema == this defines the document structure (reference). You can add validation, new methods, add virtual properties, use data types, establish references to other collections (models).

- Model == this is the class that is then used at run time to express queries against collections (reference). A Schema definition is used to build a Model.

So, as you saw in the sample you pasted, there is a kitten `Schema` defined, and then a `Model` `Kitten` is created. What's nice about using a schema and model is that Mongoose then enforces the properties/fields that are available.

You only define the `Schema`s and `Model`s once in an application. So, usually as the application starts, you'll need to execute code to define them, and then use the `Model` instances as needed throughout the application life-cycle.

There are many more reasons you'd want to use Mongoose potentially.

You're absolutely right though, you could just use something more direct, without a schema by using the NodeJS native driver. The syntax would be similar to what you showed, but a bit more complex:

MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
  if(err) { return console.dir(err); }

  var collection = db.collection('kittens');

    collection.find().toArray(function(err, kittens) {
        // here ...
    });    
});

Rather than the simple:

Kitten.find(function(err, kittens) {

});

Plus, when using Mongoose, you may find that writing more complex queries is easier to write and read:

Kitten.find().where('name', 'Harold').exec(/*callback*/);

I'd suggest reading through more of the documentation to get a better feel for the framework and whether it's a good match for your needs. The documentation is a bit scattered about unfortunately, but if you go through the sub headings of the `Guide` heading, you'll have a lot of good information available.

Problem

Do i understand correctly, that if i want to query a collection, i have to do the following: ``` var mongoose = require("mongoose"); mongoose.connect(); var db = mongoose.connection; db.on('open', function callback () { var kittySchema = mongoose.Schema({ name: String }) var Kitten = mongoose.model('Kitten', kittySchema) Kitten.find(function (err, kittens) { console.log(kittens); }) }); ``` Do i have to specify the schema each and every time, even when there is already a collection of kittens? Why can't i do something like `db.Kittens.find()`?

Original source