mongodb对话系统 [英] mongodb conversation system

查看:53
本文介绍了mongodb对话系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 mongodb 上实现一个非常的对话系统.

I'm implementing a very simple conversation system on mongodb.

这个想法应该是,当我打开一个会议时,它应该显示发送和接收的消息.到目前为止一切正常,应该很容易,通过使用像这个伪代码这样的简单查询:

The idea should be that when I'm opening a convo, it should display send and received messages. It's OK so far and should be pretty easy, by using a simple query like this pseudocode:

(from "my_id" AND to "friend_id") OR (from "friend_id" AND to "my_id")

这应该非常简单明了,但是在我看来,使用 mongodb 进行查询非常复杂(我来自 mysql).

this should be pretty straightforward and simple, but querying just looks so complicated to me with mongodb (I'm coming from mysql).

我正在尝试这个,但它根本不起作用,并且无法找出错误所在.

I'm trying this, but it's not working at all, and can't find out where the error is.

$cursor =$collection->find
            (
                array('$or' =>
                    array('$and' => array("from"=>"$profile", "to"=>"$loggeduser")),
                    array('$and' => array("to"=>"$profile", "from"=>"$loggeduser"))
                )
            )->limit(50)->sort(array('date' => -1));

这什么都不返回....错误在哪里?

this returns nothing.... Where's the mistake?

提前致谢.

推荐答案

查看此页面,了解如何进行高级 MongoDB 查询:http://www.mongodb.org/display/DOCS/Advanced+Queries

Take a look at this page on how to do advanced MongoDB queries: http://www.mongodb.org/display/DOCS/Advanced+Queries

您可以组合使用 $and$in 运算符来获得所需内容.使用 mongo shell,您的查询将如下所示:

You can use a combination of the $and and $in operators to get what you need. Using the mongo shell, your query would look something like this:

db.yourCollectionName.find({$and: {from: {$in: ["toUser", "loggedOnUser"]}}, {to: {$in: ["toUser", "loggedOnUser"]}}})

我相信这也可能给你等价的:

I believe this may also give you the equivalent:

db.yourCollectionName.find({$and: {$or: [{from: "toUser"}, {to: "toUser"}]}}, {$or: [{from: "loggedOnUser"}, {to: "loggedOnUser"}]}}})

接下来就是将上述内容转换为您使用的语言/DSL,并按日期排序.

From there it's a matter of converting the above to the language/DSL that you're using, and sorting by date.

在您的代码中,您不需要 ($and => array()) 包装您要查找的每个对象.删除它们,看起来像这样:

In your code, you don't need the ($and => array()) wrapping each of the objects that you're trying to find. Remove them, so it looks like this:

$cursor = $collection->find(
    array('$or' => 
        array(
            array("from"=>"$profile", "to"=>"$loggeduser"),
            array("to"=>"$profile", "from"=>"$loggeduser")
        )
    )
) ->limit(50)->sort(array('date' => -1));

这篇关于mongodb对话系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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