【问题标题】:MongoDB find resultMongoDB查找结果
【发布时间】:2016-02-07 12:22:49
【问题描述】:

我在用户和消息之间有一个连接表,我将获得所有 messageUserConnection - 发送者的对象(userCreatedMessage 和 userReceivedMessage 相同),并且消息 ID 相同并且读取的位置是 false 任何 messageUserConnection 或者说简单: 发送者/用户将收到所有已发送的 messageUserConnections,但并非所有接收者都已确认消息。

MessageUserConnection 的查询字符串:

db.MessageUserConnection.find()

MessageUserConnection 发件人条目

一个MessageUserConnection,其中消息的接收者没有确认消息(messageId 相同,read = false)

{
  "confirmationNeeded": true,
  "read": false,
  "userCreatedMessage": DBRef("User",
  ObjectId("5589929b887dc1fdb501cdba")),
  "userReceivedMessage": DBRef("User",
  ObjectId("5589929b887dc1fdb501cdba")),
  "message": DBRef("Message",
  ObjectId("563c57adb1a9bb55d70bd407"))
}

这是我的查询:

db.MessageUserConnection.find( { $and: [ {'confirmationNeeded': true}, { $and: [ {'userCreatedMessage.$id' : ObjectId('5589929b887dc1fdb501cdba') }, {'userReceivedMessage.$id' : ObjectId('5689929b887dc1fdb501cdbc') } ] } ] } ) 

我现在的问题是我不知道如何扩展查询以考虑接收者的 read 属性。

如果有人能提示我如何做到这一点,那就太好了。 非常感谢!

[编辑]

这是我的问题的图形可视化。我将拥有的是 - 在这种情况下 - 只有 ID = 1 的 MessageUserConnection 因为收到此消息的一个用户(id = 3)没有阅读该消息。 有没有可能用一个 MongoDB 查询来做到这一点? 非常感谢!

【问题讨论】:

  • 所以您想要$and 中的多个条件......我对吗??
  • 是的,但我不知道如何使用 $ 和发件人的 messageid 进行查询,以及如何检查 read 是否设置为 false,其中 messageid 相同就像在 sender-messageuserconnection 中一样。谢谢!

标签: mongodb mongodb-query


【解决方案1】:
db.MessageUserConnection.find( { $and: [ {'confirmationNeeded': true}, {'read': false}, {'userCreatedMessage.$id' : ObjectId('5589929b887dc1fdb501cdba') }, {'userReceivedMessage.$id' : ObjectId('5689929b887dc1fdb501cdbc') } ] } )

我认为你可以通过上面提到的查询来做到这一点..如果我得到你的问题..不知道:(

【讨论】:

  • 为什么是第一个$and? MongoDB 默认在查询元素之间执行 $and
猜你喜欢
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 2019-06-14
  • 1970-01-01
  • 2015-11-22
  • 2018-05-15
  • 1970-01-01
相关资源
最近更新 更多