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()
?
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
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.
Just do a db. createCollection('name') - it will create an empty collection without an id.
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.
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:
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.
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 };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With