我想从deposit和coin_info集合中获取数据,两个集合中的硬币ID都相同。
这里我使用查找聚合方法。但是我的结果是空数组。
var mongoose=require('mongoose');
var data = mongoose.Schema({
user_id: { type: String },
coin_key: { type: String }
});
var coin_info = new mongoose.Schema({
_id: { type: String }
coin_code: { type: String }
});
var deposte_model = mongoose.model('deposit', data);
var get_coin_info = mongoose.model('coin_infos', coin_info);
var ccc=deposte_model.aggregate([
{ "$unwind": "$projects" },
{ "$unwind": "$projects.tags" },
{
$lookup:
{
from: "get_coin_info",
localField: "coin_key",
foreignField: "_id",
as: "inventory_docs"
}
},
{ "$unwind": "$inventory_docs" },
{
"$group": {
"_id": null,
"allTags": { "$addToSet": "$inventory_docs" },
"count": { "$sum": 1 }
}
}
]).exec(function(err, results){
console.log(results);
});
答案 0 :(得分:1)
尝试将coin_info的类型更改为ObjectId并引用deposite schema
var coin_info = new mongoose.Schema({
_id:{type:String}
coin_code:{
type:ObjectId,
ref:coin_infos/coin_info //Your collection name
}
});