Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

mongodb - lookup with condition in mongoose

I have two collections. articles and bookmarks.

articles

{
    _id: "5faa889ade5e0a6326a873d3",
    name: "article 1"
},
{
    _id: "5faa889ade5e0a6326a873d",
    name: "article 2"
}

bookmarks

{
    _id: "5faa889ade5e0a6326a873d1",
    user_id: "5fc7b50da483a66a86aa7e9e",
    model_id: "5faa889ade5e0a6326a873d3"
}

I want to join article with bookmark. if user bookmarked a article. what i have tried

const aggregate = await Articles.aggregate([{
        $lookup: {
            from: "categories",  
            localField: "category_id",
            foreignField: "_id",
            as: "category_id"
        }
    },
    {

        $lookup: {
            from: "bookmarks",  
            localField: "_id",
            foreignField: "model_id",
            as: "bookmarks"
        }
    }
]);

but it will gives all bookmark for the article not only logged in user bookmark. so how can I add a condition.

{ "user_id": objectId(req.user._id) } // logged user id

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

1 Answer

0 votes
by (71.8m points)

You can use $lookup with pipeline starting from MongoDB v3.6,

  • let to pass localField _id as model_id variable, you can use the field inside lookup pipeline using $$ reference,
  • pipeline to put $match stage and match your required conditions and user_id condition
  {
    $lookup: {
      from: "bookmarks",
      let: { model_id: "$_id" },
      pipeline: [
        {
          $match: {
            $expr: { $eq: ["$$model_id", "$model_id"] },
            user_id: objectId(req.user._id)
          }
        }
      ],
      as: "bookmarks"
    }
  }

Other option for MongoDB v3.4,

  • $filter to iterate loop of bookmarks and get filtered bookmarks on the base of condition
  {
    $lookup: {
      from: "bookmarks",
      localField: "_id",
      foreignField: "model_id",
      as: "bookmarks"
    }
  },
  {
    $addFields: {
      bookmarks: {
        $filter: {
          input: "$bookmarks",
          cond: { $eq: ["$$this.user_id", objectId(req.user._id)] }
        }
      }
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...