查询集合而不在猫鼬中传递模式 [英] 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?
为什么我不能做类似db.Kittens.find()
的事情?
Why can't i do something like db.Kittens.find()
?
推荐答案
在Mongoose主页上:
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.
猫鼬无法从一组可能唯一的文档中推断出一个架构. MongoDB不会对存储在集合中的文档强制执行架构.
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.
因此,Mongoose在NodeJS本机驱动程序(此处)上增加了一层,使很多人发现它们更有生产力.使用Node.JS时,并不需要与MongoDB一起使用.
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.
因此,正如您在粘贴的样本中看到的那样,定义了小猫Schema
,然后创建了Model
Kitten
.使用模式和模型的好处是,Mongoose会强制使用可用的属性/字段.
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.
您只能在应用程序中定义一次Schema
和Model
.因此,通常在应用程序启动时,您需要执行代码来定义它们,然后在整个应用程序生命周期中根据需要使用Model
实例.
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.
您还有更多可能想要使用Mongoose的原因.
There are many more reasons you'd want to use Mongoose potentially.
但是,您绝对是对的,您可以使用更直接的方法,而无需使用NodeJS本机驱动程序的模式.语法与您显示的语法相似,但是更加复杂:
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 ...
});
});
不是简单的:
Kitten.find(function(err, kittens) {
});
此外,在使用Mongoose时,您可能会发现编写更复杂的查询更容易编写和阅读:
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*/);
我建议您通读更多文档,以更好地了解该框架以及该框架是否适合您的需求.不幸的是,文档有些分散,但是如果您浏览Guide
标题的子标题,将会有很多有用的信息.
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.
这篇关于查询集合而不在猫鼬中传递模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!