简体   繁体   中英

How do i get data from user table in mongos node js by relation id

I have a one order table
Mongo db version is 3.0.15
Order table have this columns

{
        "status": "new",
        "_id": "5fd320a8b3d5133c8bf00c4a",
        "categoryId": "5fb2e20f5ea2aa2d15cca1ef",
        "specialization": "Tesst",
        "requestedBy": "5fd232ecd7ba652c3a85f818",
        "vendorId": "5fa908773410d8591aa9f550",
        "updated_at": 1607671976182,
        "created_at": 1607671976182,
    },

requested-by belongs to second table users
I want to fetch user details from user table username user-address

in user table i have this columns

{
 "_id": "5fd232ecd7ba652c3a85f818",
 "phone": "1234567890",
 "otp"  : "123456",
 "progress_pictures": [],
 "createdAt": "2020-12-10T14:38:36.045Z",
 "updatedAt": "2020-12-10T14:39:35.127Z",

}

How can I get data anyone know?

           Order.find(
                {
                    status  : status
                },
                {

                }
            ).exec();

This is my order invite query this is working find get only order table data

i found solution using populate

       Order.find(
            {
                status  : status
            },
            {

            }
        ).populate([
            {
                path: 'requestedBy', // in order table user ref id 
                select : 'name phone user_address'
            },{
                path : 'vendorId', // in order table vendor ref id 
                select : 'vendorName vendorMobile vendorAddress vendorBusinessName'
            }]).exec();

      ----------------------- RESPONSE -----------------------

          {
                "status": "new",
                "_id": "5fd320a8b3d5133c8bf00c4a",
                "categoryId": "5fb2e20f5ea2aa2d15cca1ef",
                "specialization": "Tesst",
                "requestedBy": {
                    "user_address": [],
                    "name" : "nikhil",
                    "_id": "5fd232ecd7ba652c3a85f818",
                    "phone": "1234567890"
                },
                "vendorId": {
                    "_id": "5fa908773410d8591aa9f550",
                    "vendorMobile": "1234567890",
                    "vendorAddress" : "demo address",
                    "vendorBusinessName" : "testing hub",
                    "vendorName": "KL"
                },
                "updated_at": 1607671976182,
                "created_at": 1607671976182,
            },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM