mongodb 查询集合而不在猫鼬中传递模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21429630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:35:09  来源:igfitidea点击:

querying a collection without passing schema in mongoose

mongodbmongoose

提问by user2422960

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

为什么我不能做这样的事情db.Kittens.find()

回答by WiredPrairie

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 提供了一种直接的、基于模式的解决方案来建模您的应用程序数据,并包括开箱即用的内置类型转换、验证、查询构建、业务逻辑挂钩等。

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 无法从潜在唯一文档的集合中推断出模式。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 在 NodeJS 本机驱动程序(这里)上添加了一个层,许多人发现它更有效率。使用 Node.JS 时,虽然与 MongoDB 一起使用并不是必需的。

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 referencesto 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.
  1. Schema == 这定义了文档结构(参考)。您可以添加验证、新方法、添加虚拟属性、使用数据类型、建立对其他集合(模型)的引用
  2. 模型 == 这是然后在运行时用于表达对集合(参考)的查询的类。Schema 定义用于构建模型。

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

因此,正如您在粘贴的示例中看到的那样,Schema定义了一个小猫,然后ModelKitten创建了一个。使用模式和模型的好处是 Mongoose 然后强制执行可用的属性/字段。

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 Modelinstances as needed throughout the application life-cycle.

您只在应用程序中定义Schemas 和Models 一次。因此,通常在应用程序启动时,您需要执行代码来定义它们,然后在Model整个应用程序生命周期中根据需要使用实例。

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

您可能希望使用 Mongoose 的原因还有很多。

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:

不过,您绝对正确,您可以通过使用 NodeJS 本机驱动程序来使用更直接的东西,而无需架构。语法与您显示的类似,但更复杂一些:

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:

另外,在使用 Mongoose 时,您可能会发现编写更复杂的查询更容易编写和阅读:

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 Guideheading, you'll have a lot of good information available.

我建议通读更多文档以更好地了解该框架以及它是否适合您的需求。不幸的是,文档有点分散,但是如果您浏览标题的子标题Guide,您将获得很多有用的信息。

回答by matthewtole

You cannot use Mongoose without schemas. If you want to use MongoDB without schemas, don't use Mongoose, use something like mongodbinstead.

没有模式就不能使用 Mongoose。如果你想在没有模式的情况下使用 MongoDB,不要使用 Mongoose,而是使用类似mongodb 的东西。

回答by Ritwik

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 };