我正在尝试在mongodb中动态创建$and
查询。但我收到错误SyntaxError: missing ; before statement @(shell)
。是否无法动态创建$and
查询?
db.firstcoll.find({
$and: [{
Thing: {
$in: stuff
}
},
{
Thing: {
$ne: "One three"
}
},
{
Category: "Second"
}
]
}).forEach(function(doc1) {
var words = doc1.Thing.split(' ');
var NewQuery;
if (words.length == 1) {
NewQuery = {
$and: [{
Thing: {
$regex: words[0]
}
},
{
Category: doc1.Category
}
]
}
};
if (words.length == 2) {
NewQuery = {
$and: [{
Thing: {
$regex: words[0]
}
}, {
Thing: {
$regex: words[1]
}
}, {
Category: doc1.Category
}]
}
};
db.secondcoll.find(
NewQuery
).forEach(function(doc2) {
print("stuff: ", doc2.stuff)
})
})