简体   繁体   English

节点js猫鼬如何从两个集合中获取记录

[英]node js mongoose how to fetch record from two collection

In that below collections I want to try fetch all record from coin_infos collection matching _id of coin_infos with coin_id of wallet_infos and user_id . 在下面的那个收藏我想尝试获取来自所有记录coin_infos收集匹配_idcoin_infoscoin_idwallet_infosuser_id

coin_infos collections coin_infos收藏

_id:objectID(5a4b07b2a0050c20a6be44b3),
coin_code:BTC,  
wallet_name:bitcoin,
deposite_txn_fee:3,
min_withdrawn:5,
withdrawn_txn_fee:0.001,

wallet_infos collections wallet_infos集合

 _id:objectID(5a58a4e222068e053d71220d),
 user_id:5a4b0787a0050c20a6be44b2,
 coin_id:5a4b07b2a0050c20a6be44b3,
 balance:3.122858,

I have user ID -:5a4b0787a0050c20a6be44b2 我有用户ID-:5a4b0787a0050c20a6be44b2

output look like this:.. 输出看起来像这样:

{

_id:objectID(5a4b07b2a0050c20a6be44b3),
coin_code:BTC,
wallet_balance:3.122858,
wallet_name:bitcoin,
deposite_txn_fee:3,
min_withdrawn:5,
withdrawn_txn_fee:0.001,

"match_records" : [

_id:objectID(5a58a4e222068e053d71220d),
user_id:5a4b0787a0050c20a6be44b2,
coin_id:5a4b07b2a0050c20a6be44b3,
balance:3.122858,
 ] 
} 

My mongoDB version is 3.2.11. 我的mongoDB版本是3.2.11。

you can use $lookup and $match aggregate pipeline, as you already on mongo version 3.2+ 您可以使用$lookup$match聚合管道,因为您已经在mongo 3.2+版本中使用

$lookup joins coin_infos collection with wallet_infos on coin_id , if match found, the wallet info will be returned as a embedded document, from the embedded documents user_id you can do $match and filter $lookup通过wallet_infos上的coin_id加入coin_infos集合,如果找到匹配项,则钱包信息将作为嵌入式文档返回,从嵌入式文档user_id中,您可以进行$match和过滤

db.coin_infos.aggregate(
    [
        {
            $lookup : {
                from : "wallet_infos",
                localField : "_id",
                foreignField: "coin_id",
                as : "walletInfo"
            }
        },
        {$match : {"walletInfo.userId" : "5a4b0787a0050c20a6be44b2"}}
    ]
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在猫鼬和节点js的同一条记录中填充两个集合 - how to populate two collection on a same record in mongoose and node js 猫鼬和节点js从两个基于id的架构中获取记录 - Mongoose & node js to fetch record from two schema based on id's 如何从两个不同的集合中获取记录 - How to fetch record from two different collection 节点 JS,我如何使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z 从集合中获取数据 - Node JS, How do i get data from a collection with Mongoose 猫鼬,Node.js如何通过记录本身指定的值来增加集合中所有记录的特定值? - Mongoose, Node.js how to increment a specific value of all records in a collection by a value specified in the record itself? 通过将value字段和集合名称作为参数传递给node js,无法从mongodb获取记录 - not able to fetch the record from mongodb by passing the value field and collection name as parameters in node js 如何使用这些猫鼬模式(Node Js)从两个集合中加入? - How to join from two collections with these mongoose schema(Node Js)? mongoose快递节点JS链接二合集不使用_id - mongoose express node JS link two collection without using _id 无法使用node.js和mongoose从mongoDB中获取数据 - Unable to fetch data from mongoDB using node.js and mongoose 从node.js猫鼬中的集合输出文档 - Output document from collection in node.js mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM