mongoose4.5中文教程
  • Introduction
  • Mongoose
  • guide
    • 模式(schemas)
      • 模式类型(types)
      • 自定义类型(custom)
    • 模型(models)
    • 文档(documents)
      • 子文档(sub docs)
      • 默认值(defaults)
    • 查询(queries)
    • 验证(validation)
    • 中间件(middleware)
    • 联表(population)
    • 连接(connections)
    • 插件(plugins)
    • 承诺(promises)
    • 鉴频器(discriminators)
    • 贡献
    • ES2015 整合
    • 浏览器中的schemas
    • 自定义schema类型
    • MongoDB版本兼容性
    • 3.6 发布说明
    • 3.8 发布说明
    • 4.0 发布说明
  • API 文档
Powered by GitBook
On this page
  • Queries
  • 其他文档的参考资料
  • Streaming
  • 下一步

Was this helpful?

  1. guide

查询(queries)

Previous默认值(defaults)Next验证(validation)

Last updated 6 years ago

Was this helpful?

原文: 翻译:小虾米(QQ:509129)

Queries

可以通过的几个静态辅助方法检索文档。

任何涉及指定查询条件的方法都可以执行两种方法:

当一个回调函数:

  • 通过,操作将立即执行,结果传递给回调。

  • 未通过,查询的一个实例被返回,它提供了一个特殊的查询生成器接口。

在mongoose 4,一个有一个 .then()功能,因此可以被用来作为一个承诺。

当执行一个查询回调函数,你指定一个JSON文档作为你的查询。JSON文档的语法是MongoDB的shell一样。

var Person = mongoose.model('Person', yourSchema);

// find each person with a last name matching 'Ghost', selecting the `name` and `occupation` fields
Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
  if (err) return handleError(err);
  console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.
})

在这里,我们看到,查询立即执行,并将结果传递给我们的回调。所有在Mongoose的回调使用模式:callback(error, result)。如果执行查询时发生错误,则错误参数将包含一个错误文档,结果将是无效的。如果查询成功,错误参数将为空,结果将与查询的结果填充。

现在让我们看看在没有回调时发生了什么:

// find each person with a last name matching 'Ghost'
var query = Person.findOne({ 'name.last': 'Ghost' });

// selecting the `name` and `occupation` fields
query.select('name occupation');

// execute the query at a later time
query.exec(function (err, person) {
  if (err) return handleError(err);
  console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.
})
// With a JSON doc
Person.
  find({
    occupation: /host/,
    'name.last': 'Ghost',
    age: { $gt: 17, $lt: 66 },
    likes: { $in: ['vaporizing', 'talking'] }
  }).
  limit(10).
  sort({ occupation: -1 }).
  select({ name: 1, occupation: 1 }).
  exec(callback);

// Using query builder
Person.
  find({ occupation: /host/ }).
  where('name.last').equals('Ghost').
  where('age').gt(17).lt(66).
  where('likes').in(['vaporizing', 'talking']).
  limit(10).
  sort('-occupation').
  select('name occupation').
  exec(callback);

其他文档的参考资料

Streaming

var cursor = Person.find({ occupation: /host/ }).cursor();
cursor.on('data', function(doc) {
  // Called once for every document
});
cursor.on('close', function() {
  // Called when done
});

下一步

在Mongoose中任何一个回调传递给一个查询,回调如下模式callback(error, results)。什么样的结果是取决于操作:为 它是一种单文档,一个文档列表,文档数量, 文件数量等影响,提供更加详细的关于什么是传递给回调函数。

在上面的代码中,查询变量是类型。查询允许您建立一个查询使用链式语法,而不是指定一个JSON对象。下面的2个例子是等价的。

一个完整的查询辅助功能列表可以在中找到。

There are no joins in MongoDB but sometimes we still want references to documents in other collections. This is where comes in. Read more about how to include documents from other collections in your query results .

你可以从MongoDB查询结果。你需要调用函数代替返回一个 实例。

既然我们已经掌握了查询,让我们看看。

Queries
模型
模型
查询
findone()
find()
count()
update()
API文档模型
查询
API文档
population
here
Query#cursor()
Query#exec
QueryCursor
验证