Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

javascript - Querying fields in Mongo DB joined collection with aggregate

I've recently been using the aggregate method in Mongo DB to join two tables based on a common field, however, have hit a brick wall with trying to query specific information in the joined results. It seems that I need to use the pipeline option, but traditionally, I'd use $query to pull out field.

My JS is:

const results = await client.db().collection('data_source_one').aggregate([{
  $lookup: {
    from: 'data_source_two',
    // localField: 'created_at',
    // foreignField: 'created_at',
    pipeline: [{
      $match: {
        $expr: {
          $and: [
            { $exists: ['$data.sessions'] }
          ]
        }
      }
    }],
    as: 'combined_results'
  }
}]).toArray();

Now, data_source_two returns an object called data which has a field in there called sessions, and I want to do several checks, the first is to make sure that it exists and also make sure that it equals a certain value.

But it seems like I also have to drop the check for localField and foreignField?

Where am I going wrong? Can I just pass $query as another object into the aggregate method?

question from:https://stackoverflow.com/questions/65916015/querying-fields-in-mongo-db-joined-collection-with-aggregate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...