mongodb php - how to do “INNER JOIN”-like query

后端 未结 5 623
挽巷
挽巷 2020-12-09 06:13

I\'m using the Mongo PHP extension.

My data looks like:

users
{
  \"_id\": \"4ca30369fd0e910ecc000006\",
  \"login\": \"user11\",
  \"pass\": \"examp         


        
5条回答
  •  臣服心动
    2020-12-09 06:54

    Forget about joins.

    do a find on your news. Apply the skip number and limit for paging the results.

    $newscollection->find().skip(20).limit(10);
    

    then loop through the collection and grab the user_id in this example you would be limited to 10 items. Now do a query on users for the found user_id items.

    // replace 1,2,3,4 with array of userids you found in the news collection.
    $usercollection.find( { _id : { $in : [1,2,3,4] } } ); 
    

    Then when you print out the news it can display user information from the user collection based on the user_id.

    You did 2 queries to the database. No messing around with joins and figuring out field names etc. SIMPLE!!!

提交回复
热议问题