Reputation: 721
I have problem which has seriously been bothering for the past few days. I have mongoose
setup for a nodejs
project I have defined all the schemas and models as shown below
var studentSchema = new Schema({
fullname: {type: String, required: true},
student_id: {type: String, required: true, unique: true},
votingNo: {type: Number, required: true, unique: true},
voted: {type: Boolean, required: true, default: false}
});
var Student = mongoose.model('Student', studentSchema, 'student');
I have exported the model and i'm using it in another module. whenever I try query for results like so:
model.Student.find({}, function (err, students) {
console.log(err);
console.log(students);
});
I get results. But the moment I add a filter, like so:
model.Student.find({student_id: studentId}, function (err, students) {
console.log(err);
console.log(students);
});
The result is always an empty array.
I've tried using findOne()
but it's always returning null.
Upvotes: 3
Views: 2212
Reputation: 28397
Try to call the queries like this
var Student = mongoose.model('Student');
Student.find({}, function (err, students) {
console.log(err);
console.log(students);
});
If it doesn't work, add this before your call to be sure that the database is open.
var mongoose = require('mongoose');
console.log(mongoose.connection.readyState); // Should not return 0
Hope it helps!
Upvotes: 1