Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

mongodb - querying a collection without passing schema in mongoose

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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. 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).
  2. 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 Schemas and Models 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...