Getting info from two collections in MongoDB

Crazy traveller

I'm trying to use two collections(reviews and products) to find the title and description of products reviewed by reviewer “A”. I just need to display the title and description. Nothing else. So far I have:

db.reviews.aggregate([
    {$match: {reviewer : 'A'}},
    {$lookup: {
        from: "products",
        localField: "reviewer", 
        foreignField: "title", 
        foreignField: "description",
        as: "products_docs"}},
    {$project: {
        _id: 0,
        reviewerID: 1,
        title: 1, 
        description: 1
    }}    
    ])

There is supposed to be two products which the current output gives the ID so far but not the title or description.

/* 1 */
{
    "reviewerID" : "A"
}

/* 2 */
{
    "reviewerID" : "A"
}

Am I missing something?

sample docs :

review :

{
    "_id" : ObjectId("5d0b70f2d7367de7f5fa1589"),
    "reviewerID" : "A",
    "asin" : "1",
    "reviewerName" : "Bob",
    "helpful" : [ 
        0, 
        0
    ],
    "reviewText" : "It was really good.",
    "overall" : 1.0,
    "summary" : "Brilliant",
    "unixReviewTime" : 1402185600,
    "reviewTime" : "06 8, 2014"
}

product :

{
    "_id" : ObjectId("5d0b6d1cd7367de7f58b4906"),
    "asin" : "1",
    "description" : "Perfect for sunny days",
    "title" : "Sunglasses",
    "imUrl" : "/sunglasses.jpg",
    "related" : {
        "also_bought" : [ 
            "729300236X"
        ]
    },
    "salesRank" : {
        "Shoes" : 257607
    },
    "categories" : [ 
        [ 
            "Clothing, Shoes & Jewellery", 
            "Women", 
            "Accessories", 
            "Sunglasses & Eyewear Accessories", 
            "Sunglasses"
        ], 
        [ 
            "Clothing, Shoes & Jewellery", 
            "Men", 
            "Accessories", 
            "Sunglasses & Eyewear Accessories", 
            "Sunglasses"
        ]
    ]
}


whoami - fakeFaceTrueSoul

You can do that using $lookup, Try this below query :

db.review.aggregate([
    /** filtering out review coll to get required doc */
    {
        $match: {
            "reviewerID": "A"
        }
    },
    {
        $lookup: {
            from: "product",
            let: {
                asin: "$asin" // creating local variable from review Coll's field
            },
            pipeline: [
                {
                    $match: {
                        $expr: {
                            $eq: [
                                "$asin", // foreign field
                                "$$asin" // local variable created in let
                            ]
                        }
                    }
                },
                /** projecting only required fields from product Coll */
                {
                    $project: {
                        description: 1,
                        title: 1,
                        _id: 0
                    }
                }
            ],
            as: "data"
        }
    }
])

Test : MongoDB-Playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Query from two collections in MongoDB

MongoDB - How do join between the two collections by getting only one document from the second collection?

Getting unique values from two arraylists with Collections

Getting Information from Two Firebase Root Collections

How to delete documents from two collections in mongodb

Mongodb merge results from two collections

How to union from two different collections in Mongodb?

MongoDB merge two aggregations from different collections

Getting current collections in MongoDB from MongooseJS 5.x

Aggregation of two collections in MongoDB

Joining two collections in mongodb

Merge two collections in MongoDB

MongoDb two collections or one?

Mongodb:Right way to collect data from two collections?

how to count number of distinct values of a field from two collections in mongodb

How to find count from two collections relationship in MongoDB using Mongoose

How to calculate profit using aggregations from two collections in mongodb?

Query and Match from Two Database Collections (Mongoose/MongoDB)

Can we join two collections from different database in MongoDB

(Pymongo) connect/adding relationship with two column from different collections in MongoDB

How can I join two collections from different databases in mongoDB?

MongoDB - Embedding two collections into one

$aggregation, $sum on two collections in MongoDB

MongoDB: Populate or Query for two collections

How to relate two collections in MongoDB

Join two collections with MapReduce in MongoDB

Merge two $lookup collections in MongoDB

Comparing two unsorted collections in MongoDB

Mongodb query join two collections

TOP Ranking

HotTag

Archive