Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 478
user2422960 Avatar asked Jan 29 '14 11:01

user2422960


People also ask

Does Mongoose require schema?

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

Can we create MongoDB collection without defining schema?

Its not possible anymore. You can use Mongoose with the collections that have schema and the node driver or another mongo module for those schemaless ones.

How do I create a collection in MongoDB without any document using Mongoose in node JS?

Just do a db. createCollection('name') - it will create an empty collection without an id.

What is $in in Mongoose?

The value of the $in operator is an array that contains few values. The document will be matched where the value of the breed field matches any one of the values inside the array.


2 Answers

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.

like image 77
WiredPrairie Avatar answered Sep 28 '22 11:09

WiredPrairie


try this..

const mongoose = require('mongoose'); const Schema = mongoose.Schema;  const ProductSchema = new Schema({}, { strict: false }); const Product = mongoose.model('Product', ProductSchema, 'products');  module.exports = { Product }; 
like image 35
Ritwik Avatar answered Sep 28 '22 10:09

Ritwik