0

I have this two Collections that I use, I need to query from one of the collection using params from the first collection

Collection expamples:

Collection 1 :{
id: 1234,
lastDate : "2020-09-10T08:30:14.960+00:00"
}

Collection 2 :{
site : 1234,
date : "2020-09-10T08:30:14.960+00:00"
}

I need to get the id and "lastDate" from Collection 1 and use it to query on Collection 2

sorry for bad explanation tell if need more info or anything

ADDED SQL equivalent

SELECT * FROM collection2 
WHERE site IN (SELECT id FROM collection1) and
 date IN (SELECT lastDate FROM collection1);

3 Answers 3

1
 let params  = {_id:1}
 let data = await Collection1.aggregate([
    {$match:params},
   {
      $lookup: {
        from: "collection2",
        localField: "$collection1datefield",
        foreignField: "$collection2datefield",
        as: "collection2Object"
      }
   }
  ])
Sign up to request clarification or add additional context in comments.

6 Comments

I have more then one in each collection... this is just an example for one document...
your means from collection1 you will fetch multiple document then you will match with collection 2?
I will fetch multple document from both collections i need the data from collection two, collection one is the query param conditions
Sorry i think you didn't understand me ( its my bad not yours ) ill try to be more clear, I need to get All the ids and lastDate and compare them to the site and date on collection 2 i have this sql query that i think suppose to do the same in sql ( i know sql better then mongo so it is more easy from to demonstrate it like this): Added in orignal post
try me code it will return all matched data from collection one. then i have done a lookup (Join) with 2nd collection with date. where date from first collection will match in 2nd collection that document you will from 2nd collection.
|
0

If you have done referencing into the first collection then you can use simple query i.e

const query = await Collection1.findOne({id: req.params.id}).populate('site lastDate');

1 Comment

I need the Date from collection two to be the same as lastDate... and id need to be the same ofcurse. populate will return any popaple lastDate
0
 db.FIRST_COLLECTION.aggregate([
  {
    $lookup: {
      from: "FIRST_COLLECTION",
      localField: "id", // local field of first collection
      foreignField: "site", // foreign field at second collection
      as: "liste",
    },
  },
  { $unwind: "$liste" },
  {
    $group: {
      _id: "$site",
      data: {
        $addToSet: {
          site: $site,
          date: $date,
        },
      },
    },
  },
]);

I invite you to read more about Aggregation

2 Comments

Already tried this... this will give the data from both collection i need only from collection2
i have updated the query to group data using site so you will have just sites

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.