如何将SQL语法转换为Mongo?

问题描述

我使用Ruby on Rails和Mongoid

如何转换此语法以使其与Mongo一起使用?

def index
  @conversations = Conversation.where("sender_id = ? OR receiver_id = ?",current_user.id,current_user.id)
end

def index
  @conversation.messages.where("user_id != ? AND read = ?",false).update_all(read: true)
end

解决方法

找到了解决方法:

@conversations = Conversation.where("sender_id = ? OR receiver_id = ?",current_user.id,current_user.id)

更改为

@conversations = Conversation.any_of({sender_id: current_user.id},{receiver_id: current_user.id})

第二行必须完全重写,不能使用。所以我不知道它是否有效

@conversation.messages.where("user_id != ? AND read = ?",false).update_all(read: true)

可能无效

@conversation.messages.not.where(user_id: current_user.id).where(read: false).update_all(read: true)