问题描述
在Node.js
应用程序中,我使用以下代码来获取一些数据:
in a Node.js
App, i use this code to fetch some data:
Users.find({user_enabled:true}).populate('books')...
但是现在我有一个问题.我只希望用户使用英语书籍.
but right now i have a problem. i just want users with books that are in english language.
现在我正在执行此操作:为他们获取所有用户和所有书籍.然后在一个循环中,我可以检查每本书的语言..这不是一个好的解决方案.我需要类似SQL中的Where子句的东西.如果用户没有一本英语书籍,那么结果中不需要该用户. Mongodb
/mongoose
无法做到这一点? j在为此获取所有结果之后必须使用代码吗?
right now i'm doing this: fetch all users and all books for them. then in a loop, i can check each book language.. this is not a good solution. i need something like Where clause in SQL. if a user don't have a book in english language, i don't need that user in the result. Mongodb
/mongoose
cant do this? j have to use a code after fetching all results for this?
我需要这样的东西:
Users.find({user_enabled:true}).populate('books').where({'books.language':'en'})
推荐答案
您可以使用match来做到这一点:
You can do this with match:
Users
.find({ user_enabled: true })
.populate({
path: 'books',
match: { 'language': 'en' },
})
.exec()
有关更多详细信息,请检查 http://mongoosejs.com/docs/populate.html
For more details check http://mongoosejs.com/docs/populate.html
这篇关于猫鼬的Where子句填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!