2016-06-09 68 views
2

我有2个系列:交易&帐户。

我需要将帐户加入交易,以便我可以在Accounts.acctType字段上进行分组。问题是Transactions.accountId的类型是'string',Accounts._id的类型是'Int32'。

有什么办法可以避免这个问题,而无需更改Transactions.accountId类型?

当前查找代码:

$lookup: { 
    from: 'accounts', 
    localField: accountId, 
    foreignField: '_id', 
    as: 'accountData' 
} 

我需要什么:

$lookup: { 
    from: 'accounts', 
    localField: Number(accountId), //something like this 
    foreignField: '_id', 
    as: 'accountData' 
} 

或者:

$lookup: { 
    from: 'accounts', 
    localField: accountId, 
    foreignField: '_id.toString()', //or something like this 
    as: 'accountData' 
} 
+0

如果没有看到您的数据的例子,并且了解这些不同领域应该如何匹配,很难回答。 –

回答

1

我最终做了一个数据库更改,以获取数据类型内联Id字段。

0

您可以$project您的文档,并使用$toLower转换 “交易”,“ _id“字段转换为字符串。

db.Transaction.aggregate(
    [ 
     { "$project": { "_id": { "$toLower": "$_id" } } }, // You need include all the fields you want in your result. 
     { "$lookup": { 
      "from": "accounts", 
      "localField": "accountId", 
      "foreignField": "_id", 
      "as": "accountData" 
     }} 
    ] 
) 
+0

是不是倒退? “_id”字段位于Accounts集合中。我将不得不投资交易集合铸造“accountId”作为一个数字不会吗? – KickinMhl

+0

集合中的“int32”类型的“_id”是否加入? – styvane

+0

是的,'_id'是我想要加入的集合中的int32。 – KickinMhl

相关问题