我有一個嵌套的文件,它看起來像:
var User = new Schema({
id: String,
position: [{
title: String,
applied:[{
candidate_id: String,
name: String
}],
}],
我所希望做的是返回所有符合這某種「candidate_id」
的「應用」子文檔我到目前爲止有:
app.get('/applied', function(req, res){
var position = "58dc2bd4e7208a3ea143959e";
User.aggregate(
{$unwind : "$position"},
{$unwind : "$position.applied"},
{$match:{'position.applied.candidate_id': position}}).exec(function (err, result) {
console.log(result);
});
res.render('applied', { title: 'applied',layout:'candidate'});
});
我有返回所有匹配的位置的另一種功能,而且代碼的工作:
app.post('/search', function (req, res) {
var position = new RegExp(req.body.position, 'i');
var location = new RegExp(req.body.location, 'i');
User.aggregate(
{$unwind : "$position"},
{$match:{'position.title': position,'position.location':location}}).exec(function (err, result) {
console.log(result);
res.send({ results: result });
});
});
所以基本上我正在努力獲得一個子子文檔。任何想法,我要去哪裏錯了?
的樣本數據:
{
"_id" : ObjectId("58c2871414cd3d209abf5fc9"),
"position" : [
{
"_id" : ObjectId("58d6b7e11e793c9a506ffe8f"),
"title" : "Software Engineer",
"applied" : [
{
"candidate_id" : "58d153e97e3317291gd80087",
"name" : "Sample user"
},
{
"candidate_id" : "58d153e97e3317291fd99001",
"name" : "Sample User2"
}
]
},
{
"_id" : ObjectId("58c2871414cd3d209abf5fc0"),
"title" : "Software Engineer",
}
],
}
什麼上面去,就表示有2個職位,其中(第一項)一個已經2應用候選人,我需要做的是返回嵌套如果它匹配貓鼬查詢。
發生了什麼事?你也有一些示例數據,這不正常工作? –
@ExplosionPills該數組將返回空。我將更新問題以顯示一些示例數據! – user