我不知道该怎么办。我试着把$和&或语法,但我的代码总是出错。我试过投影。并且有一种感觉,我可能需要单独运行查询。
这是我在mongodb游乐场中尝试在VS代码中运行的最后一个代码:
db.movies.find(
{ $and : [
{ $or : [ { genre: { $eq: "Action" } }, { genre: $eq:"Adventure"},
{ $or : [ { title: "Wonder Woman" }, { title: 1 },
{ $or : [ { title: "Cloud Atlas" }, { title: 1 },
{ $or : [ { title: "Pan's Labyrinth"}, { title: 1 },
{ $or : [ { title: "Gone With the Wind"}, { title: 1 },
{ $or : [ { title: "Spaceballs"}, { title: 1 },
{ $or : [ { title: "Silence of the Lambs"}, { title: 1 },
{ $or : [ { title: "American History X"}, { title: 1 },
{ $or : [ { title: "Psycho"}, { title: 1 },
{ $or : [ { title: "The Pianist"}, { title: 1 },
{ $or : [ { title: "Gladiator"},{ title: 1 } } ] }
]}
)
有人能帮助我做什么可能是错误的,我能做什么吗?
您只是混合了负责只返回某些字段的project
对象和负责过滤文档的query
对象。
以下是它的修复方式:
db.collection.find({
$or: [
{
genre: "Action"
},
{
genre: "Adventure"
}
]
},
{
_id: 0,
title: 1
})
你也可以稍微清理一下,然后使用$in操作符:
db.collection.find({
genre: {
$in: [
"Action",
"Adventure"
]
}
},
{
_id: 0,
title: 1
})
Mongo游乐场