mongodb php-如何执行类似"INNER JOIN"的查询 [英] mongodb php - how to do "INNER JOIN"-like query

查看:233
本文介绍了mongodb php-如何执行类似"INNER JOIN"的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mongo PHP扩展.

I'm using the Mongo PHP extension.

我的数据如下:

users
{
  "_id": "4ca30369fd0e910ecc000006",
  "login": "user11",
  "pass": "example_pass",
  "date": "2010-09-29"
},
{
  "_id": "4ca30373fd0e910ecc000007",
  "login": "user22",
  "pass": "example_pass",
  "date": "2010-09-29"
}

news
{
  "_id": "4ca305c2fd0e910ecc000003",
  "name": "news 333",
  "content": "news content 3333",
  "user_id": "4ca30373fd0e910ecc000007",
  "date": "2010-09-29"
},
{
  "_id": "4ca305c2fd0e910ecc00000b",
  "name": "news 222",
  "content": "news content 2222",
  "user_id": "4ca30373fd0e910ecc000007",
  "date": "2010-09-29"
},
{
  "_id": "4ca305b5fd0e910ecc00000a",
  "name": "news 111",
  "content": "news content",
  "user_id": "4ca30369fd0e910ecc000006",
  "date": "2010-09-29"
}

如何从PHP运行类似这样的查询?

How to run a query similar like this, from PHP?

SELECT n.*, u.* 
FROM news AS n 
INNER JOIN users AS u ON n.user_id = u.id

推荐答案

MongoDB不支持联接.如果要将用户映射到新闻,可以执行以下操作

MongoDB does not support joins. If you want to map users to the news, you can do the following

1)在应用程序层执行此操作.获取用户列表,并获取新闻列表,并将其映射到您的应用程序中.如果您经常需要此方法,则该方法非常昂贵.

1) Do this at the application-layer. Get the list of users, and get the list of news and map them in your application. This method is very expensive if you need this often.

2)如果需要经常执行上一步,则应重新设计架构,以便新闻文章与用户文档一起作为嵌入式文档存储.

2) If you need to do the previous-step often, you should redesign your schema so that the news articles are stored as embedded documents along with the user documents.

    {
      "_id": "4ca30373fd0e910ecc000007",
      "login": "user22",
      "pass": "example_pass",
      "date": "2010-09-29"
      "news" : [{  
                   "name": "news 222",
                   "content": "news content 2222",
                   "date": "2010-09-29" 
                }, 
                {
                   "name": "news 222",
                   "content": "news content 2222",
                   "date": "2010-09-29"
                }]
    }

一旦使用这种格式的数据,您尝试运行的查询就是隐式的.但是要注意的一件事是,在这种模式下分析查询变得困难.您将需要使用MapReduce来获取最新添加的新闻文章和此类查询.

Once you have your data in this format, the query that you are trying to run is implicit. One thing to note, though, is that analytics queries become difficult on such a schema. You will need to use MapReduce to get the most recently added news articles and such queries.

最后,架构设计以及应用程序可以处理多少非规范化取决于您希望应用程序运行哪种查询.

In the end the schema-design and how much denormalization your application can handle depends upon what kind of queries you expect your application to run.

您可能会发现这些链接很有用. http://www.mongodb.org/display/DOCS/Schema+Design http://www.blip.tv/file/3704083

You may find these links useful. http://www.mongodb.org/display/DOCS/Schema+Design http://www.blip.tv/file/3704083

我希望这会有所帮助.

这篇关于mongodb php-如何执行类似"INNER JOIN"的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆