我在nodejs中使用Mongoose库并使用下面的查询,但我没有收到任何文件。
我尝试了{quiz.score}
和{quiz:{$elemMatch:{score:1}}}
,但都返回0长度或没有值。
我只需要得分为1的文档。
Mongoose Schema
quiz: {
type: Array,
questionId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Question',
index: true
},
score: { type: Number },
time: { type: String }
}
的NodeJS
function(){
var query = {
"quiz.score": 1
};
var select = 'quiz';
quizChild.find({
quiz:{
$elemMatch:{
score:1
}
}
}, select, function(err, attempquestion){
var att = attempquestion[0].quiz.length;
for(var i=0;i<att;i++){
var id = attempquestion[0].quiz[i].questionId;
console.log(id);
}
});
}
MongoDB文档
"quiz" : [
{
"time" : "2016-09-02T17:26:07.109Z",
"score" : "1",
"questionId" : "57c14a36b78cd543fc59b81d"
},
{
"questionId" : "57c14a36b78cd543fc59b81e",
"score" : "0",
"time" : "2016-09-02T17:29:33.699Z"
},
{
"questionId" : "57c14a36b78cd543fc59b81f",
"score" : "1",
"time" : "2016-09-02T17:31:56.329Z"
},
{
"questionId" : "57c14a36b78cd543fc59b81f",
"score" : "1",
"time" : "2016-09-02T17:31:56.345Z"
},
{
"questionId" : "57c14a36b78cd543fc59b820",
"score" : "1",
"time" : "2016-09-02T17:32:14.699Z"
},
{
"questionId" : "57c14a36b78cd543fc59b821",
"score" : "0",
"time" : "2016-09-02T17:32:30.754Z"
}
答案 0 :(得分:0)
将您的查询更改为
var query = {"quiz.score":1, "quiz.score":1};
//or
var query = {"quiz.score":1, $eq:{"quiz.score":1}};
//greater than one
var query = {"quiz.score":1, $gt: {"quiz.score":1}};
//lesser than one
var query = {"quiz.score":1, $lt:{"quiz.score":1}};