1

Supposedly I want to get a an object from my collection, an to this object, I want to add a property that will store an array of object containing ids and title matching one of the property of my object (in my case series).

Example I have this object as a result from my initial query

{
   _id: 13123123123,
   title: "TitleofObject",
   series: "SeriesName",
}

then I want to look on the same collection where the series name is the same for my object (add a new property named sameSeries to store objects matching the series) and the final result of object should look something like this

    _id: 13123123123,
   title: "TitleofObject",
   series: "SeriesName",
   sameSeries: 
    [
      {
      _id: 12312312,
      title: "anothertitleofObject"
      }, 
      {
      _id: 12312342312,
      title: "anothertitleofObject2"
      }
    ]
   

How can I achieve this using the aggregate method?


 const book = await Book.aggregate([
       {
            $match: { _id: id }
       },
])

2
  • Where do you want look like that? In the collection or after aggregation? And where do you have the array elements which should be part of sameSeries array. Commented May 30, 2022 at 3:17
  • Hello @Gibbs I want to make it a part of aggregation result only and send them as a json object, and the array of objects are also objects from the same collection that match the series of the initial object, So its like im storing those object inside the initial object that I found with id, Commented May 30, 2022 at 3:27

1 Answer 1

1
db.collection.aggregate([
  {
    "$group": { //Group by series
      "_id": "$series",
      "sameSeries": { //Create an object
        $push: { //push the required fields
          "title": "$title",
          "_id": "$_id"
        }
      }
    }
  }
])

playground

db.collection.aggregate([
  {
    "$match": {
      "_id": 13123123123,
      "sameSeries": {
        "$exists": false
      }
    }
  },
  {
    "$lookup": {
      "from": "collection",
      "localField": "series",
      "foreignField": "series",
      "as": "sameSeries"
    }
  }
])

Playground

To skip the parent id, you can do a slice

db.collection.aggregate([
  {
    "$match": {
      "_id": 13123123123,
      
    }
  },
  {
    "$lookup": {
      "from": "collection",
      "localField": "series",
      "foreignField": "series",
      "as": "sameSeries"
    }
  },
  {
    "$project": {
      _id: 1,
      series: 1,
      title: 1,
      sameSeries: {
        "$slice": [
          "$sameSeries",
          -1
        ]
      }
    }
  }
])

Play

Sign up to request clarification or add additional context in comments.

8 Comments

Hello, is there any way like to append only the sameseries properties instead of creating a new object, i want to append it only on the initial object that im looking for with id, then append the same series properties if its series is not empty and look for other objects that have the same series and add it to the sameseries
Check the edit and let me know
Hello i want to lookup only if series is not empty, I dont want it to be on match method since I still need to get that object regardless if the series is empty or not
Explain with a sample? What do you mean by non empty series?
The aggregate function should not add the sameseries array if the series is empty ,something like that sir.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.