查找不工作的mongodb

时间:2018-03-20 06:22:17

标签: node.js mongodb

我的观看数据:

{
"msg": "Recently Viewed Experiences",
"data": [
    {
        "_id": "5aafce9ea33f68234466b790",
        "userId": "5aaf58dccf409c770353b5d1",
        "expId": "5aabc6d7bdd1940858284b09",
        "__v": 0,
        "created": "2018-03-19T14:52:14.154Z"
    },
    {
        "_id": "5ab09cec046e802b9c0599fc",
        "userId": "5aaf58dccf409c770353b5d1",
        "expId": "5aafdc7abea7fe08c778885d",
        "__v": 0,
        "created": "2018-03-20T05:32:28.436Z"
    }
  ]
}

我的发现功能:

 function getViewedExperiences(){
    return new Promise(function(resolve,reject){
        Views.find(
            [{userId: req.userId},{$lookup: {from: "experiences",localField: "_id",foreignField: "expId",as: "experience"}}
            ],function(err,result){
            if(err){
                reject(err);
            }else{
                resolve(result);
            }
        });
    });
 }

我的经历:

 {
        "_id": "5aabc6d7bdd1940858284b09",
        "name": "My First Experience",
        "subtitle": "Experience Subtitle ",
        "description": "Welcome to a soulful experience of vacationing in India, the cradle of ancient civilization with rich cultural heritage.",
        "whatwedo": "Experience the sights and sounds of its amazing diversity that is embedded in its geography, people and their cultures. Explore the gifts of nature and the timeless marks",
        "whocancome": "Any on can come\n",
        "whatweprovide": "Experience the sights and sounds of its amazing diversity that is embedded in its geography",
        "cancellationPolicy": "Cancellation Policy. We require 14 days cancellation notice prior to your scheduled arrival, otherwise we will charge you cancellation fee as below. 10% of your total amount will be charged if you do not notify us about your cancellation or any changes by 14 to 8 days before your scheduled arrival",
        "hostPhone": "400",
        "geoLocation": [
            28.628454,
            28.628454
        ],
        "price": 4000,
        "__v": 0,
        "dateCreated": "2018-03-20T05:32:42.876Z",
        "rating": 5,
        "currency": "Pound",
        "address": "G70, Sector 63 Noida",
        "status": "live",
        "images": [
            "[\"https://thumbnails.trvl-media.com/59v7eyda1z0_UsX4oB0MuP8b8Ao=/a.travel-assets.com/flex/flexmanager/images/2018/02/27/TVLY_PromoCode_TRIPTIME_lpad_532x299_20180131.jpg\"]"
        ]
    }

我需要在我的视图文档中查找来自其他体验文档的数据在我的体验文档中有一个 _id _id ,我希望从体验中获取数据。我使用 $ lookup ,但它不适合我

1 个答案:

答案 0 :(得分:0)

function getViewedExperiences(){
    return new Promise(function(resolve,reject){
        Views.find(
            {userId: req.userId}
        ).populate({
            path:'expId',
            model:Experiences
        }).exec(function(err,result){
            if(err){
                reject(err);
            }else{
                resolve(result);
            }
        });
    });
}