我正在尝试只检索本书的作者中只有favorite_authors中的作者的书籍。 (注意:不是完全匹配,但除了在favorite_authors中以外的任何作者都排除了所有书籍。)
favorite_authors = User.favorite_authors
=> [BSON::ObjectId('5363c73c4d61635257516000'),
BSON::ObjectId('5363c73c4d61635257516001'),
BSON::ObjectId('5363c73c4d61635257516002'),
BSON::ObjectId('5363c73c4d61635257516003'),
BSON::ObjectId('5363c73c4d61635257516004'),
BSON::ObjectId('5363c73c4d61635257516005'),
BSON::ObjectId('5363c73c4d61635257516006')]
class Author
include Mongoid::Document
field :name, type: String
class Book
include Mongoid::Document
field :name, type: String
field :authors, type: Array
一些天真的伪代码,让您了解我想做的事情:
Book.where(authors: relevant_authors).not.where(authors: !relevant_authors)
理想情况下,我想使用Mongoid Origin查询来完成此任务。