【发布时间】:2014-01-08 14:38:19
【问题描述】:
我在如何最好地在 mongoose 中实现此连接/查询时遇到了一些麻烦。
var mongoose = require('mongoose');
var addressSchema = new mongoose.Schema{
rank: Number,
address: String,
tag_name: String,
tag_url: String};
var tagSchema = new mongoose.Schema{
address: String,
name: String,
url: String};
我保存了一堆地址和一堆标签。有些地址有标签,大多数没有。我经常分别更新地址和标签。我想要做的是查询一些特定的地址并将它们作为一个数组返回,其中填充了标签字段(地址标签字段在数据库中是空白的)。
例如,我想在不为每个地址进行数据库查询的情况下执行此操作(示例中为 101 个数据库查询)。我不确定 $match 或 $in 或 populate 是否是我正在寻找的。以下代码未经测试,可能无法正常工作,但它应该让您了解我正在尝试做什么。
var db = require('../config/dbschema');
// find an array of addresses
db.addressModel.find({ rank: { $lte: 100 } }, function(err, addresses) {
// now fill in the tag fields and print
addTagField(0, addresses);
});
// recursive function to fill in tag fields
// address tag name and tag url fields are blank in the database
function addTagField(n, addresses) {
if(n < addresses.length) {
db.tagModel.find( { address: addresses[n].address }, function(err, tag) {
// if find a tag with this address, fill in the fields
// otherwise leave blank and check next address in array
if(tag) {
addresses[n].tag_name = tag.name;
addresses[n].tag_url = tag.url;
}
addTagField(n+1, addresses);
});
} else {
console.log(addresses);
}
}
http://mongoosejs.com/docs/api.html#aggregate_Aggregate-match http://mongoosejs.com/docs/api.html#query_Query-in http://mongoosejs.com/docs/api.html#document_Document-populate
我想用更少的数据库查询完成上述操作。
【问题讨论】:
-
我不遵循你想要做的事情。
$match仅用于聚合。$in是当您希望根据您为特定字段匹配的列表匹配多个文档时。 -
由于它们位于两个不同的集合中,您要么必须收集地址并查询标签,要么使用 mapReduce。 mongoosejs.com/docs/api.html#model_Model.mapReduce