mongodb - mongoose query doesnt work in node.js but works in mongo shell -


I am trying to get a list of Todo based on 'userId' stored against Todo. . Use the following code if any row returns

  DB.TodoTable.find ({"user id": ObjectId ("54c12f5f3620c07019e6b144")}, function (hey, all) {if (mistake) Return console .error (mistake); console.dir (all);})  

The navigator debugs:

  Mongoose: todos.find ({ UserId: undefined}} {field: undefined} []  

But when I try the same in the Mongo shell, it works. This is as follows:

< Pre> & gt; db.todos.find () {"_ id": ObjectId ("54c12f643620c07019e6b145"), "created_at": ISODate ("2015-01-22T17: 12: 04.932Z"), "Uses My database schema is as follows:
 "id": ObjectId ("54c12f5f3620c07019e6b144"), "text": "data", "__v": 0}  

TodoTable = mongoose.model ('Todos', {text: {type: string, default:'}}, created_date: date, user id: {type: object, ref: 'user'}}) on Var UserTable = mongoose.model ('user', {username: string, password: string, email: string, gender: string, address: string});

After

Confusingly, mongoose.Schema.ObjectId is not < Code> ObjectID Constructor function, this is to use only to define schema.

But you do not need to create an object with that string in any way because Mongoos will do it for you on schema definition, so you can use the bus:

  DB.TodoTable.find ({"userId": "54c12f5f3620c07019e6b144"}, function (err, todos) {...})  

Or if you have a situation where you If you are in need of being clear, you can use the ObjectID as the creator:

  DB ("User ID": mongoose.mongo.ObjectID (" 54c12f5f 3620c07019e6b144 ")}, function (mistake, todos) {...})  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -