问题描述
我试图建立一个消息系统。我打算收集消息,然后根据发件人和收件人查询消息。为了做到这一点,我需要查询例如:由Bob发送并由Tony接收的所有消息以及由Tony发送并由Bob接收的所有消息。我很不清楚如何做这个或声明。
截至目前为止我已经有
$ b $ p $ Message $ $ c $ MessageRef.once('value',function(dataSnapshot){
console.log (dataSnapshot.val());
});
返回所有消息。然后,我可以做一个forEach,但似乎不是很有效。你们有什么建议吗?还有,你有没有关于我的模型的建议,建立这个消息系统比拥有聊天室好。
谢谢
Firebase目前只能查询一个儿童财产。所以如果你想查询多个属性,你必须将它们合并为一个:
消息
-DFHJ3498ua
from:Tony
to:Bob
from_to:Tony_Bob
message:Hello Bob,this is Tony
-DFHJ3598uz
from:Bob
to:Tony
from_to:Bob_Tony
留言:您好,Tony,您能帮我些忙吗?
-EFHJ3498ua
从:Tony
到:Bob
from_to:Tony_Bob
消息:您能帮我解决这个Firebase查询吗?
您可以使用以下命令查询Bob到Tony的消息:
var ref = new Firebase('https://your.firebaseio.com/Messages');
var query = ref.orderByChild('from_to')。equalTo('Bob_Tony');
query.on('value',function(snapshot){
console.log(snapshot.val()); //从Bob到Tony的所有消息
});
在类似聊天的应用程序中,您可能要考虑实际使用发送者 - 接收者对密码:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ :Tony
留言:你好,托尼,我能为你做什么?
from_Tony_to_Bob
-DFHJ3498ua
from:Tony
to:Bob
留言:Hello Bob,this is Tony
-EFHJ3498ua
from :Tony
到:Bob
消息:您可以帮助我使用此Firebase查询吗?
使用这个数据结构,您不必运行查询来获取相关消息,直接查找。
var ref = new Firebase('https://your.firebaseio.com/Messages');
var query = ref.child('from_Bob_to_Tony');
query.on('value',function(snapshot){
console.log(snapshot.val()); //从Bob到Tony的所有消息
});
I am trying to build a message system. I am planning on having a collections of messages and then querying messages based on sender and receivers. In order to do so I need to query for example: all messages that are sent by Bob and received by Tony and all messages that were sent by Tony and received by Bob. I am quite unclear about how to do this "or" statement. As of now I have
MessageRef.once('value', function(dataSnapshot) { console.log(dataSnapshot.val()); });
which return all messages. I could then do a forEach but it seems not very efficient. Would you guys have any suggestions?
Also, do you have thoughts on wether my model to build that message system is good vs having chat rooms for example.
Thank you
Firebase currently can only query on a single child property. So if you want to query multiple properties, you'll have to combine them into one:
Messages
-DFHJ3498ua
from: Tony
to: Bob
from_to: Tony_Bob
message: "Hello Bob, this is Tony"
-DFHJ3598uz
from: Bob
to: Tony
from_to: Bob_Tony
message: "Hello Tony, What can I do for you?"
-EFHJ3498ua
from: Tony
to: Bob
from_to: Tony_Bob
message: "Can you help me with this Firebase query?"
You can then query for messages from Bob to Tony using:
var ref = new Firebase('https://your.firebaseio.com/Messages');
var query = ref.orderByChild('from_to').equalTo('Bob_Tony');
query.on('value', function(snapshot) {
console.log(snapshot.val()); // all messages from Bob to Tony
});
In a chat-like application, you might want to consider actually using the sender-receiver pair as the key:
Messages
from_Bob_to_Tony
-DFHJ3598uz
from: Bob
to: Tony
message: "Hello Tony, What can I do for you?"
from_Tony_to_Bob
-DFHJ3498ua
from: Tony
to: Bob
message: "Hello Bob, this is Tony"
-EFHJ3498ua
from: Tony
to: Bob
message: "Can you help me with this Firebase query?"
With this data structure you won't have to run a query to get the relevant messages, but can do a direct lookup.
var ref = new Firebase('https://your.firebaseio.com/Messages');
var query = ref.child('from_Bob_to_Tony');
query.on('value', function(snapshot) {
console.log(snapshot.val()); // all messages from Bob to Tony
});
这篇关于或在Firebase中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!