2

Suppose I have a collection in my mongoDB: db.co and only have one document:

{ "_id" : ObjectId("50d083e32cdcf7ce065b616c"), 
  "age" : 22, 
  "friends" : [ "Tom"], 
  "location" : "NY", 
  "name" : "lee", 
  "skill" : [ "javascript", "java" ] 
 }

Then I want query it in node.js with mongoose through this code:

var coSchema = mongoose.Schema( {
    age: Number,
    friends: Array, 
    location: String,
    name: String,
    skill: Array
})

var Co = mongoose.model('Co', coSchema);
function findSomeoneInCo (name) {
  Co.find({"name": name}, function (err, doc) {
    if (err) {
        console.log('find some one failed: ' + err);
        return;
    }
    console.log('find successed: ' + doc);
  })
}

findSomeoneInCo("lee");

But it return me nothing

What's problem with my code? How can I get the corrent query result?

3

4 Answers 4

7

Mongoose pluralizes the lower-cased model name when determining the collection name to use. So with a model name of 'Co' it's going to be looking in the cos collection by default.

To override the default and align with your existing co collection:

var Co = mongoose.model('Co', coSchema, 'co');
3

Mongoose pluralizes your model name during any CURD operation, Use this & check :

var Co = mongoose.model('Co', coSchema, 'co'); //Even the case matters

or

var coSchema = new Schema({
    age: Number,
    friends: Array, 
    location: String,
    name: String,
    skill: Array
}, {collection:'co'});
var Co = mongoose.model('Co',coSchema);
1

.find() works asynchronous, meaning that its callback runs when your function already has.

see variable scope in asynchronous function

//try passing a callback function to your search function, like:
function findSomeoneInCo (name, callback) {

  //happens now - here you could still return something

  Co.find({"name": name}, function (err, doc) {

   //happens later - too late to return stuff, function has ran already

    if (err) {
        callback(err);
        return;
    }
    callback(doc);
  })
}


//call your function passing a callback function
findSomeoneInCo("lee", function(doc){
  console.log('do something with your results: ' + doc);
});
1
  • I tried this way, but it doesn't success.In fact, the first way without callback, I can query another collection's data.Just can query this one
    – hh54188
    Dec 22, 2012 at 13:54
1

I notice your callback to find() has the following parameters : err, doc

find() always returns an array so you really want this to be : err, docs

OR use findOne()

The async stuff shouldn't be a problem in the code you've posted, the nested callbacks will still be executed. Are you sure your connection is OK. I would run an open query :

Co.find({}, function(err, docs){
 console.log(docs);
}

Just to check the collection has something in it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.