如何从 mongo 中的两个不同集合中获取匹配的记录
How to fetch matched record from two different collections in mongo
我想从存款和 coin_info 集合中获取数据,这两个集合中的硬币 ID 相同。
我在这里使用查找聚合 method.but 我得到的结果是空数组吗?
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);
});
尝试将 coin_info 的类型更改为 ObjectId 并引用存放模式
var coin_info = new mongoose.Schema({
_id:{type:String}
coin_code:{
type:ObjectId,
ref:coin_infos/coin_info //Your collection name
}
});
我想从存款和 coin_info 集合中获取数据,这两个集合中的硬币 ID 相同。
我在这里使用查找聚合 method.but 我得到的结果是空数组吗?
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);
});
尝试将 coin_info 的类型更改为 ObjectId 并引用存放模式
var coin_info = new mongoose.Schema({
_id:{type:String}
coin_code:{
type:ObjectId,
ref:coin_infos/coin_info //Your collection name
}
});