Home > other >  How to group random documents?
How to group random documents?

Time:12-11

This is my collection, Just simple post data per document.

[
{
  "title": "This is a title-1",
  "description": "This is a description",
  "imageURL": "https://img.freepik1.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-1-72876078"
},
{
  "title": "This is a title-2",
  "description": "This is a description",
  "imageURL": "https://img.freepik2.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-2-72876078"
},
{
  "title": "This is a title-3",
  "description": "This is a description",
  "imageURL": "https://img.freepik3.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-3-72876078"
},
{
  "title": "This is a title-4",
  "description": "This is a description",
  "imageURL": "https://img.freepik4.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-4-72876078"
},
{
  "title": "This is a title-5",
  "description": "This is a description",
  "imageURL": "https://img.freepik5.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-5-72876078"
},
{
  "title": "This is a title-6",
  "description": "This is a description",
  "imageURL": "https://img.freepik6.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-6-72876078"
},
{
  "title": "This is a title-7",
  "description": "This is a description",
  "imageURL": "https://img.freepik7.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-7-72876078"
},
{
  "title": "This is a title-8",
  "description": "This is a description",
  "imageURL": "https://img.freepik8.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-8-72876078"
},
{
  "title": "This is a title-9",
  "description": "This is a description",
  "imageURL": "https://img.freepik9.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-9-72876078"
},
{
  "title": "This is a title-10",
  "description": "This is a description",
  "imageURL": "https://img.freepik10.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-10-72876078"
},
{
  "title": "This is a title-11",
  "description": "This is a description",
  "imageURL": "https://img.freepik11.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-11-72876078"
},
{
  "title": "This is a title-12",
  "description": "This is a description",
  "imageURL": "https://img.freepik12.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
  "slug": "this-is-a-title-12-72876078"
},
]

I want to show random posts but instead of fetching every random post document from MongoDB I want to add them in every document.

How to add random post data of other documents to every document like this:

  {
    "title": "This is a title-1",
    "description": "This is a description",
    "imageURL": "https://img.freepik1.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
    "slug": "this-is-a-title-1-72876078",
    "randomPost1": {
      "title": "This is a title-6",
      "description": "This is a description",
      "imageURL": "https://img.freepik6.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
      "slug": "this-is-a-title-6-72876078"
    },
    "randomPost2": {
      "title": "This is a title-9",
      "description": "This is a description",
      "imageURL": "https://img.freepik9.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
      "slug": "this-is-a-title-9-72876078"
    },
    "randomPost3": {
      "title": "This is a title-5",
      "description": "This is a description",
      "imageURL": "https://img.freepik5.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
      "slug": "this-is-a-title-5-72876078"
    },
    "randomPost4": {
      "title": "This is a title-12",
      "description": "This is a description",
      "imageURL": "https://img.freepik12.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
      "slug": "this-is-a-title-12-72876078"
    },
    "randomPost5": {
      "title": "This is a title-8",
      "description": "This is a description",
      "imageURL": "https://img.freepik8.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
      "slug": "this-is-a-title-8-72876078"
    },
    "randomPost6": {
      "title": "This is a title-2",
      "description": "This is a description",
      "imageURL": "https://img.freepik2.com/free-vector/night-ocean-landscape-full-moon-stars-shine_107791-7397.jpg?w=2000",
      "slug": "this-is-a-title-2-72876078"
    }
  }

How can I do this in MongoDB aggregation?

CodePudding user response:

Try this

db.records.aggregate([
{
    $group: {
        _id: "$description",
        recId:{'$first': '$_id'},
        title: { '$first': '$title' },
        imageURL: { '$first': '$imageURL' },
        description: { '$first': '$slug' },
        slug: { '$first': '$slug' }
    }
},
{
    $lookup:
    {
        from: 'records',
        let: { description: "$_id", recId:'$recId' },
        pipeline: [
            {
                $match:
                {
                    $expr:{
                        $and:
                        [
                             { $eq: ["$description", "$$description"] },
                             { $ne: ["$_id", "$$recId"] }
                             ]
                        }
                }
            },
            {
                $sample: { size: 6 }
            },
            {
                $project:{
                    _id:0,
                    description:0
                }
            }
        ],
        as: "related"
    }
}

])

  • Related